diff --git a/src/components/Project/Infrastructure/BuildingModel.jsx b/src/components/Project/Infrastructure/BuildingModel.jsx index 1ef3e1e2..7db3c2dc 100644 --- a/src/components/Project/Infrastructure/BuildingModel.jsx +++ b/src/components/Project/Infrastructure/BuildingModel.jsx @@ -34,7 +34,7 @@ const BuildingModel = ({ id: "", name: "", description: "", - projectId: project.id, + projectId: project?.id, }); useEffect(() => { @@ -48,7 +48,7 @@ const BuildingModel = ({ return () => { setValue("name", null); }; - }, [clearTrigger, onClearComplete, editingBuilding, project.id]); + }, [clearTrigger, onClearComplete, editingBuilding, project?.id]); const { register, @@ -101,7 +101,7 @@ const BuildingModel = ({ onClick={onClose} >
- Manage Buildings - {project.name} + Manage Buildings - {project?.name}
@@ -116,9 +116,9 @@ const BuildingModel = ({ - {buildings && - buildings?.length > 0 && - buildings.map((building) => ( + {project && + project?.buildings?.length > 0 && + project?.buildings.map((building) => ( diff --git a/src/components/Project/Infrastructure/FloorModel.jsx b/src/components/Project/Infrastructure/FloorModel.jsx index 75b50294..7e5a81b1 100644 --- a/src/components/Project/Infrastructure/FloorModel.jsx +++ b/src/components/Project/Infrastructure/FloorModel.jsx @@ -26,10 +26,10 @@ const FloorModel = ({ onClearComplete, } ) => { - const [ buildings, setBuildings ] = useState( [] ) - const projects_Details = getCachedData( "projectInfo" ) + const [formData, setFormData] = useState(defaultModel); - const [selectedBuilding, setSelectedBuilding] = useState({}); + const [ selectedBuilding, setSelectedBuilding ] = useState( {} ); + const [buildings, setBuildings] = useState(project?.buildings || []); // Initialize the form with React Hook Form const { @@ -56,6 +56,7 @@ const FloorModel = ({ // Handle building selection change const handleBuildigChange = (e) => { const buildingId = e.target.value; + console.log(buildingId) const building = buildings.find((b) => b.id === Number(buildingId)); if (building) { setSelectedBuilding(building); @@ -84,7 +85,7 @@ const FloorModel = ({ // Handle floor selection change const handleFloorChange = (e) => { const id = e.target.value; - const floor = buildings[getValues("buildingId")].floors.find((b) => b.id === Number(id)); + const floor = selectedBuilding.floors.find((b) => b.id === Number(id)); if (floor) { setFormData({ id: floor.id, @@ -108,12 +109,8 @@ const FloorModel = ({ const onFormSubmit = (data) => { onSubmit(data); }; - useEffect( () => - { - setBuildings(projects_Details.data?.buildings) - }, [ projects_Details ] ) - console.log(getValues("buildingId")) - console.log(buildings) + + return (
@@ -172,9 +169,9 @@ const FloorModel = ({ ) )} */} - {buildings && - buildings[getValues("buildingId")]?.length > 0 && - buildings[getValues("buildingId")]?.map((floor) => ( + {selectedBuilding && + selectedBuilding?.floors.length > 0 && + selectedBuilding?.floors.map((floor) => ( diff --git a/src/components/Project/Infrastructure/WorkAreaModel.jsx b/src/components/Project/Infrastructure/WorkAreaModel.jsx index 7dae3403..5d417772 100644 --- a/src/components/Project/Infrastructure/WorkAreaModel.jsx +++ b/src/components/Project/Infrastructure/WorkAreaModel.jsx @@ -82,7 +82,7 @@ const WorkAreaModel = ({ project, onSubmit, clearTrigger, onClearComplete, onClo const handleBuildingChange = (e) => { const { value } = e.target; - const building = project.buildings.find((b) => b.id === Number(value)); + const building = project?.buildings.find((b) => b.id === Number(value)); setSelectedBuilding(building); setSelectedFloor(null); // Reset selected floor on building change reset(defaultModel); // Reset the form when a new building is selected @@ -90,7 +90,7 @@ const WorkAreaModel = ({ project, onSubmit, clearTrigger, onClearComplete, onClo const onSubmitForm = ( data ) => { - console.log(data) + let WorkArea = { id: data.id, areaName: data.areaName, @@ -103,7 +103,8 @@ const WorkAreaModel = ({ project, onSubmit, clearTrigger, onClearComplete, onClo const handleCancel = () => { reset(defaultModel); // Reset the form to initial state setSelectedFloor(null); - setSelectedBuilding(null); + setSelectedBuilding( null ); + onClose() }; return ( @@ -127,7 +128,7 @@ const WorkAreaModel = ({ project, onSubmit, clearTrigger, onClearComplete, onClo onChange={handleBuildingChange} > - {project.buildings.map((building) => ( + {project?.buildings?.map((building) => ( @@ -171,7 +172,7 @@ const WorkAreaModel = ({ project, onSubmit, clearTrigger, onClearComplete, onClo onChange={handleWorkAreaChange} > - {selectedFloor?.workAreas.map((workArea) => ( + {selectedFloor?.workAreas?.map((workArea) => ( diff --git a/src/components/Project/Infrastructure/WorkItem.jsx b/src/components/Project/Infrastructure/WorkItem.jsx index 793fb86f..94160066 100644 --- a/src/components/Project/Infrastructure/WorkItem.jsx +++ b/src/components/Project/Infrastructure/WorkItem.jsx @@ -20,12 +20,7 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { setItemName(""); }; - // const showCreateItemModal = (modalData) => { - // openModal( - // , - // handleAssignTask ,"lg" - // ); - // }; + useEffect(() => { setNewWorkItem(workItem); diff --git a/src/components/Project/ProjectInfra.jsx b/src/components/Project/ProjectInfra.jsx index 21ad8f30..70e84b6a 100644 --- a/src/components/Project/ProjectInfra.jsx +++ b/src/components/Project/ProjectInfra.jsx @@ -5,80 +5,69 @@ import FloorModel from "./Infrastructure/FloorModel"; import showToast from "../../services/toastService"; import WorkAreaModel from "./Infrastructure/WorkAreaModel"; import TaskModel from "./Infrastructure/TaskModel"; -import ProjectRepository, {TasksRepository} from "../../repositories/ProjectRepository"; +import ProjectRepository, { + TasksRepository, +} from "../../repositories/ProjectRepository"; import ProjectModal from "./ProjectModal"; -import {useHasUserPermission} from "../../hooks/useHasUserPermission"; -import {MANAGE_PROJECT_INFRA} from "../../utils/constants"; +import { useHasUserPermission } from "../../hooks/useHasUserPermission"; +import { MANAGE_PROJECT_INFRA } from "../../utils/constants"; import InfraTable from "./Infrastructure/InfraTable"; -import {cacheData} from "../../slices/apiDataManager"; +import { cacheData, getCachedData } from "../../slices/apiDataManager"; +import { useProjectDetails } from "../../hooks/useProjects"; - - -const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) => { +const ProjectInfra = ({ + data, + activityMaster, + onDataChange, + eachSiteEngineer, +}) => { const [expandedBuildings, setExpandedBuildings] = useState([]); - const [project, setProject] = useState(data); - const[modalConfig,setModalConfig] = useState({type:null,data:null}); - const [ isModalOpen, setIsModalOpen ] = useState( false ) - const ManageInfra = useHasUserPermission(MANAGE_PROJECT_INFRA) + const { projects_Details, loading } = useProjectDetails(data.id); + const [project, setProject] = useState(projects_Details); + const [modalConfig, setModalConfig] = useState({ type: null, data: null }); + const [isModalOpen, setIsModalOpen] = useState(false); + const ManageInfra = useHasUserPermission(MANAGE_PROJECT_INFRA); - const [buildings, setBuildings] = useState(data.buildings); const [isBuildingModalOpen, setIsBuildingModalOpen] = useState(false); const [isFloorModalOpen, setIsFloorModalOpen] = useState(false); const [isWorkAreaModelOpen, setIsWorkAreaModalOpen] = useState(false); const [isTaskModelOpen, setIsTaskModalOpen] = useState(false); - const [isAssignRoleModal,setIsAssingRoleModal] = useState(false) + const [isAssignRoleModal, setIsAssingRoleModal] = useState(false); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [clearFormTrigger, setClearFormTrigger] = useState(false); - const [ CurrentBuilding, setCurrentBuilding ] = useState( "" ) + const [CurrentBuilding, setCurrentBuilding] = useState(""); const [showModal, setShowModal] = useState(false); useEffect(() => { - setProject(data); - setBuildings(data.buildings); - }, [data]); - - + setProject(projects_Details); + }, [data, projects_Details]); const openFloorModel = (projectData) => { setIsFloorModalOpen(true); }; const closeFloorModel = () => { setIsFloorModalOpen(false); - }; - const openAssignModel=(assignData)=>{ - - setCurrentBuilding(assignData) - setIsAssingRoleModal(true) - - } - - - - + const openAssignModel = (assignData) => { + setCurrentBuilding(assignData); + setIsAssingRoleModal(true); + }; const openBuildingModel = (projectData) => { setIsBuildingModalOpen(true); }; - - const submitData = async (infraObject) => { - - try - { - console.log(infraObject) - let response = await ProjectRepository.manageProjectInfra( infraObject ); + try { + let response = await ProjectRepository.manageProjectInfra(infraObject); const entity = response.data; const updatedProject = { ...project }; // Handle the building data if (entity.building) { const { id, name, description } = entity.building; - const updatedBuildings = updatedProject.buildings.map((building) => - building.id === id - ? { ...building, name, description } - : building + const updatedBuildings = updatedProject?.buildings?.map((building) => + building.id === id ? { ...building, name, description } : building ); // Add building if it doesn't exist @@ -87,107 +76,111 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = id: id, name, description, - floor: [], + floors: [], }); } updatedProject.buildings = updatedBuildings; // Update the cache for buildings - cacheData( "projectInfo", {projectId: updatedProject.id, data: updatedProject} ); - setProject(updatedProject) + cacheData("projectInfo", { + projectId: updatedProject.id, + data: updatedProject, + }); + setProject((prevProject) => ({ + ...prevProject, + buildings: updatedBuildings, + })); } // Handle the floor data - else if ( entity.floor ) - { + else if (entity.floor) { const { buildingId, id, floorName } = entity.floor; - const updatedBuildings = updatedProject.buildings.map((building) => + const updatedBuildings = updatedProject?.buildings?.map((building) => building.id == buildingId ? { - ...building, - floors: building.floors.map( ( floor ) => - - floor.id === id - ? { - ...floor, - floorName, // Update the floor name only - // Keep other properties as they are (including workArea) - } - : floor - ) - // Add the new floor if it doesn't already exist - .concat( - !building.floors.some((floor) => floor.id === id) - ? [{ id: id, floorName, workArea: null }] // New floor added with workArea set to null - : [] - ), - } + ...building, + floors: building.floors + .map((floor) => + floor.id === id + ? { + ...floor, + floorName, // Update the floor name only + // Keep other properties as they are (including workArea) + } + : floor + ) + // Add the new floor if it doesn't already exist + .concat( + !building.floors.some((floor) => floor.id === id) + ? [{ id: id, floorName, workAreas: [] }] // New floor added with workArea set to null + : [] + ), + } : building ); updatedProject.buildings = updatedBuildings; // Cache the updated project - cacheData( "projectInfo", {projectId: updatedProject.id, data: updatedProject} ); - setProject(updatedProject) + cacheData("projectInfo", { + projectId: updatedProject.id, + data: updatedProject, + }); + setProject(updatedProject); } // Handle the work area data - else if ( entity.workArea ) - { - - - let buildingId = infraObject[0].workArea.buildingId + else if (entity.workArea) { + let buildingId = infraObject[0].workArea.buildingId; const { floorId, areaName, id } = entity.workArea; // Check if the workArea exists, otherwise create a new one const updatedBuildings = updatedProject.buildings.map((building) => building.id == buildingId ? { - ...building, - floors: building.floors.map((floor) => - floor.id == floorId - ? { - ...floor, - workAreas: floor.workAreas.some((workArea) => workArea.id === id) - ? floor.workAreas.map((workArea) => - workArea.id === id - ? { ...workArea, areaName } - : workArea - ) - : [ - ...floor.workAreas, - { id, areaName, workItems: null }, - ], - } - : floor - ), - } + ...building, + floors: building.floors.map((floor) => + floor.id == floorId + ? { + ...floor, + workAreas: floor.workAreas.some( + (workArea) => workArea.id === id + ) + ? floor.workAreas.map((workArea) => + workArea.id === id + ? { ...workArea, areaName } + : workArea + ) + : [ + ...floor.workAreas, + { id, areaName, workItems: [] }, + ], + } + : floor + ), + } : building ); updatedProject.buildings = updatedBuildings; // Update the cache for work areas - cacheData( "projectInfo", {projectId: updatedProject.id, data: updatedProject} ); - setProject(updatedProject) + cacheData("projectInfo", { + projectId: updatedProject.id, + data: updatedProject, + }); + setProject(updatedProject); } // Handle the task (workItem) data - else { console.error("Unsupported data type for submitData", entity); } - } catch ( Err ) - { - showToast("Somthing wrong","error") + } catch (Err) { + console.log(Err); + showToast("Somthing wrong", "error"); } - - - - - handleClose() + handleClose(); }; - const closeBuildingModel = () => { setIsBuildingModalOpen(false); }; @@ -206,7 +199,6 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = }; const handleFloorModelFormSubmit = (updatedFloor) => { if (updatedFloor.id == "") delete updatedFloor.id; - submitData([ { building: null, @@ -222,11 +214,9 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = const closeWorkAreaModel = () => { setIsWorkAreaModalOpen(false); - }; const handleWorkAreaModelFormSubmit = (updatedModel) => { if (updatedModel.id == "") delete updatedModel.id; - submitData([ { building: null, @@ -242,11 +232,10 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = const closeTaskModel = () => { setIsTaskModalOpen(false); - }; - const handleTaskModelFormSubmit = ( updatedModel ) => - { + const handleTaskModelFormSubmit = (updatedModel) => { + // debugger if (updatedModel.id == "") updatedModel.id = 0; const updatedProject = { ...project }; @@ -254,47 +243,49 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = .then((response) => { onDataChange("task-change"); showToast("Details updated successfully.", "success"); - // setClearFormTrigger( true ); - + // setClearFormTrigger( true ); if (response?.data[0]) { - const { workItemId,workItem} = response.data[0]; + const { workItemId, workItem } = response.data[0]; + const updatedBuildings = updatedProject.buildings.map((building) => building.id == updatedModel.buildingID ? { - ...building, - floors: building.floors.map((floor) => - floor.id == updatedModel.floorId - ? { - ...floor, - workAreas: floor.workAreas.map((workArea) => - workArea.id === workItem?.workAreaId - ? { - ...workArea, - workItems: workArea.workItems.some((existingItem) => existingItem.workItemId === workItem.workItemId) - ? workArea.workItems // If the workItemId already exists, keep the current workItems - : [...workArea.workItems, workItem], - } - : workArea - ), - } - : floor - ), - } + ...building, + floors: building.floors.map((floor) => + floor.id == updatedModel.floorId + ? { + ...floor, + workAreas: floor.workAreas.map((workArea) => + workArea.id === workItem?.workAreaId + ? { + ...workArea, + workItems: workArea.workItems.some( + (existingItem) => + existingItem.workItemId === + workItem.workItemId + ) + ? workArea.workItems // If the workItemId already exists, keep the current workItems + : [...workArea.workItems, workItem], + } + : workArea + ), + } + : floor + ), + } : building ); updatedProject.buildings = updatedBuildings; - - cacheData( "projectInfo", {projectId: updatedProject.id, data: updatedProject} ); - setProject(updatedProject) + setProject(updatedProject); + cacheData("projectInfo", { + projectId: updatedProject.id, + data: updatedProject, + }); + console.log(project); } - - - - - }) .catch((error) => { showToast(error.message, "error"); @@ -307,55 +298,53 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = ); }; - const handleModalData = (type,modaldata)=>{ - setModalConfig({type:type,data:modaldata}) - } + const handleModalData = (type, modaldata) => { + setModalConfig({ type: type, data: modaldata }); + }; const openModal = () => { - const modalElement = document.getElementById('building-model'); + const modalElement = document.getElementById("building-model"); const modal = new Modal(modalElement, { backdrop: false, keyboard: true, - focus: true + focus: true, }); - modal.show() + modal.show(); }; const closeModal = () => { setIsModalOpen(false); - setModalConfig(null) + setModalConfig(null); - - const modalElement = document.getElementById('building-model'); + const modalElement = document.getElementById("building-model"); if (modalElement) { - modalElement.classList.remove('show'); // Remove modal visibility class - modalElement.style.display = 'none'; // Hide the modal element + modalElement.classList.remove("show"); // Remove modal visibility class + modalElement.style.display = "none"; // Hide the modal element } - document.body.classList.remove('modal-open'); // Remove modal-open class from body + document.body.classList.remove("modal-open"); // Remove modal-open class from body // Remove the modal backdrop - const backdropElement = document.querySelector('.modal-backdrop'); + const backdropElement = document.querySelector(".modal-backdrop"); if (backdropElement) { - backdropElement.classList.remove('modal-backdrop'); // Remove backdrop class - backdropElement.style.display = 'none'; // Hide the backdrop element + backdropElement.classList.remove("modal-backdrop"); // Remove backdrop class + backdropElement.style.display = "none"; // Hide the backdrop element } - document.body.style.overflow = 'auto'; - + document.body.style.overflow = "auto"; }; const handleShow = () => setShowModal(true); - const handleClose = () => setShowModal( false ); + const handleClose = () => setShowModal(false); return ( <>
)} - {isModalOpen && ( - + )}
-
-
+
- -
- + {loading &&

Loading....

} + {project && project.buildings?.length > 0 && ( + + )}
diff --git a/src/hooks/useProjects.js b/src/hooks/useProjects.js index 3e6e1e7e..037aba94 100644 --- a/src/hooks/useProjects.js +++ b/src/hooks/useProjects.js @@ -91,14 +91,15 @@ export const useProjectDetails =(projectId)=>{ const [error, setError] = useState(""); const fetchData = async () => { - setLoading(true) + setLoading( true ) + const project_cache = getCachedData("projectInfo"); - if (!project_cache || project_cache?.projectId !== projectId) { + if (!project_cache || project_cache?.projectId != projectId) { ProjectRepository.getProjectByprojectId(projectId) .then( ( response ) => { setProject_Details( response.data ); - cacheData("projectInfo", {projectId,data: response.data} ); + cacheData("projectInfo", {projectId:projectId,data: response.data} ); setLoading(false) }) .catch((error) => { diff --git a/src/pages/Activities/AttendancePage.jsx b/src/pages/Activities/AttendancePage.jsx index aeb12d07..fa8f533a 100644 --- a/src/pages/Activities/AttendancePage.jsx +++ b/src/pages/Activities/AttendancePage.jsx @@ -25,7 +25,6 @@ const AttendancePage = () => { const { projects, loading: projectLoading } = useProjects(); const {attendance, loading: attLoading} = useAttendace( selectedProject ); const [ attendances, setAttendances ] = useState(); - const [empRoles, setEmpRoles] = useState(null); const [isCreateModalOpen, setIsCreateModalOpen] = useState(false); const [modelConfig, setModelConfig] = useState(); @@ -71,7 +70,7 @@ const AttendancePage = () => { const handleSubmit = ( formData ) => { - debugger + dispatch( markCurrentAttendance( formData ) ).then( ( action ) => { const updatedAttendance = attendances.map(item => @@ -146,7 +145,7 @@ const AttendancePage = () => { > {!projectLoading && projects?.filter(project => loginUser?.projects?.map(Number).includes(project.id)).map((project)=>( - + ))} {projectLoading && } @@ -179,7 +178,6 @@ const AttendancePage = () => { Logs -
  • { + const {profile: loginUser} = useProfile(); const [showModal, setShowModal] = useState(false); const {projects, loading, error, refetch} = useProjects(); diff --git a/src/utils/axiosClient.jsx b/src/utils/axiosClient.jsx index 7af41fbe..04bf60d8 100644 --- a/src/utils/axiosClient.jsx +++ b/src/utils/axiosClient.jsx @@ -35,7 +35,7 @@ axiosClient.interceptors.response.use( (response) => response, async (error) => { - debugger; + const originalRequest = error.config; if (!originalRequest) {