-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ERM-3220 Update pagination mechanisms for MCLs to work without stats (#…
…680) * ERM-3220 Update pagination mechanisms for MCLs to work without stats * add pageCount property * ERM-3220 Update pagination mechanisms for MCLs to work without stats * create new hook for fetch with no stats * add hasNextPage prop to usePrevNextPagination * ERM-3220 Update pagination mechanisms for MCLs to work without stats * create new hook for fetch with no stats * add hasNextPage prop to usePrevNextPagination * refactor: Changed hasNextPage functionality Now the hasNextPage overwrites the canGoNext calculation if it is provided * refactor: Spread params tidy up * ERM-3220 Update pagination mechanisms for MCLs to work without stats * rename hook * set defaukt page * ERM-3220 Update pagination mechanisms for MCLs to work without stats * remove comments * add resetPageStore function * ERM-3220 Update pagination mechanisms for MCLs to work without stats * remove resetPageStore function * feat: useFetchMultiplePages Renamed hook to "useFetchMultiplePages", so it will be primed for any future logic to expand past just two pages. For now it will make use of the page passed in the params object Also changed out keyArray for getQueryKey which allows finer control over the queryKey for the implementing code refs ERM-3220 --------- Co-authored-by: Jack Golding <[email protected]> Co-authored-by: Ethan Freestone <[email protected]>
- Loading branch information
1 parent
a19aa79
commit 8748eb8
Showing
3 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useMemo } from 'react'; | ||
import { useQueries } from 'react-query'; | ||
import { useOkapiKy } from '@folio/stripes/core'; | ||
import { generateKiwtQueryParams } from '@k-int/stripes-kint-components'; | ||
|
||
const defaultParams = { page: 1 }; | ||
|
||
const useFetchMultiplePages = ({ | ||
getQueryKey = ({ params, pathStr, pageNum }) => [pathStr, pageNum, params], | ||
params = {}, // Accepts an object of the shape expected by the first parameter of generateKiwtQueryParams | ||
path = '', | ||
// At some point in the future I'd like this to be able to accept a list of pages, and run those all in parallel (possibly with some chunking logic) | ||
} = {}) => { | ||
const ky = useOkapiKy(); | ||
const queryArray = []; | ||
|
||
// Apply defaultParams ensuring that 'page' is defaulted properly | ||
const effectiveParams = useMemo(() => ({ ...defaultParams, ...params }), [params]); | ||
|
||
const currentPageParams = useMemo(() => ( | ||
generateKiwtQueryParams( | ||
{ ...effectiveParams }, | ||
{} | ||
) | ||
), [effectiveParams]); | ||
queryArray.push({ | ||
queryKey: getQueryKey({ | ||
params: currentPageParams, | ||
pageNum: effectiveParams.page, | ||
pathStr: path | ||
}), | ||
queryFn: () => ky.get(`${path}?${currentPageParams?.join('&')}`).json(), | ||
}); | ||
|
||
const nextPageParams = useMemo(() => ( | ||
generateKiwtQueryParams( | ||
{ ...effectiveParams, page: effectiveParams.page + 1 }, | ||
{} | ||
) | ||
), [effectiveParams]); | ||
queryArray.push({ | ||
queryKey: getQueryKey({ | ||
params: nextPageParams, | ||
pageNum: effectiveParams.page + 1, | ||
pathStr: path | ||
}), | ||
queryFn: () => ky.get(`${path}?${nextPageParams?.join('&')}`).json(), | ||
}); | ||
|
||
const queries = useQueries(queryArray); | ||
|
||
return ({ | ||
[effectiveParams.page]: queries[0], | ||
[effectiveParams.page + 1]: queries[1], // For now this is just "current" and "next", but longer term this could be expanded to n pages from a list for prefetching etc | ||
}); | ||
}; | ||
|
||
export default useFetchMultiplePages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters