Skip to content

Commit

Permalink
[TM-1603] add condition for site report species table (#817)
Browse files Browse the repository at this point in the history
  • Loading branch information
cesarLima1 authored Jan 21, 2025
1 parent 327f6bc commit 3a8afca
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
58 changes: 44 additions & 14 deletions src/admin/components/ResourceTabs/InformationTab/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Card, Grid, Stack, Typography } from "@mui/material";
import { useT } from "@transifex/react";
import classNames from "classnames";
import { FC } from "react";
import { FC, useState } from "react";
import { TabbedShowLayout, TabProps, useShowContext } from "react-admin";
import { Else, If, Then, When } from "react-if";

Expand Down Expand Up @@ -52,6 +52,11 @@ const InformationTab: FC<IProps> = props => {
const { isLoading: ctxLoading, record, resource } = useShowContext();
const t = useT();
const { framework } = useFrameworkContext();
const [totalCountNonTree, setTotalCountNonTree] = useState(0);
const [totalCountNurserySeedling, setTotalCountNurserySeedling] = useState(0);
const [totalCountSeeding, setTotalCountSeeding] = useState(0);
const [totalCountTreePlanted, setTotalCountTreePlanted] = useState(0);
const [totalCountReplanting, setTotalCountReplanting] = useState(0);
const modelName = resource?.replace("Report", "-report");
const modelUUID = record?.uuid;

Expand Down Expand Up @@ -145,13 +150,19 @@ const InformationTab: FC<IProps> = props => {
<div className="flex flex-col gap-11">
<ContextCondition frameworksHide={[Framework.PPC]}>
<div className="flex flex-col gap-4">
<Text variant="text-16-bold" className="capitalize">
Non-Trees Planted
</Text>
<div className="flex items-center gap-1 py-8">
<Text variant="text-16-bold" className="capitalize">
Non-Trees Planted:
</Text>
<Text variant="text-18-semibold" className="capitalize text-primary" as="span">
{totalCountNonTree.toLocaleString() ?? 0}
</Text>
</div>
<TreeSpeciesTablePD
modelUUID={modelUUID}
modelName={modelName}
collection="non-tree"
setTotalCount={setTotalCountNonTree}
framework={record?.framework_key}
secondColumnWidth="45%"
/>
Expand All @@ -160,13 +171,19 @@ const InformationTab: FC<IProps> = props => {
<When condition={props.type === "projects" || props.type === "project-reports"}>
<ContextCondition frameworksShow={[Framework.PPC]}>
<div className="flex flex-col gap-4">
<Text variant="text-16-bold" className="capitalize">
Saplings Grown in Nurseries:
</Text>
<div className="flex items-center gap-1 py-8">
<Text variant="text-16-bold" className="capitalize">
Saplings Grown in Nurseries:
</Text>
<Text variant="text-18-semibold" className="capitalize text-primary" as="span">
{totalCountNurserySeedling.toLocaleString() ?? 0}
</Text>
</div>
<TreeSpeciesTablePD
modelUUID={modelUUID}
modelName={modelName}
collection="nursery-seedling"
setTotalCount={setTotalCountNurserySeedling}
framework={record?.framework_key}
secondColumnWidth="45%"
/>
Expand All @@ -180,40 +197,53 @@ const InformationTab: FC<IProps> = props => {
Seeds Planted:
</Text>
<Text variant="text-18-semibold" className="capitalize text-primary" as="span">
{totalSeedlings ?? 0}
{(totalSeedlings ?? totalCountSeeding).toLocaleString()}
</Text>
</div>
<TreeSpeciesTablePD
modelUUID={modelUUID}
modelName={modelName}
collection="seeding"
setTotalCount={setTotalCountSeeding}
framework={record?.framework_key}
secondColumnWidth="45%"
/>
</div>
</ContextCondition>
<div className="flex flex-col gap-4">
<Text variant="text-16-bold" className="capitalize">
Trees Planted:
</Text>
<div className="flex items-center gap-1 py-8">
<Text variant="text-16-bold" className="capitalize">
Trees Planted:
</Text>
<Text variant="text-18-semibold" className="capitalize text-primary" as="span">
{totalCountTreePlanted.toLocaleString() ?? 0}
</Text>
</div>
<TreeSpeciesTablePD
modelUUID={modelUUID}
modelName={modelName}
collection="tree-planted"
setTotalCount={setTotalCountTreePlanted}
framework={record?.framework_key}
secondColumnWidth="45%"
/>
</div>
<When condition={props.type === "site-reports" || props.type === "project-reports"}>
<ContextCondition frameworksShow={[Framework.TF, Framework.ENTERPRISES]}>
<div className="flex flex-col gap-4">
<Text variant="text-16-bold" className="capitalize">
Replanting:
</Text>
<div className="flex items-center gap-1 py-8">
<Text variant="text-16-bold" className="capitalize">
Replanting:
</Text>
<Text variant="text-18-semibold" className="capitalize text-primary" as="span">
{totalCountReplanting.toLocaleString() ?? 0}
</Text>
</div>
<TreeSpeciesTablePD
modelUUID={modelUUID}
modelName={modelName}
collection="replanting"
setTotalCount={setTotalCountReplanting}
framework={record?.framework_key}
secondColumnWidth="45%"
/>
Expand Down
12 changes: 9 additions & 3 deletions src/components/extensive/Tables/TreeSpeciesTablePD.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface TreeSpeciesTablePDProps {
modelUUID: string;
modelName: string;
framework?: string;
setTotalCount?: React.Dispatch<React.SetStateAction<number>>;
headerName?: string;
collection?: string;
secondColumnWidth?: string;
Expand All @@ -35,6 +36,7 @@ const TreeSpeciesTablePD = ({
modelUUID,
modelName,
framework,
setTotalCount,
collection,
headerName = "species Name",
secondColumnWidth = ""
Expand Down Expand Up @@ -91,7 +93,13 @@ const TreeSpeciesTablePD = ({

const processTableData = (rows: any[]) => {
if (!rows) return [];

if (setTotalCount) {
const total = rows.reduce(
(sum, row) => sum + ((modelName === "site-report" ? row.amount : row.report_amount) || 0),
0
);
setTotalCount(total);
}
return rows.map(row => {
let speciesType = "tree";
if (!row.taxon_id) {
Expand All @@ -118,8 +126,6 @@ const TreeSpeciesTablePD = ({

const tableData = apiResponse?.data ? processTableData(apiResponse.data) : [];

console.log("rows tableData", tableData);

const rowSpeciesName = {
accessorKey: "name",
header: headerName,
Expand Down

0 comments on commit 3a8afca

Please sign in to comment.