From 94e1cc59734a11dae6639144a272f093ad540348 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Wed, 31 May 2023 11:50:36 +0100 Subject: [PATCH] Added budget updates --- src/app/components/plan/BudgetModal.tsx | 94 +++++++++++++++++++ src/app/components/plan/PartModal.tsx | 3 +- .../[slug]/property/[lmkKey]/plan/page.tsx | 18 +++- src/app/utils.ts | 21 +++++ 4 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 src/app/components/plan/BudgetModal.tsx create mode 100644 src/app/utils.ts diff --git a/src/app/components/plan/BudgetModal.tsx b/src/app/components/plan/BudgetModal.tsx new file mode 100644 index 00000000..6f940e6a --- /dev/null +++ b/src/app/components/plan/BudgetModal.tsx @@ -0,0 +1,94 @@ +import { Dialog, Transition } from "@headlessui/react"; +import { Dispatch, Fragment, SetStateAction, useState } from "react"; +import { TanButton } from "../Buttons"; + +export default function BudgetModal({ + isOpen, + setIsOpen, + budget, + setBudget, +}: { + isOpen: boolean; + setIsOpen: Dispatch>; + budget: number | "Not set"; + setBudget: Dispatch>; +}) { + const [budgetInput, setBudgetInput] = useState(null); + + function handleSubmit() { + if (budgetInput !== null) { + setBudget(budgetInput); + } + setIsOpen(false); + } + + return ( + <> + + setIsOpen(false)} + > + +
+ + +
+
+ + + + {"Set a Budget"} + +
+ setBudgetInput(Number(e.target.value))} + onKeyDown={(e) => + (e.key === "e" || e.key === "E") && e.preventDefault() + } + /> +
+ +
+ + +
+
+
+
+
+
+
+ + ); +} diff --git a/src/app/components/plan/PartModal.tsx b/src/app/components/plan/PartModal.tsx index 1c6b79d6..955c6e10 100644 --- a/src/app/components/plan/PartModal.tsx +++ b/src/app/components/plan/PartModal.tsx @@ -19,7 +19,8 @@ export default function PartModal({ setSelectedOption: Dispatch>; }) { function handleModalSubmit() { - // setTargetEpcRating(modalEpcTarget); + // Right now the dropdown is setting the state on the selected option so we might not need + // to do anything here setIsOpen(false); } diff --git a/src/app/portfolio/[slug]/property/[lmkKey]/plan/page.tsx b/src/app/portfolio/[slug]/property/[lmkKey]/plan/page.tsx index ac161ad6..25fe8d63 100644 --- a/src/app/portfolio/[slug]/property/[lmkKey]/plan/page.tsx +++ b/src/app/portfolio/[slug]/property/[lmkKey]/plan/page.tsx @@ -9,6 +9,8 @@ import { PencilSquareIcon } from "@heroicons/react/24/outline"; import PlanPart from "@/app/components/plan/PlanPart"; import EditEpctargetModal from "@/app/components/property/EditEpcTargetModal"; import Link from "next/link"; +import BudgetModal from "@/app/components/plan/BudgetModal"; +import { formatNumber } from "@/app/utils"; export default function Plan({ params, @@ -62,10 +64,11 @@ export default function Plan({ const partsTotalCost = partsConfig.reduce((sum, part) => sum + part.cost, 0); - const [budget, setBudget] = useState("Not set"); + const [budget, setBudget] = useState("Not set"); const [totalCost, setTotalCost] = useState(partsTotalCost); const [installTime, setInstallTime] = useState(totalWorkHours); const [isEditModalOpen, setIsEditModalOpen] = useState(false); + const [isBudgetModalOpen, setIsBudgetModalOpen] = useState(false); const [targetEpcRating, setTargetEpcRating] = useState( (searchParams.targetEpcRating as EpcRating) ?? "C" ); @@ -122,10 +125,12 @@ export default function Plan({
  • console.log("clicked")} + onClick={() => setIsBudgetModalOpen(true)} className="px-2 mb-2 flex items-center cursor-pointer hover:bg-brandblue hover:rounded-md transition-colors duration-200" > - Budget: {budget} + {budget !== "Not set" + ? "Budget: £" + formatNumber(budget) + : "Budget: " + budget}
  • Total cost: £{totalCost}
  • -
  • Installation Time: {installTime} hours
  • @@ -153,6 +157,12 @@ export default function Plan({ targetEpcRating={targetEpcRating} currentEpcRating={propertyData["current-energy-rating"]} /> + ); } diff --git a/src/app/utils.ts b/src/app/utils.ts new file mode 100644 index 00000000..d97347d5 --- /dev/null +++ b/src/app/utils.ts @@ -0,0 +1,21 @@ +export function formatNumber(number: number): string { + const suffixes: string[] = ["", "k", "m", "b", "t"]; + const suffixIndex: number = Math.floor(Math.log10(Math.abs(number)) / 3); + + // Check if the suffix index is within the available suffixes + if (suffixIndex >= suffixes.length) { + return number.toString(); // Return the number as is + } + + // Check if the number is smaller and round to 2 decimal places + const roundedNumber: number = + Math.abs(number) < 1000 + ? Number(number.toFixed(2)) + : Number(number.toPrecision(4)); + + const formattedNumber: string = ( + roundedNumber / Math.pow(1000, suffixIndex) + ).toString(); + + return formattedNumber + suffixes[suffixIndex]; +}