Skip to content

Commit

Permalink
Merge pull request #774 from wri/fix/TM-1581-tree-collections
Browse files Browse the repository at this point in the history
[TM-1581][TM-1582] Tree Species input bug fixes.
  • Loading branch information
roguenet authored Dec 19, 2024
2 parents afd0526 + 005d5f8 commit f73b765
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export interface RHFSeedingTableInputProps
UseControllerProps {
collection?: string;
onChangeCapture?: () => void;
formHook?: UseFormReturn;
formHook: UseFormReturn;
}

/**
Expand All @@ -19,12 +19,14 @@ export interface RHFSeedingTableInputProps
const RHFSeedingTableInput = (props: PropsWithChildren<RHFSeedingTableInputProps>) => {
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 (
Expand All @@ -39,7 +41,7 @@ const RHFSeedingTableInput = (props: PropsWithChildren<RHFSeedingTableInputProps
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" })
}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import TreeSpeciesInput, { TreeSpeciesInputProps } from "./TreeSpeciesInput";
export interface RHFTreeSpeciesInputProps
extends Omit<TreeSpeciesInputProps, "value" | "onChange" | "clearErrors">,
UseControllerProps {
formHook?: UseFormReturn;
formHook: UseFormReturn;
}

/**
Expand All @@ -17,12 +17,14 @@ export interface RHFTreeSpeciesInputProps
const RHFTreeSpeciesInput = (props: PropsWithChildren<RHFTreeSpeciesInputProps>) => {
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 (
Expand All @@ -37,7 +39,7 @@ const RHFTreeSpeciesInput = (props: PropsWithChildren<RHFTreeSpeciesInputProps>)
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" })
}
/>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ export interface TreeSpeciesInputProps extends Omit<InputWrapperProps, "error">
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();
Expand Down Expand Up @@ -74,7 +80,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) {
Expand All @@ -83,7 +93,8 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => {
uuid: uuidv4(),
name,
taxon_id: previousCount.taxonId,
amount: 0
amount: 0,
collection: props.collection
}))
);
}
Expand All @@ -101,10 +112,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]
)
);

Expand Down Expand Up @@ -140,7 +151,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("");
Expand Down Expand Up @@ -291,7 +303,7 @@ const TreeSpeciesInput = (props: TreeSpeciesInputProps) => {
</div>
<div className={classNames({ "border-r pr-6": displayPreviousCounts })} ref={refPlanted}>
<Text variant="text-14-bold" className="uppercase text-black">
{isReport ? t("SPECIES PLANTED:") : t("TREES TO BE PLANTED:")}
{isReport ? t("TOTAL PLANTED THIS REPORT:") : t("TREES TO BE PLANTED:")}
</Text>
<Text variant="text-20-bold" className="text-primary">
{props.withNumbers ? props.value.reduce((total, v) => total + (v.amount || 0), 0).toLocaleString() : "0"}
Expand Down
29 changes: 17 additions & 12 deletions src/connections/EstablishmentTrees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<EstablishmentsTreesDto["previousPlantingCounts"], null>[string];

establishmentTreesLoadFailed: boolean;
};

type EstablishmentTreesProps = Partial<EstablishmentTreesFindPathParams>;
type EstablishmentTreesProps = Partial<EstablishmentTreesFindPathParams> & {
collection?: string;
};

export type EstablishmentEntityType = EstablishmentTreesFindPathParams["entity"] | undefined;
const establishmentTreesSelector =
Expand All @@ -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<EstablishmentTreesConnection, EstablishmentTreesProps> = {
load: (connection, props) => {
Expand All @@ -45,15 +47,18 @@ const establishmentTreesConnection: Connection<EstablishmentTreesConnection, Est
isLoaded: connectionIsLoaded,

selector: selectorCache(
({ entity, uuid }) => `${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 };
}
)
)
};
Expand Down
16 changes: 11 additions & 5 deletions src/generated/v3/entityService/entityServiceSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

0 comments on commit f73b765

Please sign in to comment.