diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-15 16:36:58 +0200 |
|---|---|---|
| committer | Armand Philippot <git@armandphilippot.com> | 2022-05-15 16:36:58 +0200 |
| commit | 235fe67d770f83131c9ec10b99012319440db690 (patch) | |
| tree | 3b96e2c8a5877fe15a9cfa6bff46130fa7a04a65 /src/utils | |
| parent | fe2252ced2bb895e26179640553b5a6c02957d54 (diff) | |
chore: add Search page
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/hooks/use-data-from-api.tsx | 23 | ||||
| -rw-r--r-- | src/utils/hooks/use-pagination.tsx | 3 |
2 files changed, 25 insertions, 1 deletions
diff --git a/src/utils/hooks/use-data-from-api.tsx b/src/utils/hooks/use-data-from-api.tsx new file mode 100644 index 0000000..7082941 --- /dev/null +++ b/src/utils/hooks/use-data-from-api.tsx @@ -0,0 +1,23 @@ +import { useEffect, useState } from 'react'; + +/** + * Fetch data from an API. + * + * This hook is a wrapper to `setState` + `useEffect`. + * + * @param fetcher - A function to fetch data from API. + * @returns {T | undefined} The requested data. + */ +const useDataFromAPI = <T extends unknown>( + fetcher: () => Promise<T> +): T | undefined => { + const [data, setData] = useState<T>(); + + useEffect(() => { + fetcher().then((apiData) => setData(apiData)); + }, [fetcher]); + + return data; +}; + +export default useDataFromAPI; diff --git a/src/utils/hooks/use-pagination.tsx b/src/utils/hooks/use-pagination.tsx index 1e24b75..a80a539 100644 --- a/src/utils/hooks/use-pagination.tsx +++ b/src/utils/hooks/use-pagination.tsx @@ -99,7 +99,8 @@ const usePagination = <T extends object>({ isLoadingInitialData || (size > 0 && data && typeof data[size - 1] === 'undefined'); const isRefreshing = isValidating && data && data.length === size; - const hasNextPage = data && data[data.length - 1].pageInfo.hasNextPage; + const hasNextPage = + data && data.length > 0 && data[data.length - 1].pageInfo.hasNextPage; return { data, |
