Skip to content

Commit

Permalink
adds some restoration constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
agnlez committed Dec 12, 2024
1 parent 17332e7 commit f28cc76
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 13 deletions.
51 changes: 45 additions & 6 deletions client/src/containers/projects/form/setup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import * as React from "react";

import { useFormContext } from "react-hook-form";

import { ACTIVITY } from "@shared/entities/activity.enum";
import {
ACTIVITY,
RESTORATION_ACTIVITY_SUBTYPE,
} from "@shared/entities/activity.enum";
import { EMISSION_FACTORS_TIER_TYPES } from "@shared/entities/carbon-inputs/emission-factors.entity";
import { SEQUESTRATION_RATE_TIER_TYPES } from "@shared/entities/carbon-inputs/sequestration-rate.entity";
import { CARBON_REVENUES_TO_COVER } from "@shared/entities/custom-project.entity";
import { ECOSYSTEM } from "@shared/entities/ecosystem.enum";
import { CreateCustomProjectSchema } from "@shared/schemas/custom-projects/create-custom-project.schema";
Expand Down Expand Up @@ -63,18 +67,37 @@ export default function SetupProjectForm() {
const form = useFormContext<CreateCustomProjectForm>();

const {
activity,
// @ts-expect-error fix later
parameters: { emissionFactorUsed },
parameters: { emissionFactorUsed, tierSelector, restorationActivity },
} = form.getValues();

const isDisabled = (ecosystem: ECOSYSTEM) => {
if (activity === ACTIVITY.CONSERVATION) {
return (
emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_2 &&
ecosystem !== ECOSYSTEM.MANGROVE
);
}

if (activity === ACTIVITY.RESTORATION) {
return (
tierSelector === SEQUESTRATION_RATE_TIER_TYPES.TIER_2 &&
[ECOSYSTEM.SEAGRASS, ECOSYSTEM.SALT_MARSH].includes(ecosystem)
);
}

return false;
};

const ECOSYSTEM_OPTIONS = Object.values(ECOSYSTEM).map((ecosystem) => ({
label: ecosystem,
value: ecosystem,
disabled:
emissionFactorUsed === EMISSION_FACTORS_TIER_TYPES.TIER_2 &&
ecosystem !== ECOSYSTEM.MANGROVE,
disabled: isDisabled(ecosystem),
}));

console.log(form.formState.errors);

return (
<Accordion type="single" collapsible defaultValue="assumptions">
<AccordionItem value="assumptions" className="border-b-0">
Expand Down Expand Up @@ -219,7 +242,23 @@ export default function SetupProjectForm() {
form.setValue("ecosystem", v as ECOSYSTEM);
await form.trigger("ecosystem");

form.resetField("parameters.emissionFactorUsed");
if (activity === ACTIVITY.CONSERVATION) {
form.resetField("parameters.emissionFactorUsed");
}

if (activity === ACTIVITY.RESTORATION) {
if (
[
RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY,
RESTORATION_ACTIVITY_SUBTYPE.HYBRID,
].includes(restorationActivity)
) {
form.setValue(
"parameters.restorationActivity",
RESTORATION_ACTIVITY_SUBTYPE.PLANTING,
);
}
}
}}
>
{ECOSYSTEM_OPTIONS.map((ecosystem) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
RESTORATION_ACTIVITY_SUBTYPE,
} from "@shared/entities/activity.enum";
import { SEQUESTRATION_RATE_TIER_TYPES } from "@shared/entities/carbon-inputs/sequestration-rate.entity";
import { ECOSYSTEM } from "@shared/entities/ecosystem.enum";

import { client } from "@/lib/query-client";
import { queryKeys } from "@/lib/query-keys";
Expand Down Expand Up @@ -59,6 +60,19 @@ export default function RestorationProjectDetails() {
},
);

const RESTORATION_ACTIVITY_OPTIONS = Object.values(
RESTORATION_ACTIVITY_SUBTYPE,
).map((activity) => ({
value: activity,
label: activity,
disabled:
ecosystem === ECOSYSTEM.SEAGRASS &&
[
RESTORATION_ACTIVITY_SUBTYPE.HYBRID,
RESTORATION_ACTIVITY_SUBTYPE.HYDROLOGY,
].includes(activity),
}));

return (
<Card variant="secondary">
<div className="flex flex-col gap-4">
Expand Down Expand Up @@ -93,13 +107,15 @@ export default function RestorationProjectDetails() {
<SelectValue placeholder="Select restoration activity type" />
</SelectTrigger>
<SelectContent>
{Object.values(RESTORATION_ACTIVITY_SUBTYPE)?.map(
(option) => (
<SelectItem key={option} value={option}>
{option}
</SelectItem>
),
)}
{RESTORATION_ACTIVITY_OPTIONS.map((option) => (
<SelectItem
key={option.value}
value={option.value}
disabled={option.disabled}
>
{option.label}
</SelectItem>
))}
</SelectContent>
</Select>
</FormControl>
Expand Down Expand Up @@ -132,6 +148,10 @@ export default function RestorationProjectDetails() {
v as SEQUESTRATION_RATE_TIER_TYPES,
);
await form.trigger("parameters.tierSelector");

if (v === SEQUESTRATION_RATE_TIER_TYPES.TIER_2) {
form.setValue("ecosystem", ECOSYSTEM.MANGROVE);
}
}}
>
<SelectTrigger>
Expand Down
8 changes: 8 additions & 0 deletions client/src/containers/projects/new/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export const onSubmit = (data: CreateCustomProjectForm) => {

data = {
...originalValues,
parameters: {
...originalValues.parameters,
// @ts-expect-error fix later
...(originalValues.parameters?.plantingSuccessRate && {
// @ts-expect-error fix later
plantingSuccessRate: plantingSuccessRate / 100,
}),
},
assumptions: {
...Object.keys(originalValues.assumptions ?? {}).reduce(
(acc, assumptionKey) => {
Expand Down

0 comments on commit f28cc76

Please sign in to comment.