Last active
September 4, 2023 23:47
-
-
Save WarrenBuffering/7851dd4f3a21e297de5b0757c5d9ce3d to your computer and use it in GitHub Desktop.
goated hooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useMemo, useState } from 'react'; | |
import { FetchStatus } from '../enums'; | |
export function useFetchStatus() { | |
const [errorCode, setErrorCode] = useState<number | null>(null); | |
const [errorMessage, setErrorMessage] = useState<string>(''); | |
const [status, setStatus] = useState<FetchStatus>(FetchStatus.UNFETCHED); | |
const fail = useCallback((message: string, code?: number) => { | |
setStatus(FetchStatus.FAIL); | |
setErrorMessage(message); | |
setErrorCode(code || null); | |
}, []); | |
const reset = useCallback(() => setStatus(FetchStatus.UNFETCHED), []); | |
const start = useCallback(() => { | |
setStatus(FetchStatus.FETCHING); | |
setErrorCode(null); | |
setErrorMessage(''); | |
}, []); | |
const succeed = useCallback(() => setStatus(FetchStatus.SUCCESS), []); | |
const isFetching = useMemo(() => status === FetchStatus.FETCHING, [status]); | |
const isError = useMemo(() => status === FetchStatus.FAIL, [status]); | |
const isSuccess = useMemo(() => status === FetchStatus.SUCCESS, [status]); | |
const isUnfetched = useMemo(() => status === FetchStatus.UNFETCHED, [status]); | |
return { | |
errorMessage, | |
errorCode, | |
isFetching, | |
isError, | |
isSuccess, | |
isUnfetched, | |
reset, | |
start, | |
fail, | |
succeed, | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useCallback, useMemo, useState } from 'react'; | |
export enum InputStatus { | |
VALID = 'valid', | |
INVALID = 'invalid', | |
UNKNOWN = 'unknown', | |
} | |
export function useInputValue(initialValue: string) { | |
const [value, setValue] = useState(initialValue || ''); | |
const [statusMessage, setStatusMessage] = useState(''); | |
const [status, setStatus] = useState<InputStatus>(InputStatus.UNKNOWN); | |
const clearStatus = useCallback((): void => { | |
setStatusMessage(''); | |
setStatus(InputStatus.UNKNOWN); | |
}, []); | |
const invalidate = useCallback((msg?: string): void => { | |
setStatus(InputStatus.INVALID); | |
setStatusMessage(msg); | |
}, []); | |
const setStatus = useCallback((sts: InputStatus): void => { | |
setStatus(sts); | |
}, []); | |
const setStatusMessage = useCallback((msg: string): void => { | |
setStatusMessage(msg); | |
}, []); | |
const update = useCallback((val: string): void => { | |
setValue(val); | |
}, []); | |
const validate = useCallback((msg?: string): void => { | |
setStatus(InputStatus.VALID); | |
setStatusMessage(msg); | |
}, []); | |
const isInvalid = useMemo(() => status === InputStatus.INVALID, [status]); | |
const isValid = useMemo(() => status === InputStatus.VALID, [status]); | |
return { | |
clearStatus, | |
invalidate, | |
isInvalid, | |
isValid, | |
setStatus, | |
setStatusMessage, | |
status, | |
statusMessage, | |
update, | |
validate, | |
value, | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment