Improve safety and sorting for building dropdown
- Added optional chaining to safely access project.buildings and prevent errors if undefined. - Filtered out buildings with invalid or missing 'name' properties. - Implemented safe sorting using localeCompare, with a fallback to empty string for undefined 'name' values. - Added fallback message "No buildings found" when there are no valid buildings in the list.
This commit is contained in:
parent
18e4b91754
commit
80d64c87c6
@ -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?.buildings?.length > 0 ? (
|
||||
project.buildings
|
||||
.filter((building) => building?.name)
|
||||
.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 disabled>No buildings found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.Id && (
|
||||
<span className="danger-text">{errors.Id.message}</span>
|
||||
|
@ -135,16 +135,23 @@ const FloorModel = ({
|
||||
onChange={handleBuildigChange}
|
||||
>
|
||||
<option value="0">Select Building</option>
|
||||
{buildings &&
|
||||
buildings?.length > 0 &&
|
||||
{buildings?.length > 0 &&
|
||||
buildings
|
||||
?.slice()
|
||||
?.sort((a, b) => a.name.localeCompare(b.name))
|
||||
?.map((building) => (
|
||||
.filter((building) => building?.name)
|
||||
.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>
|
||||
))}
|
||||
|
||||
{buildings?.length === 0 && (
|
||||
<option disabled>No buildings found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.buildingId && (
|
||||
<p className="text-danger">{errors.buildingId.message}</p>
|
||||
@ -169,14 +176,23 @@ const FloorModel = ({
|
||||
) )} */}
|
||||
|
||||
{selectedBuilding &&
|
||||
selectedBuilding.floors.length > 0 &&
|
||||
selectedBuilding.floors?.length > 0 &&
|
||||
[...selectedBuilding.floors]
|
||||
?.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
||||
?.map((floor) => (
|
||||
.filter((floor) => floor?.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}
|
||||
</option>
|
||||
))}
|
||||
|
||||
{selectedBuilding?.floors?.length === 0 && (
|
||||
<option disabled>No floors found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.id && (
|
||||
<p className="text-danger">{errors.id.message}</p>
|
||||
|
@ -163,13 +163,18 @@ const TaskModel = ({
|
||||
>
|
||||
<option value="0">Select Building</option>
|
||||
{project.buildings
|
||||
?.slice()
|
||||
?.sort((a, b) => a.name.localeCompare(b.name))
|
||||
?.filter((building) => building?.name) // Ensure valid name
|
||||
?.sort((a, b) => a.name?.localeCompare(b.name))
|
||||
?.map((building) => (
|
||||
<option key={building.id} value={building.id}>
|
||||
{building.name}
|
||||
</option>
|
||||
))}
|
||||
|
||||
{project.buildings?.filter((building) => building?.name)
|
||||
.length === 0 && (
|
||||
<option disabled>No buildings found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.buildingID && (
|
||||
<p className="danger-text">{errors.buildingID.message}</p>
|
||||
@ -190,7 +195,10 @@ const TaskModel = ({
|
||||
>
|
||||
<option value="0">Select Floor</option>
|
||||
{selectedBuilding.floors
|
||||
?.slice()
|
||||
?.filter(
|
||||
(floor) =>
|
||||
floor?.floorName && Array.isArray(floor.workAreas)
|
||||
)
|
||||
?.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
||||
?.map((floor) => (
|
||||
<option key={floor.id} value={floor.id}>
|
||||
@ -198,6 +206,11 @@ const TaskModel = ({
|
||||
Areas)
|
||||
</option>
|
||||
))}
|
||||
|
||||
{selectedBuilding.floors?.filter(
|
||||
(floor) =>
|
||||
floor?.floorName && Array.isArray(floor.workAreas)
|
||||
).length === 0 && <option disabled>No floors found</option>}
|
||||
</select>
|
||||
{errors.floorId && (
|
||||
<p className="danger-text">{errors.floorId.message}</p>
|
||||
@ -219,13 +232,19 @@ const TaskModel = ({
|
||||
>
|
||||
<option value="0">Select Work Area</option>
|
||||
{selectedFloor.workAreas
|
||||
?.slice()
|
||||
?.sort((a, b) => a.areaName.localeCompare(b.areaName))
|
||||
?.filter((workArea) => workArea?.areaName)
|
||||
?.sort((a, b) => a.areaName.localeCompare(b.areaName))
|
||||
?.map((workArea) => (
|
||||
<option key={workArea.id} value={workArea.id}>
|
||||
{workArea.areaName}
|
||||
</option>
|
||||
))}
|
||||
|
||||
{selectedFloor.workAreas?.filter(
|
||||
(workArea) => workArea?.areaName
|
||||
).length === 0 && (
|
||||
<option disabled>No work areas found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.workAreaId && (
|
||||
<p className="danger-text">{errors.workAreaId.message}</p>
|
||||
@ -248,15 +267,18 @@ 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}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
{errors.activityID && (
|
||||
<p className="danger-text">{errors.activityID.message}</p>
|
||||
)}
|
||||
|
@ -158,13 +158,22 @@ const WorkAreaModel = ({
|
||||
>
|
||||
<option value="0">Select Building</option>
|
||||
{project?.buildings
|
||||
?.slice()
|
||||
.sort((a, b) => a.name.localeCompare(b.name))
|
||||
.map((building) => (
|
||||
?.filter((building) => building?.name)
|
||||
?.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>
|
||||
))}
|
||||
|
||||
{project?.buildings?.filter((building) => building?.name)
|
||||
.length === 0 && (
|
||||
<option disabled>No buildings found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.buildingId && <span>{errors.buildingId.message}</span>}
|
||||
</div>
|
||||
@ -184,13 +193,21 @@ const WorkAreaModel = ({
|
||||
>
|
||||
<option value="0">Select Floor</option>
|
||||
{selectedBuilding.floors
|
||||
?.slice()
|
||||
.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
||||
.map((floor) => (
|
||||
?.filter((floor) => floor?.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}
|
||||
</option>
|
||||
))}
|
||||
|
||||
{selectedBuilding.floors?.filter(
|
||||
(floor) => floor?.floorName
|
||||
).length === 0 && <option disabled>No floors found</option>}
|
||||
</select>
|
||||
{errors.floorId && <span>{errors.floorId.message}</span>}
|
||||
</div>
|
||||
@ -209,11 +226,24 @@ 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
|
||||
?.filter((workArea) => workArea?.areaName)
|
||||
?.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}
|
||||
</option>
|
||||
))}
|
||||
|
||||
{selectedFloor?.workAreas?.filter(
|
||||
(workArea) => workArea?.areaName
|
||||
).length === 0 && (
|
||||
<option disabled>No work areas found</option>
|
||||
)}
|
||||
</select>
|
||||
{errors.id && <span>{errors.id.message}</span>}
|
||||
</div>
|
||||
|
Loading…
x
Reference in New Issue
Block a user