Added reactivity for total sap points improvement

This commit is contained in:
Khalim Conn-Kowlessar 2023-07-28 17:08:11 +01:00
parent bc8e9f1ebe
commit cc6ea6c73c

View file

@ -6,8 +6,8 @@ import { Dialog, Transition } from "@headlessui/react";
import RecommendationTable from "@/app/components/building-passport/RecommendationTable";
import { ColumnDef } from "@tanstack/react-table";
import { Checkbox } from "@/app/shadcn_components/ui/checkbox";
import { CostMap } from "@/types/recommendations";
import { sumCostMap } from "@/app/portfolio/[slug]/building-passport/[propertyId]/recommendations/utils";
import { RecommendationMetricMap } from "@/types/recommendations";
import { sumRecommendationMetricMap } from "@/app/portfolio/[slug]/building-passport/[propertyId]/recommendations/utils";
export const uvalueColumns: ColumnDef<ComponentRecommendation>[] = [
{
@ -45,6 +45,20 @@ export const uvalueColumns: ColumnDef<ComponentRecommendation>[] = [
},
];
interface RecommendationModalProps {
title: string;
isOpen: boolean;
setIsOpen: (isOpen: boolean) => void;
recommendationData: ComponentRecommendation[];
setCardComponent: Dispatch<SetStateAction<ComponentRecommendation>>;
setCostMap: Dispatch<SetStateAction<RecommendationMetricMap>>;
costMap: RecommendationMetricMap;
setTotalEstimatedCost: Dispatch<SetStateAction<number>>;
sapMap: RecommendationMetricMap;
setSapMap: Dispatch<SetStateAction<RecommendationMetricMap>>;
setTotalSapPoints: Dispatch<SetStateAction<number>>;
}
export function RecommendationModal({
title,
isOpen = false,
@ -53,19 +67,11 @@ export function RecommendationModal({
setCardComponent,
setCostMap,
costMap,
totalEstimatedCost,
setTotalEstimatedCost,
}: {
title: string;
isOpen: boolean;
setIsOpen: (isOpen: boolean) => void;
recommendationData: ComponentRecommendation[];
setCardComponent: Dispatch<SetStateAction<ComponentRecommendation>>;
setCostMap: Dispatch<SetStateAction<CostMap>>;
costMap: CostMap;
totalEstimatedCost: number;
setTotalEstimatedCost: Dispatch<SetStateAction<number>>;
}) {
sapMap,
setSapMap,
setTotalSapPoints,
}: RecommendationModalProps) {
const [saveButtonDisabled, setSaveButtonDisabled] = useState(true);
// Find the row where default is true
@ -102,7 +108,17 @@ export function RecommendationModal({
};
setCostMap(newCostMap);
// update the cost sum
setTotalEstimatedCost(sumCostMap(newCostMap));
setTotalEstimatedCost(sumRecommendationMetricMap(newCostMap));
// Update the sap map
const newSapMap = {
...sapMap,
[title]: recommendationData[newIndex].sapPoints,
};
setSapMap(newSapMap);
// update the sap sum
console.log("setTotalSapPoints", setTotalSapPoints);
setTotalSapPoints(sumRecommendationMetricMap(newSapMap));
}
return (
@ -196,13 +212,19 @@ export default function RecommendationCard({
costMap,
totalEstimatedCost,
setTotalEstimatedCost,
sapMap,
setSapMap,
setTotalSapPoints,
}: {
componentType: string;
recommendationData: ComponentRecommendation[];
setCostMap: Dispatch<SetStateAction<CostMap>>;
costMap: CostMap;
setCostMap: Dispatch<SetStateAction<RecommendationMetricMap>>;
costMap: RecommendationMetricMap;
totalEstimatedCost: number;
setTotalEstimatedCost: Dispatch<SetStateAction<number>>;
sapMap: RecommendationMetricMap;
setSapMap: Dispatch<SetStateAction<RecommendationMetricMap>>;
setTotalSapPoints: Dispatch<SetStateAction<number>>;
}) {
const defaultComponent = recommendationData.find(
(rec: ComponentRecommendation) => rec.default
@ -268,8 +290,10 @@ export default function RecommendationCard({
setCardComponent={setCardComponent}
setCostMap={setCostMap}
costMap={costMap}
totalEstimatedCost={totalEstimatedCost}
setTotalEstimatedCost={setTotalEstimatedCost}
sapMap={sapMap}
setSapMap={setSapMap}
setTotalSapPoints={setTotalSapPoints}
/>
</div>
);