Skip to content

Commit

Permalink
refactor: reuse getNextPageParam logic across queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Sofia Cantero committed Dec 16, 2024
1 parent 2628be0 commit 23582c2
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 27 deletions.
14 changes: 14 additions & 0 deletions src/api/common/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
useMutation,
} from '@tanstack/react-query';

import { API_CONSTS } from '../consts';
import type { PaginateQuery } from '../types';

type KeyParams = {
Expand Down Expand Up @@ -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,
};
11 changes: 2 additions & 9 deletions src/api/favorites/use-favorites.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
});
11 changes: 2 additions & 9 deletions src/api/products/use-products.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
});
11 changes: 2 additions & 9 deletions src/api/purchase/get-purchases.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
});

0 comments on commit 23582c2

Please sign in to comment.