summaryrefslogtreecommitdiffstats
path: root/src/utils/hooks/use-pagination.tsx
blob: a80a53938f064ed0f22aa3157d1dbcb25ab88e09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { type EdgesResponse, type EdgesVars } from '@services/graphql/api';
import useSWRInfinite, { SWRInfiniteKeyLoader } from 'swr/infinite';

export type UsePaginationProps<T> = {
  /**
   * The initial data.
   */
  fallbackData: EdgesResponse<T>[];
  /**
   * A function to fetch more data.
   */
  fetcher: (props: EdgesVars) => Promise<EdgesResponse<T>>;
  /**
   * The number of results per page.
   */
  perPage: number;
  /**
   * An optional search string.
   */
  search?: string;
};

export type UsePaginationReturn<T> = {
  /**
   * The data from the API.
   */
  data?: EdgesResponse<T>[];
  /**
   * An error thrown by fetcher.
   */
  error: any;
  /**
   * Determine if there's more data to fetch.
   */
  hasNextPage?: boolean;
  /**
   * Determine if the initial data is loading.
   */
  isLoadingInitialData: boolean;
  /**
   * Determine if more data is currently loading.
   */
  isLoadingMore?: boolean;
  /**
   * Determine if the data is refreshing.
   */
  isRefreshing?: boolean;
  /**
   * Determine if there's a request or revalidation loading.
   */
  isValidating: boolean;
  /**
   * Set the number of pages that need to be fetched.
   */
  setSize: (
    size: number | ((_size: number) => number)
  ) => Promise<EdgesResponse<T>[] | undefined>;
};

/**
 * Handle data fetching with pagination.
 *
 * This hook is a wrapper of `useSWRInfinite` hook.
 *
 * @param {UsePaginationProps} props - The pagination configuration.
 * @returns {UsePaginationReturn} An object with pagination data and helpers.
 */
const usePagination = <T extends object>({
  fallbackData,
  fetcher,
  perPage,
  search,
}: UsePaginationProps<T>): UsePaginationReturn<T> => {
  const getKey: SWRInfiniteKeyLoader = (
    pageIndex: number,
    previousData: EdgesResponse<T>
  ): EdgesVars | null => {
    // Reached the end.
    if (previousData && !previousData.edges.length) return null;

    // Fetch data using this parameters.
    return pageIndex === 0
      ? { first: perPage, search }
      : {
          first: perPage,
          after: previousData.pageInfo.endCursor,
          search,
        };
  };

  const { data, error, isValidating, size, setSize } = useSWRInfinite(
    getKey,
    fetcher,
    { fallbackData }
  );

  const isLoadingInitialData = !data && !error;
  const isLoadingMore =
    isLoadingInitialData ||
    (size > 0 && data && typeof data[size - 1] === 'undefined');
  const isRefreshing = isValidating && data && data.length === size;
  const hasNextPage =
    data && data.length > 0 && data[data.length - 1].pageInfo.hasNextPage;

  return {
    data,
    error,
    hasNextPage,
    isLoadingInitialData,
    isLoadingMore,
    isRefreshing,
    isValidating,
    setSize,
  };
};

export default usePagination;