From 366cf90f77a33ccd3c283bcf4c452630622b813d Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Wed, 18 Dec 2024 11:37:33 -0800 Subject: [PATCH 1/4] [TM-1581] Make sure to use the collection defined in the form props. --- .../TreeSpeciesInput/TreeSpeciesInput.tsx | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx b/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx index 2cfea89fb..ef7753805 100644 --- a/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx +++ b/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx @@ -43,7 +43,13 @@ export interface TreeSpeciesInputProps extends Omit error?: FieldErrors[]; } -export type TreeSpeciesValue = { uuid?: string; name?: string; taxon_id?: string; amount?: number }; +export type TreeSpeciesValue = { + uuid?: string; + name?: string; + collection?: string; + taxon_id?: string; + amount?: number; +}; const TreeSpeciesInput = (props: TreeSpeciesInputProps) => { const id = useId(); @@ -82,7 +88,8 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => { uuid: uuidv4(), name, taxon_id: previousCount.taxonId, - amount: 0 + amount: 0, + collection: props.collection })) ); } @@ -100,10 +107,10 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => { const handleCreate = useDebounce( useCallback( (treeValue: TreeSpeciesValue) => { - onChange([...value, { ...treeValue, collection }]); + onChange([...value, { ...treeValue }]); clearErrors(); }, - [onChange, value, collection, clearErrors] + [onChange, value, clearErrors] ) ); @@ -139,7 +146,8 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => { uuid: uuidv4(), name: valueAutoComplete, taxon_id: props.useTaxonomicBackbone ? taxonId : undefined, - amount: props.withNumbers ? 0 : undefined + amount: props.withNumbers ? 0 : undefined, + collection }); setValueAutoComplete(""); From 46a08646d185ecf43b681578a3c765fc27a39818 Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Wed, 18 Dec 2024 14:57:21 -0800 Subject: [PATCH 2/4] [TM-1581] formHook.watch() appears to be more reliable for getting the current value. --- .../Inputs/TreeSpeciesInput/RHFSeedingTableInput.tsx | 10 ++++++---- .../Inputs/TreeSpeciesInput/RHFTreeSpeciesInput.tsx | 10 ++++++---- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/components/elements/Inputs/TreeSpeciesInput/RHFSeedingTableInput.tsx b/src/components/elements/Inputs/TreeSpeciesInput/RHFSeedingTableInput.tsx index 447eadfbe..5dd44724d 100644 --- a/src/components/elements/Inputs/TreeSpeciesInput/RHFSeedingTableInput.tsx +++ b/src/components/elements/Inputs/TreeSpeciesInput/RHFSeedingTableInput.tsx @@ -9,7 +9,7 @@ export interface RHFSeedingTableInputProps UseControllerProps { collection?: string; onChangeCapture?: () => void; - formHook?: UseFormReturn; + formHook: UseFormReturn; } /** @@ -19,12 +19,14 @@ export interface RHFSeedingTableInputProps const RHFSeedingTableInput = (props: PropsWithChildren) => { const t = useT(); const { - field: { value, onChange } + field: { onChange } } = useController(props); const { formHook, collection } = props; + const value = formHook.watch(props.name); + const clearErrors = useCallback(() => { - formHook?.clearErrors(props.name); + formHook.clearErrors(props.name); }, [formHook, props.name]); return ( @@ -39,7 +41,7 @@ const RHFSeedingTableInput = (props: PropsWithChildren - props.formHook?.setError(props.name, { message: t("One or more values are missing"), type: "required" }) + props.formHook.setError(props.name, { message: t("One or more values are missing"), type: "required" }) } /> ); diff --git a/src/components/elements/Inputs/TreeSpeciesInput/RHFTreeSpeciesInput.tsx b/src/components/elements/Inputs/TreeSpeciesInput/RHFTreeSpeciesInput.tsx index 3bec5353a..eb4c9ffee 100644 --- a/src/components/elements/Inputs/TreeSpeciesInput/RHFTreeSpeciesInput.tsx +++ b/src/components/elements/Inputs/TreeSpeciesInput/RHFTreeSpeciesInput.tsx @@ -7,7 +7,7 @@ import TreeSpeciesInput, { TreeSpeciesInputProps } from "./TreeSpeciesInput"; export interface RHFTreeSpeciesInputProps extends Omit, UseControllerProps { - formHook?: UseFormReturn; + formHook: UseFormReturn; } /** @@ -17,12 +17,14 @@ export interface RHFTreeSpeciesInputProps const RHFTreeSpeciesInput = (props: PropsWithChildren) => { const t = useT(); const { - field: { value, onChange } + field: { onChange } } = useController(props); const { formHook, collection } = props; + const value = formHook.watch(props.name); + const clearErrors = useCallback(() => { - formHook?.clearErrors(props.name); + formHook.clearErrors(props.name); }, [formHook, props.name]); return ( @@ -37,7 +39,7 @@ const RHFTreeSpeciesInput = (props: PropsWithChildren) collection={collection} clearErrors={clearErrors} onError={() => - props.formHook?.setError(props.name, { message: t("One or more values are missing"), type: "required" }) + props.formHook.setError(props.name, { message: t("One or more values are missing"), type: "required" }) } /> ); From d2c024218f5626bc040f4176b97240dad67bae1f Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Thu, 19 Dec 2024 12:01:51 -0800 Subject: [PATCH 3/4] [TM-1581] Use the input collection to filter which establishment and previous planting counts to use. --- .../TreeSpeciesInput/TreeSpeciesInput.tsx | 6 +++- src/connections/EstablishmentTrees.ts | 29 +++++++++++-------- .../v3/entityService/entityServiceSchemas.ts | 16 ++++++---- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx b/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx index ef7753805..756081fc1 100644 --- a/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx +++ b/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx @@ -79,7 +79,11 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => { const entity = (handleBaseEntityTrees ? entityName : undefined) as EstablishmentEntityType; const uuid = handleBaseEntityTrees ? entityUuid : undefined; - const [establishmentLoaded, { establishmentTrees, previousPlantingCounts }] = useEstablishmentTrees({ entity, uuid }); + const [establishmentLoaded, { establishmentTrees, previousPlantingCounts }] = useEstablishmentTrees({ + entity, + uuid, + collection + }); const shouldPrepopulate = value.length == 0 && Object.values(previousPlantingCounts ?? {}).length > 0; useValueChanged(shouldPrepopulate, function () { if (shouldPrepopulate) { diff --git a/src/connections/EstablishmentTrees.ts b/src/connections/EstablishmentTrees.ts index ca30a376d..7cd0ecb73 100644 --- a/src/connections/EstablishmentTrees.ts +++ b/src/connections/EstablishmentTrees.ts @@ -12,13 +12,15 @@ import { connectionHook } from "@/utils/connectionShortcuts"; import { selectorCache } from "@/utils/selectorCache"; type EstablishmentTreesConnection = { - establishmentTrees?: EstablishmentsTreesDto["establishmentTrees"]; - previousPlantingCounts?: EstablishmentsTreesDto["previousPlantingCounts"]; + establishmentTrees?: EstablishmentsTreesDto["establishmentTrees"][string]; + previousPlantingCounts?: Exclude[string]; establishmentTreesLoadFailed: boolean; }; -type EstablishmentTreesProps = Partial; +type EstablishmentTreesProps = Partial & { + collection?: string; +}; export type EstablishmentEntityType = EstablishmentTreesFindPathParams["entity"] | undefined; const establishmentTreesSelector = @@ -32,8 +34,8 @@ const establishmentTreesLoadFailed = const connectionIsLoaded = ( { establishmentTrees, establishmentTreesLoadFailed }: EstablishmentTreesConnection, - { entity, uuid }: EstablishmentTreesProps -) => entity == null || uuid == null || establishmentTrees != null || establishmentTreesLoadFailed; + { entity, uuid, collection }: EstablishmentTreesProps +) => collection == null || entity == null || uuid == null || establishmentTrees != null || establishmentTreesLoadFailed; const establishmentTreesConnection: Connection = { load: (connection, props) => { @@ -45,15 +47,18 @@ const establishmentTreesConnection: Connection `${entity}|${uuid}`, - ({ entity, uuid }) => + ({ entity, uuid, collection }) => `${entity}|${uuid}|${collection}`, + ({ entity, uuid, collection }) => createSelector( [establishmentTreesSelector(entity, uuid), establishmentTreesLoadFailed(entity, uuid)], - (treesDto, establishmentTreesLoadFailed) => ({ - establishmentTrees: treesDto?.attributes?.establishmentTrees, - previousPlantingCounts: treesDto?.attributes?.previousPlantingCounts, - establishmentTreesLoadFailed - }) + (treesDto, establishmentTreesLoadFailed) => { + const loadComplete = treesDto?.attributes?.establishmentTrees != null; + const establishmentTrees = + collection == null || !loadComplete ? undefined : treesDto.attributes.establishmentTrees[collection] ?? []; + const previousPlantingCounts = + collection == null || !loadComplete ? undefined : treesDto.attributes.previousPlantingCounts?.[collection]; + return { establishmentTrees, previousPlantingCounts, establishmentTreesLoadFailed }; + } ) ) }; diff --git a/src/generated/v3/entityService/entityServiceSchemas.ts b/src/generated/v3/entityService/entityServiceSchemas.ts index 9734a4f86..9b8edcaf1 100644 --- a/src/generated/v3/entityService/entityServiceSchemas.ts +++ b/src/generated/v3/entityService/entityServiceSchemas.ts @@ -25,15 +25,21 @@ export type ScientificNameDto = { export type EstablishmentsTreesDto = { /** - * The species that were specified at the establishment of the parent entity. + * The species that were specified at the establishment of the parent entity keyed by collection + * + * @example {"tree-planted":["Aster Peraliens","Circium carniolicum"],"non-tree":["Coffee"]} */ - establishmentTrees: string[]; + establishmentTrees: { + [key: string]: string[]; + }; /** - * If the entity in this request is a report, the sum totals of previous planting by species. + * If the entity in this request is a report, the sum totals of previous planting by species by collection. * - * @example {"Aster persaliens":{"amount":256},"Cirsium carniolicum":{"taxonId":"wfo-0000130112","amount":1024}} + * @example {"tree-planted":{"Aster persaliens":{"amount":256},"Cirsium carniolicum":{"taxonId":"wfo-0000130112","amount":1024}},"non-tree":{"Coffee":{"amount":2048}}} */ previousPlantingCounts: { - [key: string]: PreviousPlantingCountDto; + [key: string]: { + [key: string]: PreviousPlantingCountDto; + }; } | null; }; From 005d5f873d87159e29091724a8c5a96acbb9a05f Mon Sep 17 00:00:00 2001 From: Nathan Curtis Date: Thu, 19 Dec 2024 12:06:14 -0800 Subject: [PATCH 4/4] [TM-1581] [TM-1582] Update column title. --- .../elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx b/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx index 756081fc1..0339f7d28 100644 --- a/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx +++ b/src/components/elements/Inputs/TreeSpeciesInput/TreeSpeciesInput.tsx @@ -302,7 +302,7 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => {
- {isReport ? t("SPECIES PLANTED:") : t("TREES TO BE PLANTED:")} + {isReport ? t("TOTAL PLANTED THIS REPORT:") : t("TREES TO BE PLANTED:")} {props.withNumbers ? props.value.reduce((total, v) => total + (v.amount || 0), 0).toLocaleString() : "0"}