From 35d6a5641b58d0c33652d4226cc5bcc831c8f54d Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Thu, 30 Nov 2023 18:04:52 +0000 Subject: [PATCH] implemented property discrete typing to recommendation cards --- .../building-passport/RecommendationCard.tsx | 42 +++++++++---------- .../RecommendationContainer.tsx | 23 ++++++++-- src/app/db/schema/recommendations.ts | 3 +- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/src/app/components/building-passport/RecommendationCard.tsx b/src/app/components/building-passport/RecommendationCard.tsx index afe2dcf..f882912 100644 --- a/src/app/components/building-passport/RecommendationCard.tsx +++ b/src/app/components/building-passport/RecommendationCard.tsx @@ -14,16 +14,25 @@ const noSelectionStyling = "shadow active:shadow active:bg-brandmidblue w-full border rounded p-4 cursor-pointer text-gray-300 bg-white hover:bg-hoverblue hover:text-gray-100 transition-colors rounded-md flex flex-col justify-start"; const TitleMap = { - wall_insulation: "Wall Insulation", - floor_insulation: "Floor Insulation", - roof_insulation: "Roof Insulation", mechanical_ventilation: "Mechanical Ventilation", sealing_open_fireplace: "Sealing Open Fireplace", low_energy_lighting: "Low Energy Lighting", - iwi: "Internal Wall Insulation", - ewi: "External Wall Insulation", - cwi: "Cavity Wall Insulation", + // Walls + internal_wall_insulation: "Internal Wall Insulation", + external_wall_insulation: "External Wall Insulation", + cavity_wall_insulation: "Cavity Wall Insulation", + // Roof loft_insulation: "Loft Insulation", + room_roof_insulation: "Room Roof Insulation", + flat_roof_insulation: "Flat Roof Insulation", + // Floor + solid_floor_insulation: "Solid Floor Insulation", + suspended_floor_insulation: "Suspended Floor Insulation", + exposed_floor_insulation: "Exposed Floor Insulation", + // Default options when no recommendation is selected + wall_insulation: "Wall Insulation", + floor_insulation: "Floor Insulation", + roof_insulation: "Roof Insulation", }; export default function RecommendationCard({ @@ -59,26 +68,13 @@ export default function RecommendationCard({ const [modalIsOpen, setModalIsOpen] = useState(false); const getTitle = () => { - if (componentType === "wall_insulation" && cardComponent) { - const description = cardComponent.description.toLowerCase(); - if (description.includes("internal")) { - return TitleMap.iwi; - } else if (description.includes("external")) { - return TitleMap.ewi; - } else if (description.includes("cavity")) { - return TitleMap.cwi; - } + if (!cardComponent) { + return TitleMap[componentType]; } - if (componentType === "roof_insulation" && cardComponent) { - const description = cardComponent.description.toLowerCase(); - if (description.includes("loft")) { - return TitleMap.loft_insulation; - } - } + const recommendationType = cardComponent.type as RecommendationType; - // Logic for other componentTypes can be added here - return TitleMap[componentType]; + return TitleMap[recommendationType]; }; return ( diff --git a/src/app/components/building-passport/RecommendationContainer.tsx b/src/app/components/building-passport/RecommendationContainer.tsx index cb95df2..8d03d8a 100644 --- a/src/app/components/building-passport/RecommendationContainer.tsx +++ b/src/app/components/building-passport/RecommendationContainer.tsx @@ -19,20 +19,37 @@ interface RecommendationContainerProps { propertyMeta: PropertyMeta; } +const typeToCategoryMap: { [key in RecommendationType]?: RecommendationType } = + { + internal_wall_insulation: "wall_insulation", + external_wall_insulation: "wall_insulation", + cavity_wall_insulation: "wall_insulation", + loft_insulation: "roof_insulation", + room_roof_insulation: "roof_insulation", + flat_roof_insulation: "roof_insulation", + suspended_floor_insulation: "floor_insulation", + solid_floor_insulation: "floor_insulation", + exposed_floor_insulation: "floor_insulation", + }; + export default function RecommendationContainer({ recommendations, propertyMeta, }: RecommendationContainerProps) { const categorizedRecommendations = recommendations.reduce((acc, curr) => { const typeKey = curr.type as RecommendationType; + const category = typeToCategoryMap[typeKey] ?? typeKey; - if (!acc[typeKey]) { - acc[typeKey] = []; + if (!acc[category]) { + acc[category] = []; } - acc[typeKey].push(curr); + acc[category].push(curr); + return acc; }, {} as Record); + console.log(categorizedRecommendations); + const defaultWallsRecommendations = categorizedRecommendations.wall_insulation?.find( (rec: Recommendation) => rec.default diff --git a/src/app/db/schema/recommendations.ts b/src/app/db/schema/recommendations.ts index 2bedb4a..d0b5a98 100644 --- a/src/app/db/schema/recommendations.ts +++ b/src/app/db/schema/recommendations.ts @@ -96,7 +96,8 @@ export type RecommendationMaterial = InferModel< // We allow recommendation types to be a string however we'll set up a typing for it here to control // the types we expect // Note: roof_insulation, wall_insulation and floor_insulation aren't used in the app anymore and -// have been superseded by the more specific types. The backend will no longer return these types +// have been superseded by the more specific types. The backend will no longer return these types and +// we will depracate these in the future export type RecommendationType = | "internal_wall_insulation" | "external_wall_insulation"