diff --git a/src/components/Project/Infrastructure/WorkArea.jsx b/src/components/Project/Infrastructure/WorkArea.jsx index 493312b1..9e796dd6 100644 --- a/src/components/Project/Infrastructure/WorkArea.jsx +++ b/src/components/Project/Infrastructure/WorkArea.jsx @@ -1,22 +1,83 @@ import React, { useEffect, useState } from "react"; import WorkItem from "./WorkItem"; import { useProjectDetails } from "../../../hooks/useProjects"; -import { getCachedData } from "../../../slices/apiDataManager"; +import { cacheData, getCachedData } from "../../../slices/apiDataManager"; +import {useDispatch} from "react-redux"; +import {refreshData} from "../../../slices/localVariablesSlice"; +import ProjectRepository from "../../../repositories/ProjectRepository"; +import showToast from "../../../services/toastService"; const WorkArea = ({ workArea, floor, forBuilding }) => { - const [workItems, setWorkItems] = useState([]); + const [ workItems, setWorkItems ] = useState( [] ); + const dispatch = useDispatch() + const [Project,setProject] = useState() + useEffect(() => { - const project = getCachedData("projectInfo"); + const project = getCachedData( "projectInfo" ); + setProject(project) + if (!project || !forBuilding?.id || !floor?.id || !workArea?.id) return; - const building = project.buildings?.find((b) => b.id === forBuilding.id); const floors = building?.floors?.find((f) => f.id === floor.id); const workAreas = floor?.workAreas?.find((wa) => wa.id === workArea.id); - setWorkItems(workAreas?.workItems || []); - }, [workArea, floor, floor]); + }, [ workArea, floor, floor ] ); + const HanldeDeleteActivity = async (workItemId) => { + + try + { + const updatedProject = { ...Project.data }; + const response = await ProjectRepository.deleteProjectTask( workItemId); + const newProject = { + ...updatedProject, + buildings: updatedProject?.buildings.map((building) => + building?.id === building?.id + ? { + ...building, + floors: building?.floors?.map((floor) => + floor.id === floor?.id + ? { + ...floor, + workAreas: floor.workAreas.map((workArea) => + workArea.id === workArea?.id + ? { + ...workArea, + workItems: workArea.workItems.filter( + (item) => + String(item?.workItem?.id ?? item?.id) !== + String(workItemId) + ), + } + : workArea + ), + } + : floor + ), + } + : building + ), + }; + + cacheData("projectInfo", { + projectId: newProject.id, + data: newProject, + } ); + + dispatch(refreshData(true)); + + + showToast("Activity Deleted Successfully", "success"); + } catch (error) { + const message = + error.response?.data?.message || + error.message || + "An unexpected error occurred"; + showToast(message, "error"); + } + }; + return ( @@ -70,7 +131,7 @@ const WorkArea = ({ workArea, floor, forBuilding }) => { {/* ************************** */} Progress - Actions + Actions @@ -83,6 +144,7 @@ const WorkArea = ({ workArea, floor, forBuilding }) => { forBuilding={forBuilding} forFloor={floor} forWorkArea={workArea} + deleteHandleTask={HanldeDeleteActivity} /> )) ) : ( diff --git a/src/components/Project/Infrastructure/WorkItem.jsx b/src/components/Project/Infrastructure/WorkItem.jsx index 0b050068..965f44f1 100644 --- a/src/components/Project/Infrastructure/WorkItem.jsx +++ b/src/components/Project/Infrastructure/WorkItem.jsx @@ -9,11 +9,22 @@ import ConfirmModal from "../../common/ConfirmModal"; import ProjectRepository from "../../../repositories/ProjectRepository"; import { useProjectDetails } from "../../../hooks/useProjects"; import showToast from "../../../services/toastService"; -import { cacheData, getCachedData } from "../../../slices/apiDataManager"; +import { + cacheData, + clearCacheKey, + getCachedData, +} from "../../../slices/apiDataManager"; import { useDispatch } from "react-redux"; import { refreshData } from "../../../slices/localVariablesSlice"; -const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { +const WorkItem = ( { + key, + workItem, + forBuilding, + forFloor, + forWorkArea, + deleteHandleTask, +}) => { const { projectId } = useParams(); const [itemName, setItemName] = useState(""); const [NewWorkItem, setNewWorkItem] = useState(); @@ -26,10 +37,6 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { const project = getCachedData("projectInfo"); const dispatch = useDispatch(); - const { projects_Details } = useProjectDetails( - projectId || project?.projectId - ); - const openModal = () => setIsModalOpen(true); const closeModal = () => setIsModalOpen(false); const getProgress = (planned, completed) => { @@ -66,62 +73,17 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { const handleSubmit = async () => { setLoadingDelete(true); - try - { - const updatedProject = { ...projects_Details }; - const response = await ProjectRepository.deleteProjectTask( workItem.workItemId || workItem.id); - const newProject = { - ...updatedProject, - buildings: updatedProject.buildings.map((building) => - building.id === forBuilding?.id - ? { - ...building, - floors: building.floors.map((floor) => - floor.id === forFloor?.id - ? { - ...floor, - workAreas: floor.workAreas.map((workArea) => - workArea.id === forWorkArea?.id - ? { - ...workArea, - workItems: workArea.workItems.filter( - (item) => - String(item?.workItem?.id ?? item?.id) !== - String(workItem.workItemId) - ), - } - : workArea - ), - } - : floor - ), - } - : building - ), - }; - - cacheData("projectInfo", { - projectId: newProject.id, - data: newProject, - }); - - dispatch(refreshData(true)); - closeModalDelete(); - setLoadingDelete(false); - showToast("Activity Deleted Successfully", "success"); - } catch (error) { - setLoadingDelete(false); - closeModalDelete(); - const message = - error.response?.data?.message || - error.message || - "An unexpected error occurred"; - showToast(message, "error"); - } + let WorkItemId = workItem.workItemId || workItem.id; + deleteHandleTask( WorkItemId ); + setLoadingDelete(false); + closeModalDelete(); + }; - const PlannedWork = NewWorkItem?.workItem?.plannedWork || workItem?.plannedWork; - const CompletedWork = NewWorkItem?.workItem?.completedWork ?? workItem?.completedWork; + const PlannedWork = + NewWorkItem?.workItem?.plannedWork || workItem?.plannedWork; + const CompletedWork = + NewWorkItem?.workItem?.completedWork ?? workItem?.completedWork; return ( <> {isModalOpen && ( @@ -176,7 +138,7 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { )} - + @@ -236,65 +198,66 @@ const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { > - -
- {/* Reserve space for Assign button */} -
- {(!projectId && ManageTasks) && (PlannedWork !== CompletedWork) ? ( - - ) : ( - // Hidden placeholder to preserve layout - - Assign - - )} -
- - {/* Edit and Delete buttons */} - {ManageInfra && ( - <> - - - - )} -
- - - + +
+ {/* Reserve space for Assign button */} +
+ {!projectId && ManageTasks && PlannedWork !== CompletedWork ? ( + + ) : ( + // Hidden placeholder to preserve layout + + + Assign + + + )} +
+ {/* Edit and Delete buttons */} + {ManageInfra && ( + <> + + + + )} +
+ );