diff --git a/src/generated/apiComponents.ts b/src/generated/apiComponents.ts index a4e07fa0b..de7f64f19 100644 --- a/src/generated/apiComponents.ts +++ b/src/generated/apiComponents.ts @@ -32675,6 +32675,112 @@ export const useGetV2TerrafundValidationCriteriaData = ; + +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; + }[]; + } + | { + /** + * 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 diff --git a/src/generated/apiSchemas.ts b/src/generated/apiSchemas.ts index c68f2fe35..63f086d25 100644 --- a/src/generated/apiSchemas.ts +++ b/src/generated/apiSchemas.ts @@ -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; + }[]; + } + | { + /** + * Error message if the polygon or criteria data is not found + */ + error: string; + }; +}; + export type V2TerrafundCriteriaSite = { /** * The UUID of the polygon. diff --git a/src/hooks/paginated/useLoadCriteriaSite.ts b/src/hooks/paginated/useLoadCriteriaSite.ts index e8f437cd6..be82cce6f 100644 --- a/src/hooks/paginated/useLoadCriteriaSite.ts +++ b/src/hooks/paginated/useLoadCriteriaSite.ts @@ -3,7 +3,7 @@ import { useEffect, useState } from "react"; import { fetchGetV2EntityPolygons, fetchGetV2EntityPolygonsCount, - fetchGetV2TerrafundValidationCriteriaData + fetchPostV2TerrafundValidationCriteriaData } from "@/generated/apiComponents"; interface LoadCriteriaSiteHook { @@ -13,7 +13,7 @@ interface LoadCriteriaSiteHook { progress: number; polygonCriteriaMap: Record; refetch: () => void; - fetchCriteriaData: (uuid: string) => void; + fetchCriteriaData: (uuids: string[]) => void; loadCriteriaInOrder: () => void; } @@ -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!) { @@ -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; }); };