diff --git a/src/api/common/utils.tsx b/src/api/common/utils.tsx index 8ee5be1..b6d12a4 100644 --- a/src/api/common/utils.tsx +++ b/src/api/common/utils.tsx @@ -5,6 +5,7 @@ import { useMutation, } from '@tanstack/react-query'; +import { API_CONSTS } from '../consts'; import type { PaginateQuery } from '../types'; type KeyParams = { @@ -123,3 +124,16 @@ export const parseAxiosError = (error: any) => { } return DEFAULT_ERROR_MESSAGE; }; + +export const getPage = (lastPage: any): number | undefined => { + const nextUrl = lastPage.pagination.next_url; + const queryString = nextUrl.split('?')[1]; + const urlParams = new URLSearchParams(queryString); + const page = urlParams.get('page'); + return page ? parseInt(page, 10) : undefined; +}; + +export const DEFAULT_PAGE_PARAMS = { + getNextPageParam: (lastPage: any) => getPage(lastPage), + initialPageParam: API_CONSTS.INITIAL_PAGE, +}; diff --git a/src/api/favorites/use-favorites.ts b/src/api/favorites/use-favorites.ts index 6219f6e..f989489 100644 --- a/src/api/favorites/use-favorites.ts +++ b/src/api/favorites/use-favorites.ts @@ -1,6 +1,6 @@ import { createInfiniteQuery } from 'react-query-kit'; -import { client } from '../common'; +import { client, DEFAULT_PAGE_PARAMS } from '../common'; import { API_CONSTS, QUERY_KEYS } from '../consts'; import type { FavoriteProductsResponse } from './types'; @@ -25,12 +25,5 @@ export const useFavorites = createInfiniteQuery({ ); return data; }, - getNextPageParam: (lastPage) => { - const nextUrl = lastPage.pagination.next_url; - const queryString = nextUrl.split('?')[1]; - const urlParams = new URLSearchParams(queryString); - const page = urlParams.get('page'); - return page ? parseInt(page, 10) : undefined; - }, - initialPageParam: 1, + ...DEFAULT_PAGE_PARAMS, }); diff --git a/src/api/products/use-products.ts b/src/api/products/use-products.ts index b6d9480..d713290 100644 --- a/src/api/products/use-products.ts +++ b/src/api/products/use-products.ts @@ -1,6 +1,6 @@ import { createInfiniteQuery } from 'react-query-kit'; -import { client } from '../common'; +import { client, DEFAULT_PAGE_PARAMS } from '../common'; import { API_CONSTS, QUERY_KEYS } from '../consts'; import type { FetchProductsResponse } from './types'; @@ -24,12 +24,5 @@ export const useProducts = createInfiniteQuery({ }); return data; }, - getNextPageParam: (lastPage) => { - const nextUrl = lastPage.pagination.next_url; - const queryString = nextUrl.split('?')[1]; - const urlParams = new URLSearchParams(queryString); - const page = urlParams.get('page'); - return page ? parseInt(page, 10) : undefined; - }, - initialPageParam: 1, + ...DEFAULT_PAGE_PARAMS, }); diff --git a/src/api/purchase/get-purchases.ts b/src/api/purchase/get-purchases.ts index 2781288..787697b 100644 --- a/src/api/purchase/get-purchases.ts +++ b/src/api/purchase/get-purchases.ts @@ -1,6 +1,6 @@ import { createInfiniteQuery } from 'react-query-kit'; -import { client } from '../common'; +import { client, DEFAULT_PAGE_PARAMS } from '../common'; import { API_CONSTS, QUERY_KEYS } from '../consts'; import type { PurchasesResponse } from './types'; @@ -22,12 +22,5 @@ export const usePurchases = createInfiniteQuery({ }); return data; }, - getNextPageParam: (lastPage) => { - const nextUrl = lastPage.pagination.next_url; - const queryString = nextUrl.split('?')[1]; - const urlParams = new URLSearchParams(queryString); - const page = urlParams.get('page'); - return page ? parseInt(page, 10) : undefined; - }, - initialPageParam: 1, + ...DEFAULT_PAGE_PARAMS, });