Skip to content

Commit

Permalink
use new endpoint to avoid multiples request for validation data (#787)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarLima1 authored Dec 31, 2024
1 parent b918ed7 commit 3381ab5
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 10 deletions.
106 changes: 106 additions & 0 deletions src/generated/apiComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32675,6 +32675,112 @@ export const useGetV2TerrafundValidationCriteriaData = <TData = GetV2TerrafundVa
);
};

export type PostV2TerrafundValidationCriteriaDataError = Fetcher.ErrorWrapper<
| {
status: 400;
payload: {
/**
* @example Invalid or missing UUIDs array
*/
error?: string;
};
}
| {
status: 404;
payload: {
/**
* @example Criteria data not found for the given polygon IDs
*/
error?: string;
};
}
>;

export type PostV2TerrafundValidationCriteriaDataResponse = {
[key: string]:
| {
/**
* The ID of the polygon
*/
polygon_id: string;
/**
* List of validation criteria
*/
criteria_list: {
/**
* The ID of the criteria
*/
criteria_id?: number;
/**
* The latest created at timestamp of the criteria
*
* @format date-time
*/
latest_created_at?: string;
/**
* Indicates if the criteria is valid or not (1 for valid, 0 for invalid)
*/
valid?: number;
/**
* Extra information about the polygon validation
*/
extra_info?: Record<string, any>;
}[];
}
| {
/**
* Error message if the polygon or criteria data is not found
*/
error: string;
};
};

export type PostV2TerrafundValidationCriteriaDataRequestBody = {
/**
* A list of UUIDs of the polygons
*/
uuids?: string[];
};

export type PostV2TerrafundValidationCriteriaDataVariables = {
body?: PostV2TerrafundValidationCriteriaDataRequestBody;
} & ApiContext["fetcherOptions"];

export const fetchPostV2TerrafundValidationCriteriaData = (
variables: PostV2TerrafundValidationCriteriaDataVariables,
signal?: AbortSignal
) =>
apiFetch<
PostV2TerrafundValidationCriteriaDataResponse,
PostV2TerrafundValidationCriteriaDataError,
PostV2TerrafundValidationCriteriaDataRequestBody,
{},
{},
{}
>({ url: "/v2/terrafund/validation/criteria-data", method: "post", ...variables, signal });

export const usePostV2TerrafundValidationCriteriaData = (
options?: Omit<
reactQuery.UseMutationOptions<
PostV2TerrafundValidationCriteriaDataResponse,
PostV2TerrafundValidationCriteriaDataError,
PostV2TerrafundValidationCriteriaDataVariables
>,
"mutationFn"
>
) => {
const { fetcherOptions } = useApiContext();
return reactQuery.useMutation<
PostV2TerrafundValidationCriteriaDataResponse,
PostV2TerrafundValidationCriteriaDataError,
PostV2TerrafundValidationCriteriaDataVariables
>(
(variables: PostV2TerrafundValidationCriteriaDataVariables) =>
fetchPostV2TerrafundValidationCriteriaData({ ...fetcherOptions, ...variables }),
options
);
};

export type PostV2TerrafundValidationSitePolygonsQueryParams = {
/**
* The UUID of the polygon
Expand Down
39 changes: 39 additions & 0 deletions src/generated/apiSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22441,6 +22441,45 @@ export type V2TerrafundCriteriaData = {
}[];
};

export type V2TerrafundCriteriaDataMultiple = {
[key: string]:
| {
/**
* The ID of the polygon
*/
polygon_id: string;
/**
* List of validation criteria
*/
criteria_list: {
/**
* The ID of the criteria
*/
criteria_id?: number;
/**
* The latest created at timestamp of the criteria
*
* @format date-time
*/
latest_created_at?: string;
/**
* Indicates if the criteria is valid or not (1 for valid, 0 for invalid)
*/
valid?: number;
/**
* Extra information about the polygon validation
*/
extra_info?: Record<string, any>;
}[];
}
| {
/**
* Error message if the polygon or criteria data is not found
*/
error: string;
};
};

export type V2TerrafundCriteriaSite = {
/**
* The UUID of the polygon.
Expand Down
23 changes: 13 additions & 10 deletions src/hooks/paginated/useLoadCriteriaSite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useEffect, useState } from "react";
import {
fetchGetV2EntityPolygons,
fetchGetV2EntityPolygonsCount,
fetchGetV2TerrafundValidationCriteriaData
fetchPostV2TerrafundValidationCriteriaData
} from "@/generated/apiComponents";

interface LoadCriteriaSiteHook {
Expand All @@ -13,7 +13,7 @@ interface LoadCriteriaSiteHook {
progress: number;
polygonCriteriaMap: Record<string, any>;
refetch: () => void;
fetchCriteriaData: (uuid: string) => void;
fetchCriteriaData: (uuids: string[]) => void;
loadCriteriaInOrder: () => void;
}

Expand Down Expand Up @@ -61,9 +61,8 @@ const useLoadCriteriaSite = (
const partialResponse = (await fetchGetV2EntityPolygons({
queryParams
})) as any;
for (const polygon of partialResponse) {
await fetchCriteriaData(polygon.poly_id);
}
const polyUuids = partialResponse.map((polygon: any) => polygon.poly_id);
await fetchCriteriaData(polyUuids);
result = result.concat(partialResponse);
setData(result as any);
if (offset + limit > count!) {
Expand All @@ -84,15 +83,19 @@ const useLoadCriteriaSite = (
loadInBatches();
};

const fetchCriteriaData = async (uuid: string) => {
const criteriaData = await fetchGetV2TerrafundValidationCriteriaData({
queryParams: {
uuid: uuid
const fetchCriteriaData = async (polyUuids: string[]) => {
const criteriaData = await fetchPostV2TerrafundValidationCriteriaData({
body: {
uuids: polyUuids
}
});
setPolygonCriteriaMap(prev => {
const newMap = { ...prev };
newMap[uuid] = criteriaData;
polyUuids.forEach(uuid => {
if (criteriaData[uuid]) {
newMap[uuid] = criteriaData[uuid];
}
});
return newMap;
});
};
Expand Down

0 comments on commit 3381ab5

Please sign in to comment.