From 5be4f781cf243665f2dcad5b8f429aa47a02c7ab Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Sat, 3 May 2025 10:37:08 +0530 Subject: [PATCH] safely sort and render building options with fallbacks - Added alphabetical sorting for building names using localeCompare - Handled potential undefined values in building.name - Prevented mutation of the original buildings array using slice() - Displayed fallback message when no buildings are available --- .../Project/Infrastructure/BuildingModel.jsx | 59 ++++++++++--------- .../Project/Infrastructure/FloorModel.jsx | 34 +++++++---- .../Project/Infrastructure/TaskModel.jsx | 35 +++++++---- .../Project/Infrastructure/WorkAreaModel.jsx | 47 +++++++++++---- 4 files changed, 114 insertions(+), 61 deletions(-) diff --git a/src/components/Project/Infrastructure/BuildingModel.jsx b/src/components/Project/Infrastructure/BuildingModel.jsx index 374c7719..3467f850 100644 --- a/src/components/Project/Infrastructure/BuildingModel.jsx +++ b/src/components/Project/Infrastructure/BuildingModel.jsx @@ -5,7 +5,7 @@ import { zodResolver } from "@hookform/resolvers/zod"; import ProjectRepository from "../../../repositories/ProjectRepository"; import { useSelector } from "react-redux"; import { useProjectDetails } from "../../../hooks/useProjects"; -import {getCachedData} from "../../../slices/apiDataManager"; +import { getCachedData } from "../../../slices/apiDataManager"; import showToast from "../../../services/toastService"; // Zod validation schema @@ -29,8 +29,8 @@ const BuildingModel = ({ const selectedProject = useSelector( (store) => store.localVariables.projectId ); - const [buildings ,setBuildings] = useState([]) - const projects_Details = getCachedData("projectInfo") + const [buildings, setBuildings] = useState([]); + const projects_Details = getCachedData("projectInfo"); const [formData, setFormData] = useState({ id: "", name: "", @@ -80,24 +80,21 @@ const BuildingModel = ({ const onSubmitHandler = async (data) => { onSubmit({ ...data, projectId: project.id }); - reset( { - Id:"0", + reset({ + Id: "0", name: "", description: "", - } ); - if ( data.Id !== "0" ) - { - showToast( "Building updated successfully.", "success" ); - } else - { - showToast( "Building created successfully.", "success" ); - } + }); + if (data.Id !== "0") { + showToast("Building updated successfully.", "success"); + } else { + showToast("Building created successfully.", "success"); + } }; - useEffect( () => - { - setBuildings(projects_Details.data?.buildings) - },[projects_Details]) + useEffect(() => { + setBuildings(projects_Details.data?.buildings); + }, [projects_Details]); return (
@@ -122,16 +119,24 @@ const BuildingModel = ({ handleBuildingChange(e); }} > - - - - {project && - project?.buildings?.length > 0 && - project?.buildings.map((building) => ( - - ))} + + + {project && project.buildings?.length > 0 ? ( + project.buildings + .slice() + .sort((a, b) => { + const nameA = a?.name || ""; + const nameB = b?.name || ""; + return nameA.localeCompare(nameB); + }) + .map((building) => ( + + )) + ) : ( + + )} {errors.Id && ( {errors.Id.message} diff --git a/src/components/Project/Infrastructure/FloorModel.jsx b/src/components/Project/Infrastructure/FloorModel.jsx index 2d64c275..39dc5598 100644 --- a/src/components/Project/Infrastructure/FloorModel.jsx +++ b/src/components/Project/Infrastructure/FloorModel.jsx @@ -135,16 +135,22 @@ const FloorModel = ({ onChange={handleBuildigChange} > - {buildings && - buildings?.length > 0 && + {buildings && buildings.length > 0 ? ( buildings - ?.slice() - ?.sort((a, b) => a.name.localeCompare(b.name)) - ?.map((building) => ( + .slice() + .sort((a, b) => { + const nameA = a?.name || ""; + const nameB = b?.name || ""; + return nameA.localeCompare(nameB); + }) + .map((building) => ( - ))} + )) + ) : ( + + )} {errors.buildingId && (

{errors.buildingId.message}

@@ -168,15 +174,21 @@ const FloorModel = ({ ) )} */} - {selectedBuilding && - selectedBuilding.floors.length > 0 && + {selectedBuilding && selectedBuilding.floors.length > 0 ? ( [...selectedBuilding.floors] - ?.sort((a, b) => a.floorName.localeCompare(b.floorName)) - ?.map((floor) => ( + .sort((a, b) => { + const nameA = a?.floorName || ""; + const nameB = b?.floorName || ""; + return nameA.localeCompare(nameB); + }) + .map((floor) => ( - ))} + )) + ) : ( + + )} {errors.id && (

{errors.id.message}

diff --git a/src/components/Project/Infrastructure/TaskModel.jsx b/src/components/Project/Infrastructure/TaskModel.jsx index ee3c1ff1..02157fea 100644 --- a/src/components/Project/Infrastructure/TaskModel.jsx +++ b/src/components/Project/Infrastructure/TaskModel.jsx @@ -163,8 +163,12 @@ const TaskModel = ({ > {project.buildings - ?.slice() - ?.sort((a, b) => a.name.localeCompare(b.name)) + ?.slice() // + ?.sort((a, b) => { + const nameA = a?.name || ""; + const nameB = b?.name || ""; + return nameA.localeCompare(nameB); + }) ?.map((building) => ( {selectedBuilding.floors ?.slice() - ?.sort((a, b) => a.floorName.localeCompare(b.floorName)) + ?.sort((a, b) => { + const nameA = a?.floorName || ""; + const nameB = b?.floorName || ""; + return nameA.localeCompare(nameB); + }) ?.map((floor) => ( ))} @@ -219,8 +228,12 @@ const TaskModel = ({ > {selectedFloor.workAreas - ?.slice() - ?.sort((a, b) => a.areaName.localeCompare(b.areaName)) + ?.slice() + ?.sort((a, b) => { + const nameA = a?.areaName || ""; + const nameB = b?.areaName || ""; + return nameA.localeCompare(nameB); + }) ?.map((workArea) => ( {activities ?.slice() - ?.sort((a, b) => - a.activityName.localeCompare(b.activityName) - ) + ?.sort((a, b) => { + const nameA = a?.activityName || ""; + const nameB = b?.activityName || ""; + return nameA.localeCompare(nameB); + }) ?.map((activity) => ( {project?.buildings ?.slice() - .sort((a, b) => a.name.localeCompare(b.name)) - .map((building) => ( + ?.sort((a, b) => { + const nameA = a?.name || ""; + const nameB = b?.name || ""; + return nameA.localeCompare(nameB); + }) + ?.map((building) => ( ))} @@ -183,12 +187,17 @@ const WorkAreaModel = ({ onChange={handleFloorChange} > - {selectedBuilding.floors - ?.slice() - .sort((a, b) => a.floorName.localeCompare(b.floorName)) - .map((floor) => ( + {selectedBuilding?.floors + ?.slice() + ?.sort((a, b) => { + const nameA = a?.floorName || ""; + const nameB = b?.floorName || ""; + return nameA.localeCompare(nameB); + }) + ?.map((floor) => ( ))} @@ -209,11 +218,23 @@ const WorkAreaModel = ({ onChange={handleWorkAreaChange} > - {selectedFloor?.workAreas?.map((workArea) => ( - - ))} + {selectedFloor?.workAreas?.length > 0 ? ( + selectedFloor.workAreas + ?.slice() + ?.sort((a, b) => { + const nameA = a?.areaName || ""; + const nameB = b?.areaName || ""; + return nameA.localeCompare(nameB); + }) + ?.map((workArea) => ( + + )) + ) : ( + + )} {errors.id && {errors.id.message}}