From 4fc6a5c9f3c55f129660ab9f86f96adf254f67f4 Mon Sep 17 00:00:00 2001 From: pramod mahajan Date: Mon, 14 Jul 2025 20:18:36 +0530 Subject: [PATCH] dynamically update planned and completed work in ProjectInfra and project list card on activity create/edit/delete --- .../Infrastructure/EditActivityModal.jsx | 12 +- .../Project/Infrastructure/TaskModel.jsx | 13 +- .../Project/Infrastructure/WorkItem.jsx | 77 +-- src/hooks/useProjects.js | 478 ++++++++++++------ 4 files changed, 376 insertions(+), 204 deletions(-) diff --git a/src/components/Project/Infrastructure/EditActivityModal.jsx b/src/components/Project/Infrastructure/EditActivityModal.jsx index 06a6e41e..14b64b53 100644 --- a/src/components/Project/Infrastructure/EditActivityModal.jsx +++ b/src/components/Project/Infrastructure/EditActivityModal.jsx @@ -116,7 +116,17 @@ useEffect(() => { floorId: floor?.id, workAreaId: workArea?.id, }; - UpdateTask([payload]) + let plannedTask = + workItem?.workItem?.plannedWork || workItem?.plannedWork || 0; + let completedTask = workItem?.workItem?.completedWork || workItem?.completedWork || 0 + UpdateTask({ + payload: [payload], + PreviousPlannedWork: plannedTask, + buildingId: building?.id, + floorId: floor?.id, + workAreaId: workArea?.id, + previousCompletedWork:completedTask + }); } return (
diff --git a/src/components/Project/Infrastructure/TaskModel.jsx b/src/components/Project/Infrastructure/TaskModel.jsx index 12cf8c87..06261603 100644 --- a/src/components/Project/Infrastructure/TaskModel.jsx +++ b/src/components/Project/Infrastructure/TaskModel.jsx @@ -69,10 +69,9 @@ const TaskModel = ({ project, onSubmit, onClose }) => { const selectedCategory = categoryData?.find((c) => c.id === watchCategoryId); const { mutate: CreateTask, isPending } = useManageTask({ - onSuccessCallback: ( response ) => - { - showToast( response?.message, "success" ) - onClose?.() + onSuccessCallback: (response) => { + showToast(response?.message, "success"); + onClose?.(); }, }); useEffect(() => { @@ -96,8 +95,10 @@ const TaskModel = ({ project, onSubmit, onClose }) => { }, [categories]); const onSubmitForm = async (data) => { - const payload = [data]; - CreateTask(payload); + const payload = [data]; + CreateTask({payload:payload,buildingId: data.buildingID, + floorId: data.floorId, + workAreaId: data.workAreaId, PreviousPlannedWork:0,previousCompletedWork:0}); }; return ( diff --git a/src/components/Project/Infrastructure/WorkItem.jsx b/src/components/Project/Infrastructure/WorkItem.jsx index 1116991c..df1f2bc1 100644 --- a/src/components/Project/Infrastructure/WorkItem.jsx +++ b/src/components/Project/Infrastructure/WorkItem.jsx @@ -10,7 +10,10 @@ import { } from "../../../utils/constants"; import ConfirmModal from "../../common/ConfirmModal"; import ProjectRepository from "../../../repositories/ProjectRepository"; -import { useDeleteProjectTask, useProjectDetails } from "../../../hooks/useProjects"; +import { + useDeleteProjectTask, + useProjectDetails, +} from "../../../hooks/useProjects"; import showToast from "../../../services/toastService"; import { cacheData, @@ -19,7 +22,7 @@ import { } from "../../../slices/apiDataManager"; import { refreshData } from "../../../slices/localVariablesSlice"; import GlobalModel from "../../common/GlobalModel"; -import {useDeleteMasterItem} from "../../../hooks/masterHook/useMaster"; +import { useDeleteMasterItem } from "../../../hooks/masterHook/useMaster"; const WorkItem = ({ workItem, @@ -40,19 +43,18 @@ const WorkItem = ({ const [loadingDelete, setLoadingDelete] = useState(false); const project = getCachedData("projectInfo"); - const openModal = () => setIsModalOpen(true); - const closeModal = () => setIsModalOpen( false ); - - const showModalDelete = () => setShowModal2(true); + const closeModal = () => setIsModalOpen(false); + + const showModalDelete = () => setShowModal2(true); const closeModalDelete = () => setShowModal2(false); const getProgress = (planned, completed) => { return (completed * 100) / planned + "%"; }; - - const {mutate:DeleteTask,isPending } = useDeleteProjectTask(() => { - closeModalDelete?.(); - }); + + const { mutate: DeleteTask, isPending } = useDeleteProjectTask(() => { + closeModalDelete?.(); + }); const handleAssignTask = () => { setItemName(""); @@ -61,15 +63,15 @@ const WorkItem = ({ setNewWorkItem(workItem); }, [workItem]); - const refreshWorkItem = (plannedTask) =>{ + const refreshWorkItem = (plannedTask) => { if (workItem) { - const updated = { - ...workItem, - todaysAssigned: (workItem.todaysAssigned || 0) + plannedTask, - }; - setNewWorkItem(updated); - } - } + const updated = { + ...workItem, + todaysAssigned: (workItem.todaysAssigned || 0) + plannedTask, + }; + setNewWorkItem(updated); + } + }; let assigndata = { building: forBuilding, floor: forFloor, @@ -85,15 +87,17 @@ const WorkItem = ({ tooltipTriggerList.forEach((el) => new bootstrap.Tooltip(el)); }, []); - - - - - const handleSubmit = async () => { let WorkItemId = workItem.workItemId || workItem.id; - DeleteTask({workItemId:WorkItemId,workAreaId:forWorkArea?.id}) - + debugger + DeleteTask({ + workItemId: WorkItemId, + workAreaId: forWorkArea?.id, + completedTask: workItem?.completedWork, + plannedTask: workItem?.plannedWork, + buildingId: forBuilding?.id, + floorId: forFloor?.id, + }); }; const PlannedWork = @@ -104,18 +108,26 @@ const WorkItem = ({ <> {isModalOpen && ( - + )} {showModal && ( - setShowModal(false)}> + setShowModal(false)} + > setShowModal(false)} + onClose={() => setShowModal(false)} /> )} @@ -167,7 +179,6 @@ const WorkItem = ({ : "NA"} - {hasWorkItem @@ -195,9 +206,7 @@ const WorkItem = ({ {hasWorkItem ? `${ - NewWorkItem?.todaysAssigned ?? - workItem?.todaysAssigned ?? - "0" + NewWorkItem?.todaysAssigned ?? workItem?.todaysAssigned ?? "0" }` : "NA"} @@ -251,7 +260,7 @@ const WorkItem = ({ setShowModal(true)} + onClick={() => setShowModal(true)} role="button" > setShowModal(true) } + onClick={() => setShowModal(true)} > Edit diff --git a/src/hooks/useProjects.js b/src/hooks/useProjects.js index 2cc5ee1d..daf705fa 100644 --- a/src/hooks/useProjects.js +++ b/src/hooks/useProjects.js @@ -6,7 +6,12 @@ import { useDispatch, useSelector } from "react-redux"; import { setProjectId } from "../slices/localVariablesSlice"; import EmployeeList from "../components/Directory/EmployeeList"; import eventBus from "../services/eventBus"; -import {Mutation, useMutation, useQuery, useQueryClient} from "@tanstack/react-query"; +import { + Mutation, + useMutation, + useQuery, + useQueryClient, +} from "@tanstack/react-query"; import showToast from "../services/toastService"; // export const useProjects = () => { @@ -211,7 +216,7 @@ export const useProjects = () => { error, refetch, } = useQuery({ - queryKey: ['ProjectsList'], + queryKey: ["ProjectsList"], queryFn: async () => { const response = await ProjectRepository.getProjectList(); return response.data; @@ -227,97 +232,112 @@ export const useProjects = () => { }; }; -export const useEmployeesByProjectAllocated = (selectedProject) => -{ - const {data = [], isLoading, refetch, error} = useQuery( { - queryKey: ["empListByProjectAllocated", selectedProject ], - queryFn: async () => - { - const res = await ProjectRepository.getProjectAllocation( selectedProject ); - return res.data || res +export const useEmployeesByProjectAllocated = (selectedProject) => { + const { + data = [], + isLoading, + refetch, + error, + } = useQuery({ + queryKey: ["empListByProjectAllocated", selectedProject], + queryFn: async () => { + const res = await ProjectRepository.getProjectAllocation(selectedProject); + return res.data || res; }, enabled: !!selectedProject, - onError: ( error ) => - { - showToast(error.message || "Error while Fetching project Allocated Employees", "error"); - } - } ) - + onError: (error) => { + showToast( + error.message || "Error while Fetching project Allocated Employees", + "error" + ); + }, + }); + return { projectEmployees: data, - loading:isLoading, + loading: isLoading, error, - refetch - } -} + refetch, + }; +}; -export const useProjectDetails = ( projectId,isAuto = true ) => -{ - const {data: projects_Details, isLoading, error, refetch} = useQuery( { - queryKey: [ "projectInfo", projectId ], - queryFn: async () => - { - const res = await ProjectRepository.getProjectByprojectId( projectId ); +export const useProjectDetails = (projectId, isAuto = true) => { + const { + data: projects_Details, + isLoading, + error, + refetch, + } = useQuery({ + queryKey: ["projectInfo", projectId], + queryFn: async () => { + const res = await ProjectRepository.getProjectByprojectId(projectId); return res.data || res; }, enabled: !!projectId && isAuto, - onError: ( error ) => - { - showToast(error.message || "Error while Fetching project Details", "error"); - } - } ) - return { projects_Details, loading:isLoading, error, refetch }; -} + onError: (error) => { + showToast( + error.message || "Error while Fetching project Details", + "error" + ); + }, + }); + return { projects_Details, loading: isLoading, error, refetch }; +}; -export const useProjectsByEmployee = (employeeId) => -{ - const { data:projectNameList =[],isLoading,error,refetch} = useQuery( { - queryKey: [ "ProjectsByEmployee", employeeId ], - queryFn: async () => - { - const res = await ProjectRepository.getProjectsByEmployee( employeeId ); +export const useProjectsByEmployee = (employeeId) => { + const { + data: projectNameList = [], + isLoading, + error, + refetch, + } = useQuery({ + queryKey: ["ProjectsByEmployee", employeeId], + queryFn: async () => { + const res = await ProjectRepository.getProjectsByEmployee(employeeId); return res.data || res; }, enabled: !!employeeId, - onError: ( error ) => - { - showToast(error.message || "Error while Fetching project Employee", "error"); - } - }) - return {projectList, loading:isLoading,error,refetch } -} + onError: (error) => { + showToast( + error.message || "Error while Fetching project Employee", + "error" + ); + }, + }); + return { projectList, loading: isLoading, error, refetch }; +}; -export const useProjectName = () => -{ - const {data = [],isLoading,error,refetch} = useQuery( { - queryKey: [ "basicProjectNameList" ], - queryFn: async () => - { +export const useProjectName = () => { + const { + data = [], + isLoading, + error, + refetch, + } = useQuery({ + queryKey: ["basicProjectNameList"], + queryFn: async () => { const res = await ProjectRepository.projectNameList(); return res.data || res; }, - onError: ( error ) => - { - showToast(error.message || "Error while Fetching project Name", "error"); - } - } ) - return {projectNames:data,loading:isLoading,Error:error,refetch} -} + onError: (error) => { + showToast(error.message || "Error while Fetching project Name", "error"); + }, + }); + return { projectNames: data, loading: isLoading, Error: error, refetch }; +}; - - export const useProjectInfra = (projectId) => { const { data: projectInfra, isLoading, error, } = useQuery({ - queryKey: ["ProjectInfra", projectId], + queryKey: ["ProjectInfra", projectId], queryFn: async () => { const res = await ProjectRepository.getProjectInfraByproject(projectId); return res.data; }, - enabled: !!projectId , + enabled: !!projectId, onError: (error) => { showToast(error.message || "Error while fetching project infra", "error"); }, @@ -326,30 +346,27 @@ export const useProjectInfra = (projectId) => { return { projectInfra, isLoading, error }; }; - -export const useProjectTasks = (workAreaId,IsExpandedArea=false) => -{ - const { data:ProjectTaskList,isLoading,error } = useQuery( { - queryKey: [ "WorkItems",workAreaId ], - queryFn: async () => - { +export const useProjectTasks = (workAreaId, IsExpandedArea = false) => { + const { + data: ProjectTaskList, + isLoading, + error, + } = useQuery({ + queryKey: ["WorkItems", workAreaId], + queryFn: async () => { const res = await ProjectRepository.getProjectTasksByWorkArea(workAreaId); return res.data; }, enabled: !!workAreaId && !!IsExpandedArea, - onError: ( error ) => - { - showToast(error.message || "Error while Fetching project Tasks", "error"); - } - } ) - return {ProjectTaskList,isLoading,error} -} - + onError: (error) => { + showToast(error.message || "Error while Fetching project Tasks", "error"); + }, + }); + return { ProjectTaskList, isLoading, error }; +}; // -- -------------Mutation------------------------------- - - export const useCreateProject = ({ onSuccessCallback }) => { const queryClient = useQueryClient(); @@ -360,8 +377,8 @@ export const useCreateProject = ({ onSuccessCallback }) => { }, onSuccess: (data) => { // Invalidate the cache - queryClient.invalidateQueries( {queryKey: [ 'ProjectsList' ]} ); - queryClient.invalidateQueries({queryKey:['basicProjectNameList']}); + queryClient.invalidateQueries({ queryKey: ["ProjectsList"] }); + queryClient.invalidateQueries({ queryKey: ["basicProjectNameList"] }); // Emit event for consumers (like useProjects or others) eventBus.emit("project", { @@ -381,27 +398,19 @@ export const useCreateProject = ({ onSuccessCallback }) => { }); }; - export const useUpdateProject = ({ onSuccessCallback }) => { const queryClient = useQueryClient(); - const { - mutate, - isPending, - isSuccess, - isError, - } = useMutation({ - mutationFn: async ( {projectId, updatedData} ) => - { + const { mutate, isPending, isSuccess, isError } = useMutation({ + mutationFn: async ({ projectId, updatedData }) => { return await ProjectRepository.updateProject(projectId, updatedData); }, - onSuccess: ( data, variables ) => - { + onSuccess: (data, variables) => { const { projectId } = variables; - - queryClient.invalidateQueries({queryKey:["ProjectsList"]}); - queryClient.invalidateQueries( {queryKey: [ "projectInfo", projectId ]} ); - queryClient.invalidateQueries({queryKey:['basicProjectNameList']}); + + queryClient.invalidateQueries({ queryKey: ["ProjectsList"] }); + queryClient.invalidateQueries({ queryKey: ["projectInfo", projectId] }); + queryClient.invalidateQueries({ queryKey: ["basicProjectNameList"] }); eventBus.emit("project", { keyword: "Update_Project", @@ -415,9 +424,8 @@ export const useUpdateProject = ({ onSuccessCallback }) => { } }, - onError: ( error ) => - { - console.log(error) + onError: (error) => { + console.log(error); showToast(error?.message || "Error while updating project", "error"); }, }); @@ -430,20 +438,16 @@ export const useUpdateProject = ({ onSuccessCallback }) => { }; }; - -export const useManageProjectInfra = ( {onSuccessCallback} ) => -{ +export const useManageProjectInfra = ({ onSuccessCallback }) => { const queryClient = useQueryClient(); return useMutation({ - mutationFn: async ( {infraObject, projectId} ) => - { + mutationFn: async ({ infraObject, projectId }) => { return await ProjectRepository.manageProjectInfra(infraObject); }, - onSuccess: ( data, variables ) => - { - const { projectId } = variables; - queryClient.invalidateQueries({queryKey:["ProjectInfra", projectId]}); - if (onSuccessCallback) onSuccessCallback(data,variables); + onSuccess: (data, variables) => { + const { projectId } = variables; + queryClient.invalidateQueries({ queryKey: ["ProjectInfra", projectId] }); + if (onSuccessCallback) onSuccessCallback(data, variables); }, onError: (error) => { showToast(error.message || "Failed to update Project Infra", "error"); @@ -451,39 +455,36 @@ export const useManageProjectInfra = ( {onSuccessCallback} ) => }); }; - export const useManageProjectAllocation = ({ onSuccessCallback, onErrorCallback, }) => { const queryClient = useQueryClient(); - const { - mutate, - isPending, - isSuccess, - isError, - } = useMutation({ - mutationFn: async ( {items} ) => - { + const { mutate, isPending, isSuccess, isError } = useMutation({ + mutationFn: async ({ items }) => { const response = await ProjectRepository.manageProjectAllocation(items); return response.data; }, onSuccess: (data, variables, context) => { - queryClient.invalidateQueries({queryKey:['empListByProjectAllocated']}); - queryClient.removeQueries({queryKey:["projectEmployees"]}) + queryClient.invalidateQueries({ + queryKey: ["empListByProjectAllocated"], + }); + queryClient.removeQueries({ queryKey: ["projectEmployees"] }); if (variables?.added) { - showToast('Employee Assigned Successfully', 'success'); + showToast("Employee Assigned Successfully", "success"); } else { - showToast('Removed Employee Successfully', 'success'); + showToast("Removed Employee Successfully", "success"); } if (onSuccessCallback) onSuccessCallback(data, context); }, onError: (error) => { const message = - error?.response?.data?.message || error.message || 'Error occurred during API call'; - showToast(message, 'error'); + error?.response?.data?.message || + error.message || + "Error occurred during API call"; + showToast(message, "error"); if (onErrorCallback) onErrorCallback(error); }, }); @@ -496,48 +497,199 @@ export const useManageProjectAllocation = ({ }; }; -export const useManageTask = ({onSuccessCallback}) => -{ +export const useManageTask = ({ onSuccessCallback }) => { const queryClient = useQueryClient(); + const selectedProject = useSelector( + (store) => store.localVariables.projectId + ); + return useMutation({ + mutationFn: async ({ + payload, + PreviousPlannedWork, + previousCompletedWork, + buildingId, + floorId, + workAreaId, + }) => await ProjectRepository.manageProjectTasks(payload), + onSuccess: (data, variables) => { + const { + workAreaId, + buildingId, + floorId, + PreviousPlannedWork, + previousCompletedWork, + } = variables; + queryClient.invalidateQueries({ queryKey: ["WorkItems"] }); + const getPlannedDelta = (previous, current) => current - previous; + queryClient.setQueryData(["ProjectInfra", selectedProject], (oldData) => { + if (!oldData) return oldData; + const { workAreaId, floorId, buildingId } = variables; + const updatedData = JSON.parse(JSON.stringify(oldData)); + return updatedData.map((building) => { + if (building.id !== buildingId) return building; + return { + ...building, + floors: building.floors.map((floor) => { + if (floor.id !== floorId) return floor; - return useMutation( { - mutationFn: async ( payload ) => await ProjectRepository.manageProjectTasks( payload ), - onSuccess: ( data, variables ) => - { - queryClient.invalidateQueries({ queryKey: ["WorkItems"] }) + return { + ...floor, + workAreas: floor.workAreas.map((area) => { + if (area.id !== workAreaId) return area; + + const previousPlanned = PreviousPlannedWork ?? 0; + const currentPlanned = + data?.data[0]?.workItem.plannedWork ?? 0; + + const plannedWorkDelta = getPlannedDelta( + previousPlanned, + currentPlanned + ); + const updatedPlannedWork = + (area.plannedWork ?? 0) + plannedWorkDelta; + const previousCompleted = previousCompletedWork; + + const currentCompleted = + data?.data[0]?.workItem.completedWork ?? 0; + const completedWorkDelta = getPlannedDelta( + previousCompleted, + currentCompleted + ); + const updatedCompletedWork = + (area.completedWork ?? 0) + completedWorkDelta; + return { + ...area, + plannedWork: updatedPlannedWork, + completedWork: updatedCompletedWork, + }; + }), + }; + }), + }; + }); + }); + + queryClient.setQueryData(["ProjectsList"], (projects) => { + if (!projects) return projects; + + return projects.map((project) => { + if (project.id !== selectedProject) return project; + + const previousPlanned = PreviousPlannedWork ?? 0; + const currentPlanned = data?.data[0]?.workItem.plannedWork ?? 0; + + const plannedWorkDelta = getPlannedDelta( + previousPlanned, + currentPlanned + ); + const updatedPlannedWork = + (project.plannedWork ?? 0) + plannedWorkDelta; + const previousCompleted = previousCompletedWork; + const currentCompleted = data?.data[0]?.workItem.completedWork ?? 0; + const completedWorkDelta = getPlannedDelta( + previousCompleted, + currentCompleted + ); + const updatedCompletedWork = + (project.completedWork ?? 0) + completedWorkDelta; + return { + ...project, + plannedWork: updatedPlannedWork, + completedWork: updatedCompletedWork, + }; + }); + }); if (onSuccessCallback) onSuccessCallback(data); }, - onError: (error) => - { - const message = - error?.response?.data?.message || error.message || 'Error occurred during API call'; - showToast(message, 'error'); - } - - }) -} - -export const useDeleteProjectTask = (onSuccessCallback) => { - const queryClient = useQueryClient(); - - return useMutation({ - mutationFn: async ( {workItemId, workAreaId} ) => - { - return await ProjectRepository.deleteProjectTask(workItemId); - }, - onSuccess: ( _, variables ) => - { - showToast("Task deleted successfully", "success"); - queryClient.invalidateQueries({queryKey:[ "WorkItems",variables.workAreaId]}); - if (onSuccessCallback) onSuccessCallback(); - }, onError: (error) => { - showToast( - error?.response?.data?.message || error.message || "Failed to delete task", - "error" - ); - if (onSuccessCallback) onSuccessCallback(); + const message = + error?.response?.data?.message || + error.message || + "Error occurred during API call"; + showToast(message, "error"); }, }); }; +export const useDeleteProjectTask = (onSuccessCallback) => { + const queryClient = useQueryClient(); + const selectedProject = useSelector( + (store) => store.localVariables.projectId + ); + return useMutation({ + mutationFn: async ({ + workItemId, + workAreaId, + completedTask, + plannedTask, + }) => { + return await ProjectRepository.deleteProjectTask(workItemId); + }, + onSuccess: (_, variables) => { + const { completedTask, plannedTask, workAreaId, buildingId, floorId } = + variables; + + queryClient.invalidateQueries({ + queryKey: ["WorkItems", variables.workAreaId], + }); + + queryClient.setQueryData(["ProjectInfra", selectedProject], (oldData) => { + if (!oldData) return oldData; + + const { workAreaId, floorId, buildingId, plannedTask, completedTask } = + variables; + + const updatedData = JSON.parse(JSON.stringify(oldData)); + + return updatedData.map((building) => { + if (building.id !== buildingId) return building; + + return { + ...building, + floors: building.floors.map((floor) => { + if (floor.id !== floorId) return floor; + + return { + ...floor, + workAreas: floor.workAreas.map((area) => { + if (area.id !== workAreaId) return area; + + return { + ...area, + plannedWork: (area.plannedWork ?? 0) - plannedTask, + completedWork: (area.completedWork ?? 0) - completedTask, + }; + }), + }; + }), + }; + }); + }); + + queryClient.setQueryData(["ProjectsList"], (projects) => { + if (!projects) return projects; + + return projects.map((project) => { + if (project.id !== selectedProject) return project; + + return { + ...project, + plannedWork: (project.plannedWork ?? 0) - plannedTask, + completedWork: (project.completedWork ?? 0) - completedTask, + }; + }); + }); + showToast("Task deleted successfully", "success"); + if (onSuccessCallback) onSuccessCallback(); + }, + onError: (error) => { + showToast( + error?.response?.data?.message || + error.message || + "Failed to delete task", + "error" + ); + if (onSuccessCallback) onSuccessCallback(); + }, + }); +};