Skip to content

Commit

Permalink
Merge branch 'rithviknishad/feat/use-paginated-query' of https://gith…
Browse files Browse the repository at this point in the history
…ub.com/ohcnetwork/care_fe into treatment-summary-ui
  • Loading branch information
amjithtitus09 committed Feb 10, 2025
2 parents d0c0c04 + ebfcd79 commit 55d001e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Utils/request/query.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import careConfig from "@careConfig";

import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants";

import { getResponseBody } from "@/Utils/request/request";
import {
ApiCallOptions,
Expand Down Expand Up @@ -143,15 +145,25 @@ const paginatedQuery = <
Route extends ApiRoute<PaginatedResponse<unknown>, unknown>,
>(
route: Route,
options?: ApiCallOptions<Route> & { maxPages?: number },
options?: ApiCallOptions<Route> & { pageSize?: number; maxPages?: number },
) => {
return async ({ signal }: { signal: AbortSignal }) => {
const items: Route["TRes"]["results"] = [];
let hasNextPage = true;
let page = 0;

const pageSize = options?.pageSize ?? RESULTS_PER_PAGE_LIMIT;

while (hasNextPage) {
const res = await query(route, { ...options })({ signal });
const res = await query(route, {
...options,
queryParams: {
...options?.queryParams,
limit: pageSize,
offset: page * pageSize,
},
})({ signal });

items.push(...res.results);

if (options?.maxPages && page >= options.maxPages) {
Expand Down

0 comments on commit 55d001e

Please sign in to comment.