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 ProjectRepository from "../../../repositories/ProjectRepository";
|
||||||
import { useSelector } from "react-redux";
|
import { useSelector } from "react-redux";
|
||||||
import { useProjectDetails } from "../../../hooks/useProjects";
|
import { useProjectDetails } from "../../../hooks/useProjects";
|
||||||
import {getCachedData} from "../../../slices/apiDataManager";
|
import { getCachedData } from "../../../slices/apiDataManager";
|
||||||
import showToast from "../../../services/toastService";
|
import showToast from "../../../services/toastService";
|
||||||
|
|
||||||
// Zod validation schema
|
// Zod validation schema
|
||||||
@ -29,8 +29,8 @@ const BuildingModel = ({
|
|||||||
const selectedProject = useSelector(
|
const selectedProject = useSelector(
|
||||||
(store) => store.localVariables.projectId
|
(store) => store.localVariables.projectId
|
||||||
);
|
);
|
||||||
const [buildings ,setBuildings] = useState([])
|
const [buildings, setBuildings] = useState([]);
|
||||||
const projects_Details = getCachedData("projectInfo")
|
const projects_Details = getCachedData("projectInfo");
|
||||||
const [formData, setFormData] = useState({
|
const [formData, setFormData] = useState({
|
||||||
id: "",
|
id: "",
|
||||||
name: "",
|
name: "",
|
||||||
@ -80,24 +80,21 @@ const BuildingModel = ({
|
|||||||
|
|
||||||
const onSubmitHandler = async (data) => {
|
const onSubmitHandler = async (data) => {
|
||||||
onSubmit({ ...data, projectId: project.id });
|
onSubmit({ ...data, projectId: project.id });
|
||||||
reset( {
|
reset({
|
||||||
Id:"0",
|
Id: "0",
|
||||||
name: "",
|
name: "",
|
||||||
description: "",
|
description: "",
|
||||||
} );
|
});
|
||||||
if ( data.Id !== "0" )
|
if (data.Id !== "0") {
|
||||||
{
|
showToast("Building updated successfully.", "success");
|
||||||
showToast( "Building updated successfully.", "success" );
|
} else {
|
||||||
} else
|
showToast("Building created successfully.", "success");
|
||||||
{
|
}
|
||||||
showToast( "Building created successfully.", "success" );
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect( () =>
|
useEffect(() => {
|
||||||
{
|
setBuildings(projects_Details.data?.buildings);
|
||||||
setBuildings(projects_Details.data?.buildings)
|
}, [projects_Details]);
|
||||||
},[projects_Details])
|
|
||||||
return (
|
return (
|
||||||
<div className="modal-dialog modal-lg modal-simple modal-edit-user">
|
<div className="modal-dialog modal-lg modal-simple modal-edit-user">
|
||||||
<div className="modal-content">
|
<div className="modal-content">
|
||||||
@ -122,16 +119,24 @@ const BuildingModel = ({
|
|||||||
handleBuildingChange(e);
|
handleBuildingChange(e);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
<option value="0">Add New Building</option>
|
||||||
<option value="0">Add New Building</option>
|
|
||||||
|
{project && project.buildings?.length > 0 ? (
|
||||||
{project &&
|
project.buildings
|
||||||
project?.buildings?.length > 0 &&
|
.slice()
|
||||||
project?.buildings.map((building) => (
|
.sort((a, b) => {
|
||||||
<option key={building.id} value={building.id}>
|
const nameA = a?.name || "";
|
||||||
{building.name}
|
const nameB = b?.name || "";
|
||||||
</option>
|
return nameA.localeCompare(nameB);
|
||||||
))}
|
})
|
||||||
|
.map((building) => (
|
||||||
|
<option key={building.id} value={building.id}>
|
||||||
|
{building.name}
|
||||||
|
</option>
|
||||||
|
))
|
||||||
|
) : (
|
||||||
|
<option value="0">No Buildings Available</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.Id && (
|
{errors.Id && (
|
||||||
<span className="danger-text">{errors.Id.message}</span>
|
<span className="danger-text">{errors.Id.message}</span>
|
||||||
|
|||||||
@ -135,16 +135,22 @@ const FloorModel = ({
|
|||||||
onChange={handleBuildigChange}
|
onChange={handleBuildigChange}
|
||||||
>
|
>
|
||||||
<option value="0">Select Building</option>
|
<option value="0">Select Building</option>
|
||||||
{buildings &&
|
{buildings && buildings.length > 0 ? (
|
||||||
buildings?.length > 0 &&
|
|
||||||
buildings
|
buildings
|
||||||
?.slice()
|
.slice()
|
||||||
?.sort((a, b) => a.name.localeCompare(b.name))
|
.sort((a, b) => {
|
||||||
?.map((building) => (
|
const nameA = a?.name || "";
|
||||||
|
const nameB = b?.name || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
|
.map((building) => (
|
||||||
<option key={building.id} value={building.id}>
|
<option key={building.id} value={building.id}>
|
||||||
{building.name}
|
{building.name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))
|
||||||
|
) : (
|
||||||
|
<option value="0">No Buildings Available</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.buildingId && (
|
{errors.buildingId && (
|
||||||
<p className="text-danger">{errors.buildingId.message}</p>
|
<p className="text-danger">{errors.buildingId.message}</p>
|
||||||
@ -168,15 +174,21 @@ const FloorModel = ({
|
|||||||
</option>
|
</option>
|
||||||
) )} */}
|
) )} */}
|
||||||
|
|
||||||
{selectedBuilding &&
|
{selectedBuilding && selectedBuilding.floors.length > 0 ? (
|
||||||
selectedBuilding.floors.length > 0 &&
|
|
||||||
[...selectedBuilding.floors]
|
[...selectedBuilding.floors]
|
||||||
?.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
.sort((a, b) => {
|
||||||
?.map((floor) => (
|
const nameA = a?.floorName || "";
|
||||||
|
const nameB = b?.floorName || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
|
.map((floor) => (
|
||||||
<option key={floor.id} value={floor.id}>
|
<option key={floor.id} value={floor.id}>
|
||||||
{floor.floorName}
|
{floor.floorName}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))
|
||||||
|
) : (
|
||||||
|
<option value="0">No Floors Available</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.id && (
|
{errors.id && (
|
||||||
<p className="text-danger">{errors.id.message}</p>
|
<p className="text-danger">{errors.id.message}</p>
|
||||||
|
|||||||
@ -163,8 +163,12 @@ const TaskModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Building</option>
|
<option value="0">Select Building</option>
|
||||||
{project.buildings
|
{project.buildings
|
||||||
?.slice()
|
?.slice() //
|
||||||
?.sort((a, b) => a.name.localeCompare(b.name))
|
?.sort((a, b) => {
|
||||||
|
const nameA = a?.name || "";
|
||||||
|
const nameB = b?.name || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
?.map((building) => (
|
?.map((building) => (
|
||||||
<option key={building.id} value={building.id}>
|
<option key={building.id} value={building.id}>
|
||||||
{building.name}
|
{building.name}
|
||||||
@ -191,11 +195,16 @@ const TaskModel = ({
|
|||||||
<option value="0">Select Floor</option>
|
<option value="0">Select Floor</option>
|
||||||
{selectedBuilding.floors
|
{selectedBuilding.floors
|
||||||
?.slice()
|
?.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) => (
|
?.map((floor) => (
|
||||||
<option key={floor.id} value={floor.id}>
|
<option key={floor.id} value={floor.id}>
|
||||||
{floor.floorName} - ({floor.workAreas.length} Work
|
{floor.floorName} - ({floor?.workAreas?.length || 0}{" "}
|
||||||
Areas)
|
Work Areas){" "}
|
||||||
|
{/* Default to 0 if workAreas is undefined */}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
@ -219,8 +228,12 @@ const TaskModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Work Area</option>
|
<option value="0">Select Work Area</option>
|
||||||
{selectedFloor.workAreas
|
{selectedFloor.workAreas
|
||||||
?.slice()
|
?.slice()
|
||||||
?.sort((a, b) => a.areaName.localeCompare(b.areaName))
|
?.sort((a, b) => {
|
||||||
|
const nameA = a?.areaName || "";
|
||||||
|
const nameB = b?.areaName || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
?.map((workArea) => (
|
?.map((workArea) => (
|
||||||
<option key={workArea.id} value={workArea.id}>
|
<option key={workArea.id} value={workArea.id}>
|
||||||
{workArea.areaName}
|
{workArea.areaName}
|
||||||
@ -248,9 +261,11 @@ const TaskModel = ({
|
|||||||
<option value="0">Select Activity</option>
|
<option value="0">Select Activity</option>
|
||||||
{activities
|
{activities
|
||||||
?.slice()
|
?.slice()
|
||||||
?.sort((a, b) =>
|
?.sort((a, b) => {
|
||||||
a.activityName.localeCompare(b.activityName)
|
const nameA = a?.activityName || "";
|
||||||
)
|
const nameB = b?.activityName || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
?.map((activity) => (
|
?.map((activity) => (
|
||||||
<option key={activity.id} value={activity.id}>
|
<option key={activity.id} value={activity.id}>
|
||||||
{activity.activityName}
|
{activity.activityName}
|
||||||
|
|||||||
@ -159,10 +159,14 @@ const WorkAreaModel = ({
|
|||||||
<option value="0">Select Building</option>
|
<option value="0">Select Building</option>
|
||||||
{project?.buildings
|
{project?.buildings
|
||||||
?.slice()
|
?.slice()
|
||||||
.sort((a, b) => a.name.localeCompare(b.name))
|
?.sort((a, b) => {
|
||||||
.map((building) => (
|
const nameA = a?.name || "";
|
||||||
|
const nameB = b?.name || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
|
?.map((building) => (
|
||||||
<option key={building.id} value={building.id}>
|
<option key={building.id} value={building.id}>
|
||||||
{building.name}
|
{building.name} {/* Fallback if name is undefined */}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
@ -183,12 +187,17 @@ const WorkAreaModel = ({
|
|||||||
onChange={handleFloorChange}
|
onChange={handleFloorChange}
|
||||||
>
|
>
|
||||||
<option value="0">Select Floor</option>
|
<option value="0">Select Floor</option>
|
||||||
{selectedBuilding.floors
|
{selectedBuilding?.floors
|
||||||
?.slice()
|
?.slice()
|
||||||
.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
?.sort((a, b) => {
|
||||||
.map((floor) => (
|
const nameA = a?.floorName || "";
|
||||||
|
const nameB = b?.floorName || "";
|
||||||
|
return nameA.localeCompare(nameB);
|
||||||
|
})
|
||||||
|
?.map((floor) => (
|
||||||
<option key={floor.id} value={floor.id}>
|
<option key={floor.id} value={floor.id}>
|
||||||
{floor.floorName}
|
{floor.floorName}{" "}
|
||||||
|
{/* Fallback if floorName is undefined */}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
@ -209,11 +218,23 @@ const WorkAreaModel = ({
|
|||||||
onChange={handleWorkAreaChange}
|
onChange={handleWorkAreaChange}
|
||||||
>
|
>
|
||||||
<option value="0">Create New Work Area</option>
|
<option value="0">Create New Work Area</option>
|
||||||
{selectedFloor?.workAreas?.map((workArea) => (
|
{selectedFloor?.workAreas?.length > 0 ? (
|
||||||
<option key={workArea.id} value={workArea.id}>
|
selectedFloor.workAreas
|
||||||
{workArea.areaName}
|
?.slice()
|
||||||
</option>
|
?.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>
|
</select>
|
||||||
{errors.id && <span>{errors.id.message}</span>}
|
{errors.id && <span>{errors.id.message}</span>}
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user