Skip to content

Commit

Permalink
ERM-3220 Update pagination mechanisms for MCLs to work without stats
Browse files Browse the repository at this point in the history
* create new hook for fetch with no stats
* add hasNextPage prop to usePrevNextPagination
  • Loading branch information
CalamityC committed May 8, 2024
1 parent e0bd255 commit 078d8a9
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export { default as useInterfaceCredentials } from './useInterfaceCredentials';
export { default as useSingleFetchInterfaceCredentials } from './useSingleFetchInterfaceCredentials';
export { default as useChunkedCQLFetch } from './useChunkedCQLFetch';
export { default as useChunkedUsers } from './useChunkedUsers';
export { default as useFetchWithNoStats } from './useFetchWithNoStats';
export { default as useParallelBatchFetch } from './useParallelBatchFetch';
export { default as usePrevNextPagination } from './usePrevNextPagination';
export { default as usePrevious } from './usePrevious';
Expand Down
54 changes: 54 additions & 0 deletions lib/hooks/useFetchWithNoStats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { useMemo } from 'react';
import { useQueries } from 'react-query';
import { useOkapiKy } from '@folio/stripes/core';
import { generateKiwtQueryParams } from '@k-int/stripes-kint-components';

const usePrevNextPagination = ({
id, // for future use / local zustand store

Check warning on line 7 in lib/hooks/useFetchWithNoStats.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'id' is assigned a value but never used. Allowed unused vars must match /React/u

Check warning on line 7 in lib/hooks/useFetchWithNoStats.js

View workflow job for this annotation

GitHub Actions / github-actions-ci

'id' is assigned a value but never used. Allowed unused vars must match /React/u
params = {},
path = '',
keyArray = [],
} = {}) => {
const ky = useOkapiKy();
const queryArray = [];
const currentPageParams = useMemo(() => (
generateKiwtQueryParams(
{ ...params, stats: false },
{}
)
), [params]);
const currentPageParamsSpread = [...currentPageParams];
queryArray.push({
queryKey: [
path,
currentPageParams,
...keyArray
],
queryFn: () => ky.get(`${path}?${currentPageParamsSpread?.join('&')}`).json(),
});

const nextPageParams = useMemo(() => (
generateKiwtQueryParams(
{ ...params, page: params.page + 1, stats: false },
{}
)
), [params]);
const nextPageParamsSpread = [...nextPageParams];
queryArray.push({
queryKey: [
path,
nextPageParams,
...keyArray
],
queryFn: () => ky.get(`${path}?${nextPageParamsSpread?.join('&')}`).json(),
});

const queries = useQueries(queryArray);

return ({
currentPage: queries[0],
nextPage: queries[1],
});
};

export default usePrevNextPagination;
4 changes: 2 additions & 2 deletions lib/hooks/usePrevNextPagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const usePrevNextPagination = ({
pageSize = DEFAULT_PAGINATION_SIZE, // Only needed for reading back MCL props
id = DEFAULT_PAGE_KEY, // This id is ONLY used for syncToLocation: false cases, as a key to the zustand store
syncToLocation = true, // Used to turn on/off location syncing, so can be used as standalone state if required,
pageCount = 0
hasNextPage = false
} = {}) => {
/* ------ ZUSTAND STORE ------ */
// For NON-SYNC-TO-LOCATION use cases, store the currentPage in a keyed store
Expand Down Expand Up @@ -147,7 +147,7 @@ const usePrevNextPagination = ({
]);

// Set up MCL specific props based on page
const pagingCanGoNext = currentPage && (currentPage < Number(count) / pageSize || pageCount === pageSize);
const pagingCanGoNext = currentPage && (currentPage < Number(count) / pageSize || hasNextPage);
const pagingCanGoPrevious = currentPage && Number(currentPage) > 1;
const pagingOffset = currentPage ? (currentPage - 1) * pageSize : 0;
const onNeedMoreData = (...args) => {
Expand Down

0 comments on commit 078d8a9

Please sign in to comment.