From 235fe67d770f83131c9ec10b99012319440db690 Mon Sep 17 00:00:00 2001 From: Armand Philippot Date: Sun, 15 May 2022 16:36:58 +0200 Subject: chore: add Search page --- src/utils/hooks/use-data-from-api.tsx | 23 +++++++++++++++++++++++ src/utils/hooks/use-pagination.tsx | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/utils/hooks/use-data-from-api.tsx (limited to 'src/utils/hooks') 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 = ( + fetcher: () => Promise +): T | undefined => { + const [data, setData] = useState(); + + 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 = ({ 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, -- cgit v1.2.3