-
+
diff --git a/src/components/Project/Infrastructure/InfraTable.jsx b/src/components/Project/Infrastructure/InfraTable.jsx
index 90e6a0c1..969e01fc 100644
--- a/src/components/Project/Infrastructure/InfraTable.jsx
+++ b/src/components/Project/Infrastructure/InfraTable.jsx
@@ -1,69 +1,154 @@
-import {useEffect, useState} from "react";
+import { useEffect, useState } from "react";
import Building from "./Building";
import Floor from "./Floor";
+import FloorModel from "./FloorModel";
+import showToast from "../../../services/toastService";
+import ProjectRepository from "../../../repositories/ProjectRepository";
-const InfraTable = ( {buildings,assign} ) =>
-{
- const [projectBuiling,setProjectBuilding] = useState([])
- const [expandedBuildings, setExpandedBuildings] = useState([]);
-
- const toggleBuilding = (buildingId) => {
- setExpandedBuildings((prevExpanded) =>
- prevExpanded.includes(buildingId)
- ? prevExpanded.filter((id) => id !== buildingId)
- : [...prevExpanded, buildingId]
- );
- };
-
+const InfraTable = ({ buildings }) => {
+ const [projectBuilding, setProjectBuilding] = useState([]);
+ const [expandedBuildings, setExpandedBuildings] = useState([]);
+ const [showFloorModal, setShowFloorModal] = useState(false);
+ const [selectedBuilding, setSelectedBuilding] = useState(null);
+ const [clearTrigger, setClearTrigger] = useState(false);
- const getContent = (building) => {
- return building.floors.length > 0 ? (
- building.floors.map((floor) =>
)
- ) : (
-
-
-
- No floors have been added yet. Please add floors to start managing your building.
-
-
- |
-
- );
- };
-
-
- useEffect( () =>
- {
- setProjectBuilding(buildings)
- },[buildings])
-
- return (
-
- {projectBuiling && projectBuiling.length > 0 && (
-
-
- {projectBuiling.map((building) => (
-
- ))}
-
-
- )}
-
+ const toggleBuilding = (buildingId) => {
+ setExpandedBuildings((prevExpanded) =>
+ prevExpanded.includes(buildingId)
+ ? prevExpanded.filter((id) => id !== buildingId)
+ : [...prevExpanded, buildingId]
);
};
- export default InfraTable
\ No newline at end of file
+
+ const handleAddFloor = (building) => {
+ setSelectedBuilding(building);
+ setShowFloorModal(true);
+ };
+
+ const handleFloorSubmit = async (data) => {
+ try {
+ const payload = [
+ {
+ building: null,
+ floor: {
+ id: data.id || "0",
+ floorName: data.floorName,
+ buildingId: data.buildingId,
+ },
+ workArea: null,
+ },
+ ];
+
+ const res = await ProjectRepository.manageProjectInfra(payload);
+
+ if (res?.success) {
+ showToast("Floor saved successfully!", "success");
+
+ // Find and update the correct building
+ const updatedBuildings = projectBuilding.map((b) => {
+ if (b.id === parseInt(data.buildingId)) {
+ const newFloor = {
+ id: res.data?.[0]?.floor?.id || Math.random(),
+ floorName: res.data?.[0]?.floor?.floorName || data.floorName,
+ workAreas: [],
+ };
+
+ return {
+ ...b,
+ floors: [...(b.floors || []), newFloor],
+ };
+ }
+ return b;
+ });
+
+ setProjectBuilding(updatedBuildings);
+ setShowFloorModal(false);
+ setClearTrigger(true);
+ } else {
+ showToast("Failed to save floor", "error");
+ }
+ } catch (err) {
+ console.error("Error adding floor", err);
+ showToast("Error occurred while saving floor", "error");
+ }
+ };
+
+
+ const handleClearComplete = () => {
+ setClearTrigger(false);
+ };
+
+ const getContent = (building) => {
+ return building.floors?.length > 0 ? (
+ building.floors.map((floor) => (
+
+ ))
+ ) : (
+
+
+
+ No floors have been added yet. Please add floors to start managing your building.
+
+
+ |
+
+ );
+ };
+ useEffect(() => {
+ setProjectBuilding(buildings);
+ }, [buildings]);
+
+ return (
+
+ {projectBuilding && projectBuilding.length > 0 && (
+
+
+ {projectBuilding.map((building) => (
+
+ ))}
+
+
+ )}
+ {showFloorModal && selectedBuilding && (
+
+ setShowFloorModal(false)}
+ onSubmit={handleFloorSubmit}
+ onClearComplete={handleClearComplete}
+ />
+
+ )}
+
+
+ );
+};
+
+export default InfraTable;
diff --git a/src/components/Project/ProjectInfra.jsx b/src/components/Project/ProjectInfra.jsx
index 351eeead..f85e91db 100644
--- a/src/components/Project/ProjectInfra.jsx
+++ b/src/components/Project/ProjectInfra.jsx
@@ -312,12 +312,12 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) =
}
const openModal = () => {
const modalElement = document.getElementById('building-model');
- const modal = new Modal(modalElement, {
- backdrop: false,
- keyboard: true,
- focus: true
- });
- modal.show()
+ const modal = new Modal(modalElement, {
+ backdrop: false,
+ keyboard: true,
+ focus: true
+ });
+ modal.show()
};
const closeModal = () => {
setIsModalOpen(false);
@@ -364,10 +364,12 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) =
)}
@@ -437,15 +439,14 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) =
Manage Building
+ type="button"
+ className="link-button m-1"
+ onClick={() => openFloorModel()}
+>
+