From 30962b20cfb4c5b11ae0f65ae9606ae55f104cd4 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Fri, 28 Jul 2023 17:13:26 +0100 Subject: [PATCH] split out uvalue columns and recommendations modal --- .../building-passport/RecommendationCard.tsx | 199 +----------------- 1 file changed, 2 insertions(+), 197 deletions(-) diff --git a/src/app/components/building-passport/RecommendationCard.tsx b/src/app/components/building-passport/RecommendationCard.tsx index 9fd0468..c563624 100644 --- a/src/app/components/building-passport/RecommendationCard.tsx +++ b/src/app/components/building-passport/RecommendationCard.tsx @@ -1,204 +1,9 @@ "use client"; import { ComponentRecommendation } from "@/app/db/schema/recommendations"; -import { Dispatch, Fragment, SetStateAction, useState } from "react"; +import { Dispatch, SetStateAction, useState } from "react"; import { formatNumber } from "@/app/utils"; -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 { RecommendationMetricMap } from "@/types/recommendations"; -import { sumRecommendationMetricMap } from "@/app/portfolio/[slug]/building-passport/[propertyId]/recommendations/utils"; - -export const uvalueColumns: ColumnDef[] = [ - { - accessorKey: "description", - header: "Description", - }, - { - accessorKey: "estimatedCost", - header: "Estimated Cost", - cell: ({ row }) => { - return
£{formatNumber(row.getValue("estimatedCost"))}
; - }, - }, - { - accessorKey: "newUValue", - header: "New U-Value", - }, - { - accessorKey: "default", - id: "default", - header: "Selected?", - cell: ({ row }) => ( - { - if (value === true && !row.getIsSelected()) { - row.toggleSelected(true); - } - }} - aria-label="Select row" - /> - ), - enableSorting: false, - enableHiding: false, - }, -]; - -interface RecommendationModalProps { - title: string; - isOpen: boolean; - setIsOpen: (isOpen: boolean) => void; - recommendationData: ComponentRecommendation[]; - setCardComponent: Dispatch>; - setCostMap: Dispatch>; - costMap: RecommendationMetricMap; - setTotalEstimatedCost: Dispatch>; - sapMap: RecommendationMetricMap; - setSapMap: Dispatch>; - setTotalSapPoints: Dispatch>; -} - -export function RecommendationModal({ - title, - isOpen = false, - setIsOpen, - recommendationData, - setCardComponent, - setCostMap, - costMap, - setTotalEstimatedCost, - sapMap, - setSapMap, - setTotalSapPoints, -}: RecommendationModalProps) { - const [saveButtonDisabled, setSaveButtonDisabled] = useState(true); - - // Find the row where default is true - const [defaultRowIndex, setDefaultRowIndex] = useState( - recommendationData.findIndex((d) => d.default) - ); - - // Initialise the state with the default row index - const [rowSelection, setRowSelection] = useState( - defaultRowIndex !== -1 ? { [defaultRowIndex]: true } : {} - ); - - function closeModal() { - setIsOpen(false); - // If the user closes the modal, re-set the state of the row selection to the default, since nothing has changed - setRowSelection({ [defaultRowIndex]: true }); - } - - function saveChanges() { - // disable the button to prevent multiple clicks - // TODO: Add a loading state to show we're saving - setSaveButtonDisabled(true); - setIsOpen(false); - // Update the card component data - const newIndex = parseInt(Object.keys(rowSelection)[0]); - - setCardComponent(recommendationData[newIndex]); - // Set the default index - setDefaultRowIndex(newIndex); - // Update the cost map - const newCostMap = { - ...costMap, - [title]: recommendationData[newIndex].estimatedCost, - }; - setCostMap(newCostMap); - // update the cost sum - 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 ( - <> - - - -
- - -
-
- - - - {title} - - -
- - - - -
-
-
-
-
-
-
- - ); -} +import RecommendationModal from "./RecommendationModal"; const selectionStyling = "shadow active:shadow active:bg-brandmidblue w-full border rounded p-4 cursor-pointer text-gray-900 bg-gray-100 hover:bg-hoverblue hover:text-gray-100 transition-colors rounded-md flex flex-col justify-start";