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 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?.buildings?.length > 0 ? (
|
||||||
{project &&
|
project.buildings
|
||||||
project?.buildings?.length > 0 &&
|
.filter((building) => building?.name)
|
||||||
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 disabled>No buildings found</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,23 @@ const FloorModel = ({
|
|||||||
onChange={handleBuildigChange}
|
onChange={handleBuildigChange}
|
||||||
>
|
>
|
||||||
<option value="0">Select Building</option>
|
<option value="0">Select Building</option>
|
||||||
{buildings &&
|
{buildings?.length > 0 &&
|
||||||
buildings?.length > 0 &&
|
|
||||||
buildings
|
buildings
|
||||||
?.slice()
|
.filter((building) => building?.name)
|
||||||
?.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>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{buildings?.length === 0 && (
|
||||||
|
<option disabled>No buildings found</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.buildingId && (
|
{errors.buildingId && (
|
||||||
<p className="text-danger">{errors.buildingId.message}</p>
|
<p className="text-danger">{errors.buildingId.message}</p>
|
||||||
@ -169,14 +176,23 @@ const FloorModel = ({
|
|||||||
) )} */}
|
) )} */}
|
||||||
|
|
||||||
{selectedBuilding &&
|
{selectedBuilding &&
|
||||||
selectedBuilding.floors.length > 0 &&
|
selectedBuilding.floors?.length > 0 &&
|
||||||
[...selectedBuilding.floors]
|
[...selectedBuilding.floors]
|
||||||
?.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
.filter((floor) => floor?.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}>
|
<option key={floor.id} value={floor.id}>
|
||||||
{floor.floorName}
|
{floor.floorName}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{selectedBuilding?.floors?.length === 0 && (
|
||||||
|
<option disabled>No floors found</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.id && (
|
{errors.id && (
|
||||||
<p className="text-danger">{errors.id.message}</p>
|
<p className="text-danger">{errors.id.message}</p>
|
||||||
|
@ -163,13 +163,18 @@ const TaskModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Building</option>
|
<option value="0">Select Building</option>
|
||||||
{project.buildings
|
{project.buildings
|
||||||
?.slice()
|
?.filter((building) => building?.name) // Ensure valid name
|
||||||
?.sort((a, b) => a.name.localeCompare(b.name))
|
?.sort((a, b) => a.name?.localeCompare(b.name))
|
||||||
?.map((building) => (
|
?.map((building) => (
|
||||||
<option key={building.id} value={building.id}>
|
<option key={building.id} value={building.id}>
|
||||||
{building.name}
|
{building.name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{project.buildings?.filter((building) => building?.name)
|
||||||
|
.length === 0 && (
|
||||||
|
<option disabled>No buildings found</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.buildingID && (
|
{errors.buildingID && (
|
||||||
<p className="danger-text">{errors.buildingID.message}</p>
|
<p className="danger-text">{errors.buildingID.message}</p>
|
||||||
@ -190,7 +195,10 @@ const TaskModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Floor</option>
|
<option value="0">Select Floor</option>
|
||||||
{selectedBuilding.floors
|
{selectedBuilding.floors
|
||||||
?.slice()
|
?.filter(
|
||||||
|
(floor) =>
|
||||||
|
floor?.floorName && Array.isArray(floor.workAreas)
|
||||||
|
)
|
||||||
?.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
?.sort((a, b) => a.floorName.localeCompare(b.floorName))
|
||||||
?.map((floor) => (
|
?.map((floor) => (
|
||||||
<option key={floor.id} value={floor.id}>
|
<option key={floor.id} value={floor.id}>
|
||||||
@ -198,6 +206,11 @@ const TaskModel = ({
|
|||||||
Areas)
|
Areas)
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{selectedBuilding.floors?.filter(
|
||||||
|
(floor) =>
|
||||||
|
floor?.floorName && Array.isArray(floor.workAreas)
|
||||||
|
).length === 0 && <option disabled>No floors found</option>}
|
||||||
</select>
|
</select>
|
||||||
{errors.floorId && (
|
{errors.floorId && (
|
||||||
<p className="danger-text">{errors.floorId.message}</p>
|
<p className="danger-text">{errors.floorId.message}</p>
|
||||||
@ -219,13 +232,19 @@ const TaskModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Work Area</option>
|
<option value="0">Select Work Area</option>
|
||||||
{selectedFloor.workAreas
|
{selectedFloor.workAreas
|
||||||
?.slice()
|
?.filter((workArea) => workArea?.areaName)
|
||||||
?.sort((a, b) => a.areaName.localeCompare(b.areaName))
|
?.sort((a, b) => a.areaName.localeCompare(b.areaName))
|
||||||
?.map((workArea) => (
|
?.map((workArea) => (
|
||||||
<option key={workArea.id} value={workArea.id}>
|
<option key={workArea.id} value={workArea.id}>
|
||||||
{workArea.areaName}
|
{workArea.areaName}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{selectedFloor.workAreas?.filter(
|
||||||
|
(workArea) => workArea?.areaName
|
||||||
|
).length === 0 && (
|
||||||
|
<option disabled>No work areas found</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.workAreaId && (
|
{errors.workAreaId && (
|
||||||
<p className="danger-text">{errors.workAreaId.message}</p>
|
<p className="danger-text">{errors.workAreaId.message}</p>
|
||||||
@ -248,15 +267,18 @@ 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}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
{errors.activityID && (
|
{errors.activityID && (
|
||||||
<p className="danger-text">{errors.activityID.message}</p>
|
<p className="danger-text">{errors.activityID.message}</p>
|
||||||
)}
|
)}
|
||||||
|
@ -158,13 +158,22 @@ const WorkAreaModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Building</option>
|
<option value="0">Select Building</option>
|
||||||
{project?.buildings
|
{project?.buildings
|
||||||
?.slice()
|
?.filter((building) => building?.name)
|
||||||
.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>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{project?.buildings?.filter((building) => building?.name)
|
||||||
|
.length === 0 && (
|
||||||
|
<option disabled>No buildings found</option>
|
||||||
|
)}
|
||||||
</select>
|
</select>
|
||||||
{errors.buildingId && <span>{errors.buildingId.message}</span>}
|
{errors.buildingId && <span>{errors.buildingId.message}</span>}
|
||||||
</div>
|
</div>
|
||||||
@ -184,13 +193,21 @@ const WorkAreaModel = ({
|
|||||||
>
|
>
|
||||||
<option value="0">Select Floor</option>
|
<option value="0">Select Floor</option>
|
||||||
{selectedBuilding.floors
|
{selectedBuilding.floors
|
||||||
?.slice()
|
?.filter((floor) => floor?.floorName)
|
||||||
.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>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
{selectedBuilding.floors?.filter(
|
||||||
|
(floor) => floor?.floorName
|
||||||
|
).length === 0 && <option disabled>No floors found</option>}
|
||||||
</select>
|
</select>
|
||||||
{errors.floorId && <span>{errors.floorId.message}</span>}
|
{errors.floorId && <span>{errors.floorId.message}</span>}
|
||||||
</div>
|
</div>
|
||||||
@ -209,11 +226,24 @@ 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
|
||||||
<option key={workArea.id} value={workArea.id}>
|
?.filter((workArea) => workArea?.areaName)
|
||||||
{workArea.areaName}
|
?.sort((a, b) => {
|
||||||
</option>
|
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>
|
</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