-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
63 additions
and
54 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"printWidth": 80, | ||
"printWidth": 120, | ||
"semi": true, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export { useData } from './useData/useData'; | ||
export { useOutsideClick } from './useOutsideClick/useOutsideClick'; | ||
export { usePortal } from './usePortal/usePortal'; | ||
export { useUrlParam } from './useUrlParam/useUrlParam'; | ||
export { useUrlParamReactRouter } from './useUrlParam/useUrlParamReactRouter'; | ||
export * from './useData/useData'; | ||
export * from './useOutsideClick/useOutsideClick'; | ||
export * from './usePortal/usePortal'; | ||
export * from './useUrlParam/useUrlParam'; | ||
export * from './useUrlParam/useUrlParamReactRouter'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,64 @@ | ||
import { useCallback, useRef, useState } from 'react'; | ||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; | ||
|
||
type execFn = (token: AbortController) => Promise<unknown>; | ||
export type UseDataExecuteFn = (controller: AbortController) => Promise<Response>; | ||
export type UseDataRequestor = (fn: UseDataExecuteFn) => Promise<void>; | ||
export type UseDataReturn<T> = [UseDataResponse<T>, UseDataRequestor]; | ||
export type UseDataStatuses = 'IDLE' | 'FETCHING' | 'RESPONDING'; | ||
export type UseDataOptions = { | ||
debug?: boolean; | ||
}; | ||
export type UseDataResponse<T> = { | ||
data: T | null; | ||
raw: Response | null; | ||
status: UseDataStatuses; | ||
}; | ||
|
||
const defaultResponse = { raw: null, data: null, status: 'IDLE' }; | ||
|
||
/** | ||
* @description A custom hook to fetch data from an API. | ||
* @param {unknown} defaultValue The default value to return if no data is fetched. | ||
* @returns {Array} An array containing the fetched data and the request function. | ||
* @param {boolean} options Options settings for the hook. | ||
* @returns {T} An object or array of type T. | ||
*/ | ||
export const useData = (defaultValue = null) => { | ||
const [response, setResponse] = useState<unknown>(defaultValue); | ||
const [status, setStatus] = useState('IDLE'); | ||
export function useData<T>({ debug }: UseDataOptions = { debug: false }): UseDataReturn<T> { | ||
const [response, setResponse] = useState<UseDataResponse<T> | null>(null); | ||
const [status, setStatus] = useState<UseDataStatuses>('IDLE'); | ||
const cancelRef = useRef<AbortController | null>(null); | ||
|
||
const requestor = useCallback(async (fn: execFn) => { | ||
useEffect(() => { | ||
if (debug === true) console.log('Fetch Response: ', response); | ||
}, [debug, response]); | ||
|
||
useEffect(() => { | ||
if (debug === true) console.log('Fetch Status: ', status); | ||
}, [debug, status]); | ||
|
||
const resp = useMemo(() => { | ||
return { | ||
...(response ?? defaultResponse), | ||
status, | ||
}; | ||
}, [response, status]); | ||
|
||
const requestor: UseDataRequestor = useCallback(async (fn: UseDataExecuteFn) => { | ||
async function go() { | ||
if (cancelRef.current != null) { | ||
cancelRef.current.abort(); | ||
cancelRef.current.abort('Canceled'); | ||
} | ||
cancelRef.current = new AbortController(); | ||
|
||
setStatus('LOADING'); | ||
setResponse(await fn(cancelRef.current)); | ||
setStatus('FETCHING'); | ||
const res = await fn(cancelRef.current); | ||
const data = (await res.json()) as T; | ||
setResponse({ | ||
raw: res, | ||
data, | ||
status: 'RESPONDING', | ||
}); | ||
setStatus('IDLE'); | ||
} | ||
await go(); | ||
}, []); | ||
|
||
return [{ ...(response ?? {}), status }, requestor]; | ||
}; | ||
return [resp, requestor] as UseDataReturn<T>; | ||
} |
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
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
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
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
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
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