-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
248 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
client/src/containers/projects/form/restoration-plan/columns.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { createColumnHelper } from "@tanstack/react-table"; | ||
|
||
import CellValue from "@/containers/projects/form/cell-value"; | ||
import { DataColumnDef } from "@/containers/projects/form/cost-inputs-overrides/constants"; | ||
import { CreateCustomProjectForm } from "@/containers/projects/form/setup"; | ||
|
||
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']" | ||
name={`parameters.restorationYearlyBreakdown.${props.row.original.year}`} | ||
hasUnit | ||
/> | ||
), | ||
// size: 75, | ||
maxSize: 55, | ||
// cell: (props) => { | ||
// return <Label>{props.getValue()}</Label>; | ||
// }, | ||
}), | ||
]; |
167 changes: 167 additions & 0 deletions
167
client/src/containers/projects/form/restoration-plan/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import { useMemo } from "react"; | ||
|
||
import { useFormContext } from "react-hook-form"; | ||
|
||
import { ASSUMPTIONS_NAME_TO_DTO_MAP } from "@shared/schemas/assumptions/assumptions.enums"; | ||
import { | ||
flexRender, | ||
getCoreRowModel, | ||
useReactTable, | ||
} from "@tanstack/react-table"; | ||
|
||
import { client } from "@/lib/query-client"; | ||
import { queryKeys } from "@/lib/query-keys"; | ||
|
||
import { AssumptionsFormProperty } from "@/containers/projects/form/assumptions/columns"; | ||
import { COLUMNS } from "@/containers/projects/form/restoration-plan/columns"; | ||
import { CreateCustomProjectForm } from "@/containers/projects/form/setup"; | ||
|
||
import { | ||
Accordion, | ||
AccordionContent, | ||
AccordionItem, | ||
AccordionTrigger, | ||
} from "@/components/ui/accordion"; | ||
import { | ||
Table, | ||
TableBody, | ||
TableCell, | ||
TableHead, | ||
TableHeader, | ||
TableRow, | ||
} from "@/components/ui/table"; | ||
|
||
export default function RestorationPlanProjectForm() { | ||
const form = useFormContext<CreateCustomProjectForm>(); | ||
|
||
const v = form.getValues(); | ||
console.log(v); | ||
|
||
const { | ||
ecosystem, | ||
activity, | ||
assumptions: { projectLength }, | ||
} = form.getValues(); | ||
|
||
const { queryKey } = queryKeys.customProjects.assumptions({ | ||
ecosystem, | ||
activity, | ||
}); | ||
const { data: defaultRestorationProjectLength } = | ||
client.customProjects.getDefaultAssumptions.useQuery( | ||
queryKey, | ||
{ | ||
query: { ecosystem, activity }, | ||
}, | ||
{ | ||
queryKey, | ||
select: (data) => | ||
Number( | ||
data.body.data.find( | ||
(assumption) => assumption.name === "Restoration project length", | ||
)?.value, | ||
), | ||
}, | ||
); | ||
|
||
const totalYears = projectLength | ||
? Number(projectLength) | ||
: defaultRestorationProjectLength; | ||
|
||
console.log({ totalYears }); | ||
|
||
const DATA = useMemo( | ||
() => | ||
Array.from({ | ||
length: (totalYears as NonNullable<typeof totalYears>) + 2, | ||
}) | ||
.map((_, i) => ({ | ||
year: i - 1, | ||
annualHectaresRestored: 0, | ||
})) | ||
.filter(({ year }) => year != 0), | ||
[totalYears], | ||
); | ||
|
||
const table = useReactTable({ | ||
data: DATA, | ||
columns: COLUMNS, | ||
getCoreRowModel: getCoreRowModel(), | ||
}); | ||
|
||
return ( | ||
<Accordion type="single" collapsible defaultValue="assumptions"> | ||
<AccordionItem value="assumptions" className="border-b-0"> | ||
<AccordionTrigger className="pt-0"> | ||
<div className="flex flex-col gap-3"> | ||
<div className="flex items-baseline gap-2"> | ||
<h2 className="text-2xl font-medium">Restoration Plan</h2> | ||
<span className="font-normal text-muted-foreground"> | ||
optional | ||
</span> | ||
</div> | ||
<p className="font-normal text-muted-foreground"> | ||
Overrides annual hectares inputs | ||
</p> | ||
</div> | ||
</AccordionTrigger> | ||
<AccordionContent className="pb-0"> | ||
<Table> | ||
<TableHeader> | ||
{table.getHeaderGroups().map((headerGroup) => ( | ||
<TableRow key={headerGroup.id} className="divide-x-0"> | ||
{headerGroup.headers.map((header) => { | ||
return ( | ||
<TableHead | ||
key={header.id} | ||
className="bg-transparent font-normal" | ||
style={{ | ||
width: header.column.getSize(), | ||
}} | ||
> | ||
{header.isPlaceholder | ||
? null | ||
: flexRender( | ||
header.column.columnDef.header, | ||
header.getContext(), | ||
)} | ||
</TableHead> | ||
); | ||
})} | ||
</TableRow> | ||
))} | ||
</TableHeader> | ||
<TableBody> | ||
{table.getRowModel().rows?.length ? ( | ||
table.getRowModel().rows.map((row) => ( | ||
<TableRow | ||
key={row.id} | ||
className="divide-x-0 divide-y-0 border-b-0" | ||
> | ||
{row.getVisibleCells().map((cell) => ( | ||
<TableCell key={cell.id} className="py-3"> | ||
{flexRender( | ||
cell.column.columnDef.cell, | ||
cell.getContext(), | ||
)} | ||
</TableCell> | ||
))} | ||
</TableRow> | ||
)) | ||
) : ( | ||
<TableRow> | ||
<TableCell | ||
colSpan={COLUMNS.length} | ||
className="h-24 text-center" | ||
> | ||
No results. | ||
</TableCell> | ||
</TableRow> | ||
)} | ||
</TableBody> | ||
</Table> | ||
</AccordionContent> | ||
</AccordionItem> | ||
</Accordion> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters