-
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.
Merge pull request #682 from folio-org/release_9.1.2
Release 9.1.2
- Loading branch information
Showing
6 changed files
with
227 additions
and
158 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
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
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
Oops, something went wrong.