mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 12:37:20 +00:00
fix pagination if endpoint's total is not available
when using a connection that has softscans disabled (e.g. D1) pagination failed. Fixing it by overfetching and slicing
This commit is contained in:
@@ -35,7 +35,7 @@ export const useApiInfiniteQuery = <
|
||||
RefineFn extends (data: ResponseObject<Data>) => unknown = (data: ResponseObject<Data>) => Data,
|
||||
>(
|
||||
fn: (api: Api, page: number) => FetchPromise<Data>,
|
||||
options?: SWRConfiguration & { refine?: RefineFn },
|
||||
options?: SWRConfiguration & { refine?: RefineFn; pageSize?: number },
|
||||
) => {
|
||||
const [endReached, setEndReached] = useState(false);
|
||||
const api = useApi();
|
||||
@@ -47,14 +47,14 @@ export const useApiInfiniteQuery = <
|
||||
// @ts-ignore
|
||||
const swr = useSWRInfinite<RefinedData>(
|
||||
(index, previousPageData: any) => {
|
||||
if (previousPageData && !previousPageData.length) {
|
||||
if (index > 0 && previousPageData && previousPageData.length < (options?.pageSize ?? 0)) {
|
||||
setEndReached(true);
|
||||
return null; // reached the end
|
||||
}
|
||||
return promise(index).request.url;
|
||||
},
|
||||
(url: string) => {
|
||||
return new FetchPromise(new Request(url), { fetcher: api.fetcher }, refine).execute();
|
||||
return new FetchPromise(new Request(url), { fetcher: api.fetcher }).execute();
|
||||
},
|
||||
{
|
||||
revalidateFirstPage: false,
|
||||
|
||||
Reference in New Issue
Block a user