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
This commit is contained in:
parent
5cc5ff49c2
commit
5be4f781cf
@ -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 (
|
||||
<div className="modal-dialog modal-lg modal-simple modal-edit-user">
|
||||
<div className="modal-content">
|
||||
@ -122,16 +119,24 @@ const BuildingModel = ({
|
||||
handleBuildingChange(e);
|
||||
}}
|
||||
>
|
||||
|
||||
<option value="0">Add New Building</option>
|
||||
|
||||
{project &&
|
||||
project?.buildings?.length > 0 &&
|
||||
project?.buildings.map((building) => (
|
||||
<option key={building.id} value={building.id}>
|
||||
{building.name}
|
||||
</option>
|
||||
))}
|
||||
<option value="0">Add New Building</option>
|
||||
|
||||
{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) => (
|
||||
<option key={building.id} value={building.id}>
|
||||
{building.name}
|
||||
</option>
|
||||
))
|
||||
) : (
|
||||
<option value="0">No Buildings Available</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.Id && (
|
||||
<span className="danger-text">{errors.Id.message}</span>
|
||||
|
@ -135,16 +135,22 @@ const FloorModel = ({
|
||||
onChange={handleBuildigChange}
|
||||
>
|
||||
<option value="0">Select Building</option>
|
||||
{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) => (
|
||||
<option key={building.id} value={building.id}>
|
||||
{building.name}
|
||||
</option>
|
||||
))}
|
||||
))
|
||||
) : (
|
||||
<option value="0">No Buildings Available</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.buildingId && (
|
||||
<p className="text-danger">{errors.buildingId.message}</p>
|
||||
@ -168,15 +174,21 @@ const FloorModel = ({
|
||||
</option>
|
||||
) )} */}
|
||||
|
||||
{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) => (
|
||||
<option key={floor.id} value={floor.id}>
|
||||
{floor.floorName}
|
||||
</option>
|
||||
))}
|
||||
))
|
||||
) : (
|
||||
<option value="0">No Floors Available</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.id && (
|
||||
<p className="text-danger">{errors.id.message}</p>
|
||||
|
@ -163,8 +163,12 @@ const TaskModel = ({
|
||||
>
|
||||
<option value="0">Select Building</option>
|
||||
{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) => (
|
||||
<option key={building.id} value={building.id}>
|
||||
{building.name}
|
||||
@ -191,11 +195,16 @@ const TaskModel = ({
|
||||
<option value="0">Select Floor</option>
|
||||
{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) => (
|
||||
<option key={floor.id} value={floor.id}>
|
||||
{floor.floorName} - ({floor.workAreas.length} Work
|
||||
Areas)
|
||||
{floor.floorName} - ({floor?.workAreas?.length || 0}{" "}
|
||||
Work Areas){" "}
|
||||
{/* Default to 0 if workAreas is undefined */}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
@ -219,8 +228,12 @@ const TaskModel = ({
|
||||
>
|
||||
<option value="0">Select Work Area</option>
|
||||
{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) => (
|
||||
<option key={workArea.id} value={workArea.id}>
|
||||
{workArea.areaName}
|
||||
@ -248,9 +261,11 @@ const TaskModel = ({
|
||||
<option value="0">Select Activity</option>
|
||||
{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) => (
|
||||
<option key={activity.id} value={activity.id}>
|
||||
{activity.activityName}
|
||||
|
@ -159,10 +159,14 @@ const WorkAreaModel = ({
|
||||
<option value="0">Select Building</option>
|
||||
{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) => (
|
||||
<option key={building.id} value={building.id}>
|
||||
{building.name}
|
||||
{building.name} {/* Fallback if name is undefined */}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
@ -183,12 +187,17 @@ const WorkAreaModel = ({
|
||||
onChange={handleFloorChange}
|
||||
>
|
||||
<option value="0">Select Floor</option>
|
||||
{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) => (
|
||||
<option key={floor.id} value={floor.id}>
|
||||
{floor.floorName}
|
||||
{floor.floorName}{" "}
|
||||
{/* Fallback if floorName is undefined */}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
@ -209,11 +218,23 @@ const WorkAreaModel = ({
|
||||
onChange={handleWorkAreaChange}
|
||||
>
|
||||
<option value="0">Create New Work Area</option>
|
||||
{selectedFloor?.workAreas?.map((workArea) => (
|
||||
<option key={workArea.id} value={workArea.id}>
|
||||
{workArea.areaName}
|
||||
</option>
|
||||
))}
|
||||
{selectedFloor?.workAreas?.length > 0 ? (
|
||||
selectedFloor.workAreas
|
||||
?.slice()
|
||||
?.sort((a, b) => {
|
||||
const nameA = a?.areaName || "";
|
||||
const nameB = b?.areaName || "";
|
||||
return nameA.localeCompare(nameB);
|
||||
})
|
||||
?.map((workArea) => (
|
||||
<option key={workArea.id} value={workArea.id}>
|
||||
{workArea.areaName}{" "}
|
||||
{/* Fallback if areaName is undefined */}
|
||||
</option>
|
||||
))
|
||||
) : (
|
||||
<option value="0">No Work Areas Available</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.id && <span>{errors.id.message}</span>}
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user