Skip to content

Commit

Permalink
fix: APP-112 project and credit class pagination (#2356)
Browse files Browse the repository at this point in the history
  • Loading branch information
paul121 committed May 23, 2024
1 parent 7735616 commit cbe749f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
58 changes: 54 additions & 4 deletions web-marketplace/src/lib/ecocredit/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { QueryClientImpl as DataQueryClientImpl } from '@regen-network/api/lib/g
import {
BatchBalanceInfo,
BatchInfo,
ClassInfo,
DeepPartial,
ProjectInfo,
QueryBalanceRequest,
QueryBalanceResponse,
QueryBalancesRequest,
Expand Down Expand Up @@ -421,7 +423,19 @@ export const queryProjectsByClass = async ({
request,
}: QueryProjectsByClassProps): Promise<QueryProjectsByClassResponse> => {
try {
return client.ProjectsByClass({ classId: request.classId });
let projects: ProjectInfo[] = [];
let response: QueryProjectsByClassResponse | undefined;
while (!response || response.pagination?.nextKey?.length) {
if (response?.pagination?.nextKey?.length) {
request.pagination = { key: response.pagination.nextKey };
}
response = await client.ProjectsByClass(request);
projects.push(...response.projects);
}
return {
$type: response.$type,
projects,
};
} catch (err) {
throw new Error(`Error fetching projects by class: ${err}`);
}
Expand Down Expand Up @@ -772,7 +786,19 @@ export const queryClasses = async ({
request,
}: QueryClassesProps): Promise<QueryClassesResponse> => {
try {
return await client.Classes(request);
let classes: ClassInfo[] = [];
let response: QueryClassesResponse | undefined;
while (!response || response.pagination?.nextKey?.length) {
if (response?.pagination?.nextKey?.length) {
request.pagination = { key: response.pagination.nextKey };
}
response = await client.Classes(request);
classes.push(...response.classes);
}
return {
$type: response.$type,
classes,
};
} catch (err) {
throw new Error(
`Error in the Classes query of the ledger ecocredit module: ${err}`,
Expand Down Expand Up @@ -810,7 +836,19 @@ export const queryProjects = async ({
request,
}: QueryProjectsProps): Promise<QueryProjectsResponse> => {
try {
return await client.Projects(request);
let projects: ProjectInfo[] = [];
let response: QueryProjectsResponse | undefined;
while (!response || response.pagination?.nextKey?.length) {
if (response?.pagination?.nextKey?.length) {
request.pagination = { key: response.pagination.nextKey };
}
response = await client.Projects(request);
projects.push(...response.projects);
}
return {
$type: response.$type,
projects,
};
} catch (err) {
throw new Error(
`Error in the Projects query of the ledger ecocredit module: ${err}`,
Expand All @@ -829,7 +867,19 @@ export const queryProjectsByAdmin = async ({
request,
}: QueryProjectsByAdminProps): Promise<QueryProjectsByAdminResponse> => {
try {
return await client.ProjectsByAdmin(request);
let projects: ProjectInfo[] = [];
let response: QueryProjectsByAdminResponse | undefined;
while (!response || response.pagination?.nextKey?.length) {
if (response?.pagination?.nextKey?.length) {
request.pagination = { key: response.pagination.nextKey };
}
response = await client.ProjectsByAdmin(request);
projects.push(...response.projects);
}
return {
$type: response.$type,
projects,
};
} catch (err) {
throw new Error(
`Error in the ProjectsByAdmin query of the ledger ecocredit module: ${err}`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { queryClasses } from 'lib/ecocredit/api';
import { CLASSES_KEY } from './getClassesQuery.constants';
import {
ReactQueryClassesProps,
Expand All @@ -12,7 +13,7 @@ export const getClassesQuery = ({
queryKey: [CLASSES_KEY],
queryFn: async () => {
if (!client) return null;
return await client.Classes(request);
return await queryClasses({ client, request });
},
...params,
});

0 comments on commit cbe749f

Please sign in to comment.