Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[N/A] custom-project creation: restoration #159

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion client/src/containers/projects/form/cell-value.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ComponentProps } from "react";

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

import { cn } from "@/lib/utils";

import { CreateCustomProjectForm } from "@/containers/projects/form/setup";

import {
Expand All @@ -12,8 +16,12 @@ import { Input } from "@/components/ui/input";

export default function CellValue({
name,
className,
hasUnit = false,
}: {
name: keyof CreateCustomProjectForm;
className?: ComponentProps<typeof FormControl>["className"];
hasUnit?: boolean;
}) {
const form = useFormContext<CreateCustomProjectForm>();

Expand All @@ -26,14 +34,17 @@ export default function CellValue({
const { value, ...restField } = field;

return (
<FormItem>
<FormItem className={className}>
<FormControl>
<Input
{...restField}
type="number"
placeholder="Insert value"
min={0}
defaultValue={field.value as number}
className={cn({
"pr-12": hasUnit,
})}
/>
</FormControl>
<FormMessage />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useFormContext } from "react-hook-form";

import { ACTIVITY } from "@shared/entities/activity.enum";
import { COSTS_DTO_TO_NAME_MAP } from "@shared/schemas/assumptions/assumptions.enums";
import {
flexRender,
Expand Down Expand Up @@ -34,12 +35,21 @@ const NO_DATA: DataColumnDef<CapexFormProperty>[] = [];
export default function CapexCostInputsTable() {
const form = useFormContext<CreateCustomProjectForm>();

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

const { queryKey } = queryKeys.customProjects.defaultCosts({
ecosystem,
countryCode,
activity,
restorationActivity,
});

const { data, isSuccess } =
Expand All @@ -50,6 +60,7 @@ export default function CapexCostInputsTable() {
ecosystem,
countryCode,
activity,
...(activity === ACTIVITY.RESTORATION && { restorationActivity }),
},
},
{
Expand All @@ -71,7 +82,11 @@ export default function CapexCostInputsTable() {
defaultValue: data.body.data[key as keyof typeof data.body.data],
value: "",
})),
enabled: !!ecosystem && !!countryCode && !!activity,
enabled:
!!ecosystem &&
!!countryCode &&
((!!activity && activity === ACTIVITY.CONSERVATION) ||
(activity === ACTIVITY.RESTORATION && !!restorationActivity)),
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useFormContext } from "react-hook-form";

import { ACTIVITY } from "@shared/entities/activity.enum";
import { COSTS_DTO_TO_NAME_MAP } from "@shared/schemas/assumptions/assumptions.enums";
import {
flexRender,
Expand Down Expand Up @@ -34,12 +35,21 @@ const NO_DATA: DataColumnDef<OpexFormProperty>[] = [];
export default function OpexCostInputsTable() {
const form = useFormContext<CreateCustomProjectForm>();

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

const { queryKey } = queryKeys.customProjects.defaultCosts({
ecosystem,
countryCode,
activity,
restorationActivity,
});
const { data, isSuccess } =
client.customProjects.getDefaultCostInputs.useQuery(
Expand All @@ -49,6 +59,7 @@ export default function OpexCostInputsTable() {
ecosystem,
countryCode,
activity,
...(activity === ACTIVITY.RESTORATION && { restorationActivity }),
},
},
{
Expand All @@ -70,7 +81,9 @@ export default function OpexCostInputsTable() {
defaultValue: data.body.data[key as keyof typeof data.body.data],
value: "",
})),
enabled: !!ecosystem && !!countryCode && !!activity,
enabled:
(!!activity && activity === ACTIVITY.CONSERVATION) ||
(activity === ACTIVITY.RESTORATION && !!restorationActivity),
},
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useFormContext } from "react-hook-form";

import { ACTIVITY } from "@shared/entities/activity.enum";
import { COSTS_DTO_TO_NAME_MAP } from "@shared/schemas/assumptions/assumptions.enums";
import {
flexRender,
Expand Down Expand Up @@ -34,12 +35,21 @@ const NO_DATA: DataColumnDef<OtherFormProperty>[] = [];
export default function OtherCostInputsTable() {
const form = useFormContext<CreateCustomProjectForm>();

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

const { queryKey } = queryKeys.customProjects.defaultCosts({
ecosystem,
countryCode,
activity,
restorationActivity,
});
const { data, isSuccess } =
client.customProjects.getDefaultCostInputs.useQuery(
Expand All @@ -49,6 +59,7 @@ export default function OtherCostInputsTable() {
ecosystem,
countryCode,
activity,
...(activity === ACTIVITY.RESTORATION && { restorationActivity }),
},
},
{
Expand All @@ -70,7 +81,9 @@ export default function OtherCostInputsTable() {
defaultValue: data.body.data[key as keyof typeof data.body.data],
value: "",
})),
enabled: !!ecosystem && !!countryCode && !!activity,
enabled:
(!!activity && activity === ACTIVITY.CONSERVATION) ||
(activity === ACTIVITY.RESTORATION && !!restorationActivity),
},
);

Expand Down
17 changes: 16 additions & 1 deletion client/src/containers/projects/form/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { useFormContext } from "react-hook-form";

import { ACTIVITY } from "@shared/entities/activity.enum";

import AssumptionsProjectForm from "@/containers/projects/form/assumptions";
import CostInputsOverridesProjectForm from "@/containers/projects/form/cost-inputs-overrides";
import SetupProjectForm from "@/containers/projects/form/setup";
import RestorationPlanProjectForm from "@/containers/projects/form/restoration-plan";
import SetupProjectForm, {
CreateCustomProjectForm,
} from "@/containers/projects/form/setup";

import { Card } from "@/components/ui/card";

export default function ProjectForm({ onSubmit }: { onSubmit: () => void }) {
const form = useFormContext<CreateCustomProjectForm>();
const { activity } = form.getValues();

return (
<form className="w-full space-y-8" onSubmit={onSubmit}>
<div className="flex flex-col gap-3">
Expand All @@ -17,6 +27,11 @@ export default function ProjectForm({ onSubmit }: { onSubmit: () => void }) {
<Card className="flex flex-1 flex-col" variant="secondary">
<CostInputsOverridesProjectForm />
</Card>
{activity === ACTIVITY.RESTORATION && (
<Card className="flex flex-1 flex-col" variant="secondary">
<RestorationPlanProjectForm />
</Card>
)}
</div>
</form>
);
Expand Down
35 changes: 35 additions & 0 deletions client/src/containers/projects/form/restoration-plan/columns.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createColumnHelper } from "@tanstack/react-table";

import CellValue from "@/containers/projects/form/cell-value";

import { Label } from "@/components/ui/label";

export type RestorationPlanFormProperty = {
year: number;
annualHectaresRestored: number;
};

const columnHelper = createColumnHelper<RestorationPlanFormProperty>();

export const COLUMNS = [
columnHelper.accessor("year", {
header: () => <span>Year</span>,
cell: (props) => {
return <Label>{props.getValue()}</Label>;
},
}),
columnHelper.accessor("annualHectaresRestored", {
header: () => (
<span className="flex justify-end">Annual hectares restored / year</span>
),
cell: (props) => (
<CellValue
className="relative after:absolute after:right-6 after:top-1/2 after:inline-block after:-translate-y-1/2 after:text-sm after:text-muted-foreground after:content-['ha']"
// @ts-expect-error fix later
name={`parameters.restorationYearlyBreakdown.${props.row.original.year === -1 ? 0 : props.row.original.year}`}
hasUnit
/>
),
maxSize: 55,
}),
];
Loading
Loading