From 93c95e007a19cffa75cbe741a8c1a05b5ba23fba Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Mon, 7 Apr 2025 17:14:07 +0530 Subject: [PATCH] integrated create Task api and handling data caching updated for task. --- .../Project/Infrastructure/BuildingModel.jsx | 15 +- .../Project/Infrastructure/TaskModel.jsx | 259 +++++++++--------- .../Project/Infrastructure/WorkArea.jsx | 98 ++++--- .../Project/Infrastructure/WorkItem.jsx | 200 +++++++++----- src/components/Project/ProjectInfra.jsx | 85 +++--- src/repositories/ProjectRepository.jsx | 2 +- 6 files changed, 375 insertions(+), 284 deletions(-) diff --git a/src/components/Project/Infrastructure/BuildingModel.jsx b/src/components/Project/Infrastructure/BuildingModel.jsx index 59f3abde..f7169b2f 100644 --- a/src/components/Project/Infrastructure/BuildingModel.jsx +++ b/src/components/Project/Infrastructure/BuildingModel.jsx @@ -37,9 +37,14 @@ const BuildingModel = ({ } else if (editingBuilding) { setFormData({ ...editingBuilding, projectId: project.id }); } + + return () => + { + setValue("name",null) + } }, [clearTrigger, onClearComplete, editingBuilding, project.id]); - const { register, handleSubmit, formState: { errors }, setValue } = useForm({ + const { register, handleSubmit, formState: { errors }, setValue,reset,getValues} = useForm({ resolver: zodResolver(buildingSchema), defaultValues: formData, // Set default values from formData state }); @@ -59,7 +64,11 @@ const BuildingModel = ({ const onSubmitHandler = async( data ) => { - onSubmit({ ...data, projectId: project.id }); + onSubmit( {...data, projectId: project.id} ); + reset( { + name: null, + description:null + }) }; return ( @@ -116,7 +125,7 @@ const BuildingModel = ({
- )} + +
diff --git a/src/components/Project/Infrastructure/WorkArea.jsx b/src/components/Project/Infrastructure/WorkArea.jsx index 96ba4310..479b56ed 100644 --- a/src/components/Project/Infrastructure/WorkArea.jsx +++ b/src/components/Project/Infrastructure/WorkArea.jsx @@ -1,46 +1,62 @@ -import React from "react"; +import React,{useEffect} from "react"; import WorkItem from "./WorkItem"; -const WorkArea = ({ workArea, floor,forBuilding }) => { - return ( - - - -
-
-
- - {floor.floorName} - {workArea.areaName}   - -
-
+const WorkArea = ( {workArea, floor, forBuilding} ) => +{ + useEffect(() => { + + }, [workArea]); + return ( + + + +
+
+
+ + {floor.floorName} - {workArea.areaName}   + +
+
+ + + + {workArea?.workItems && workArea.workItems.length > 0 ? ( + + + + + + + {/* for mobile view */} + + {/* for greather than mobile view ************* */} + + + {/* ************************** */} + + + + + + {workArea?.workItems && workArea.workItems.length > 0 ? ( + workArea.workItems.map((workItem) => ( + + )) + ) : ( + + + + )} + +
ActivityStatusPlannedCompletedProgressActions
+ No Data +
- - {workArea?.workItems && workArea.workItems.length > 0 ? ( - - - - - - - - - - - - - - {workArea.workItems.map((workItem) => ( - - ))} - -
ActivityPlannedCompletedProgressActions
- - - ) : null} -
- ); - }; -export default WorkArea; \ No newline at end of file + ) : null} + + ); +}; +export default WorkArea; diff --git a/src/components/Project/Infrastructure/WorkItem.jsx b/src/components/Project/Infrastructure/WorkItem.jsx index 1a2b21c7..9b2902a7 100644 --- a/src/components/Project/Infrastructure/WorkItem.jsx +++ b/src/components/Project/Infrastructure/WorkItem.jsx @@ -1,15 +1,15 @@ -import React, { useState } from "react"; +import React, { useState,useEffect } from "react"; import { useModal } from "../../../ModalContext"; import AssignRoleModel from "../AssignRole"; -import {useParams} from "react-router-dom"; +import { useParams } from "react-router-dom"; import GlobalModel from "../../common/GlobalModel"; -const WorkItem = ( {workItem, forBuilding, forFloor, forWorkArea} ) =>{ - const {projectId} = useParams() - const [ itemName, setItemName ] = useState( '' ); +const WorkItem = ({ workItem, forBuilding, forFloor, forWorkArea }) => { + const { projectId } = useParams(); + const [ itemName, setItemName ] = useState( "" ); + const [NewWorkItem,setNewWorkItem] = useState() const [isModalOpen, setIsModalOpen] = useState(false); - - + const openModal = () => setIsModalOpen(true); const closeModal = () => setIsModalOpen(false); const getProgress = (planned, completed) => { @@ -17,8 +17,8 @@ const WorkItem = ( {workItem, forBuilding, forFloor, forWorkArea} ) =>{ }; const handleAssignTask = () => { - console.log("Item Created:", itemName); - setItemName(''); + + setItemName(""); }; // const showCreateItemModal = (modalData) => { @@ -27,79 +27,131 @@ const WorkItem = ( {workItem, forBuilding, forFloor, forWorkArea} ) =>{ // handleAssignTask ,"lg" // ); // }; - + + useEffect(() => { + + setNewWorkItem(workItem) + }, [workItem]); // This hook will run whenever the workItem prop changes + let assigndata = { building: forBuilding, floor: forFloor, workArea: forWorkArea, - workItem - } + workItem, + }; + + const hasWorkItem = NewWorkItem && NewWorkItem + return ( <> - - + + - - - - - {workItem.workItem.activityMaster - ? workItem.workItem.activityMaster.activityName - : "NA"} - - - - {workItem.workItem ? workItem.workItem.plannedWork : "NA"} - - - {workItem.workItem ? workItem.workItem.completedWork : "NA"} - - -
-
-
- - -
- {!projectId && ( )} - - -
- + + + + + {hasWorkItem ? ( NewWorkItem?.workItem?.activityMaster?.activityName || workItem.activityMaster?.activityName ) :"NA" + } + + + {/* for mobile view */} + + {hasWorkItem ? (NewWorkItem?.workItem?.completedWork || workItem?.completedWork) :"NA" }/{" "} + { hasWorkItem ? (NewWorkItem?.workItem?.plannedWork || workItem?.plannedWork) : "NA"} + + {/* for greather than mobile view ************* */} + + {hasWorkItem ? (NewWorkItem?.workItem?.plannedWork || workItem?.plannedWork): "NA"} + + + {hasWorkItem ? (NewWorkItem?.workItem?.completedWork || workItem?.completedWork) : "NA"} + + {/* ************************************************ */} + +
+
+
+ + {/* for greather than mobile view */} + +
+ {!projectId && ( + + )} + + +
+ + {/* for mobile view */} + + + - + ); }; diff --git a/src/components/Project/ProjectInfra.jsx b/src/components/Project/ProjectInfra.jsx index 087f176c..351eeead 100644 --- a/src/components/Project/ProjectInfra.jsx +++ b/src/components/Project/ProjectInfra.jsx @@ -64,7 +64,7 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = const submitData = async (infraObject) => { - + try { console.log(infraObject) @@ -134,7 +134,7 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = // Handle the work area data else if ( entity.workArea ) { - debugger + let buildingId = infraObject[0].workArea.buildingId @@ -172,38 +172,6 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = setProject(updatedProject) } // Handle the task (workItem) data - else if (entity.workItem) { - const { buildingId, floorId, workAreaId, name, description } = entity.workItem; - const updatedBuildings = updatedProject.buildings.map((building) => - building.id === buildingId - ? { - ...building, - floors: building.floors.map((floor) => - floor.id === floorId - ? { - ...floor, - workAreas: floor.workAreas.map((workArea) => - workArea.id === workAreaId - ? { - ...workArea, - tasks: workArea.tasks.map((task) => - task.id === entity.workItem.id ? { ...task, name, description } : task - ), - } - : workArea - ), - } - : floor - ), - } - : building - ); - - updatedProject.buildings = updatedBuildings; - - - cacheData("projectInfo", { projectId: updatedProject.id, data: updatedProject }); - } else { console.error("Unsupported data type for submitData", entity); @@ -277,15 +245,56 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = }; - const handleTaskModelFormSubmit = (updatedModel) => { + const handleTaskModelFormSubmit = ( updatedModel ) => + { if (updatedModel.id == "") updatedModel.id = 0; - + const updatedProject = { ...project }; ProjectRepository.manageProjectTasks([updatedModel]) .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 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 + ); + + updatedProject.buildings = updatedBuildings; + + + cacheData( "projectInfo", {projectId: updatedProject.id, data: updatedProject} ); + setProject(updatedProject) + } + + + + + }) .catch((error) => { showToast(error.message, "error"); @@ -456,7 +465,7 @@ const ProjectInfra = ({ data, activityMaster, onDataChange,eachSiteEngineer }) = onClick={() => openTaskModel()} > - Manage Tasks + Create Tasks
diff --git a/src/repositories/ProjectRepository.jsx b/src/repositories/ProjectRepository.jsx index b360f917..a9a4d0b2 100644 --- a/src/repositories/ProjectRepository.jsx +++ b/src/repositories/ProjectRepository.jsx @@ -16,7 +16,7 @@ const ProjectRepository = { manageProjectAllocation: ( data ) => api.post( "/api/project/allocation", data ), manageProjectInfra: (data) => api.post("/api/project/manage-infra", data), - manageProjectTasks: (data) => api.post("/api/project/manage-infra", data), + manageProjectTasks: (data) => api.post("/api/project/task", data), updateProject: (id, data) => api.put(`/api/project/update/${id}`, data), deleteProject: (id) => api.delete(`/projects/${id}`),