aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/hooks
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/hooks')
-rw-r--r--src/utils/hooks/use-data-from-api.tsx23
-rw-r--r--src/utils/hooks/use-pagination.tsx3
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,