From d2b80b4f02f02fcac7d4493a6f80176e8ba6ef6f Mon Sep 17 00:00:00 2001 From: pramod mahajan Date: Mon, 14 Jul 2025 20:19:25 +0530 Subject: [PATCH] dynamically update planned and completed work in ProjectInfra and project list card on Task reported --- src/components/Activities/ReportTask.jsx | 7 +- src/hooks/useTasks.js | 131 ++++++++++++++--------- 2 files changed, 86 insertions(+), 52 deletions(-) diff --git a/src/components/Activities/ReportTask.jsx b/src/components/Activities/ReportTask.jsx index 8194a5cc..a3550683 100644 --- a/src/components/Activities/ReportTask.jsx +++ b/src/components/Activities/ReportTask.jsx @@ -62,7 +62,12 @@ export const ReportTask = ({ report, closeModal }) => { checkList: [], }; - reportTask({ reportData, workAreaId: report?.workItem?.workArea?.id }); + reportTask({ + reportData, + workAreaId: report?.workItem?.workArea?.id, + buildingId: report?.workItem?.workArea?.floor?.building.id, + floorId: report?.workItem?.workArea?.floor?.id, + }); }; const handleClose = () => { closeModal(); diff --git a/src/hooks/useTasks.js b/src/hooks/useTasks.js index 1cf531c0..b793ced4 100644 --- a/src/hooks/useTasks.js +++ b/src/hooks/useTasks.js @@ -1,11 +1,10 @@ import { useEffect, useState } from "react"; import { TasksRepository } from "../repositories/TaskRepository"; import { cacheData, getCachedData } from "../slices/apiDataManager"; -import {MasterRespository} from "../repositories/MastersRepository"; +import { MasterRespository } from "../repositories/MastersRepository"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import showToast from "../services/toastService"; -import {useSelector} from "react-redux"; - +import { useSelector } from "react-redux"; // ---------Query--------------------------------- @@ -28,7 +27,6 @@ export const useTaskList = (projectId, dateFrom, toDate) => { return response.data; }, enabled, - }); return { TaskList, loading, error, refetch }; @@ -46,7 +44,7 @@ export const useTaskById = (TaskId) => { const res = await TasksRepository.getTaskById(TaskId); return res.data; }, - enabled: !!TaskId, + enabled: !!TaskId, }); return { Task, loading, error, refetch }; @@ -75,47 +73,80 @@ export const useAuditStatus = () => { const res = await MasterRespository.getAuditStatus(); return res.data; }, - }); return { status, loading, error, refetch }; }; - // -----------------------Mutation------------------------ -const toDate = new Date().toISOString().split('T')[0]; -const dateFrom = new Date(Date.now() - 6 * 24 * 60 * 60 * 1000).toISOString().split('T')[0]; +const toDate = new Date().toISOString().split("T")[0]; +const dateFrom = new Date(Date.now() - 6 * 24 * 60 * 60 * 1000) + .toISOString() + .split("T")[0]; - - -export const useReportTask = ( {onSuccessCallback, onErrorCallback} = {} ) => -{ +export const useReportTask = ({ onSuccessCallback, onErrorCallback } = {}) => { const queryClient = useQueryClient(); - const { - mutate, - isPending, - isSuccess, - isError, - error, - } = useMutation({ - mutationFn: async ( {reportData,workAreaId} ) => - { - debugger + const selectedProject = useSelector( + (store) => store.localVariables.projectId + ); + const { mutate, isPending, isSuccess, isError, error } = useMutation({ + mutationFn: async ({ reportData, workAreaId }) => { return await TasksRepository.reportTask(reportData); }, - onSuccess: ( data, variables ) => - { - const {workAreaId} = variables; - queryClient.invalidateQueries( {queryKey: [ "taskList" ]} ); - queryClient.invalidateQueries( {queryKey: [ "WorkItems", workAreaId ]} ); - queryClient.invalidateQueries( {queryKey: [ 'ProjectsList' ]} ); - showToast( "Task Reported Successfully.", "success" ); + onSuccess: (data, variables) => { + const { workAreaId, buildingId, floorId } = variables; + queryClient.invalidateQueries({ queryKey: ["taskList"] }); + queryClient.invalidateQueries({ queryKey: ["WorkItems", workAreaId] }); + 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 { + ...floor, + workAreas: floor.workAreas.map((area) => { + if (area.id !== workAreaId) return area; + return { + ...area, + completedWork: + (area.completedWork ?? 0) + + (data?.data?.completedTask ?? 0), + }; + }), + }; + }), + }; + }); + }); + queryClient.setQueryData(["ProjectsList"], (projects) => { + if (!projects) return projects; + const updatedProject = JSON.parse(JSON.stringify(projects)); + return updatedProject.map((project) => { + if (project.id !== selectedProject) return project; + + return { + ...project, + completedWork: + (project.completedWork ?? 0) + (data?.data?.completedTask ?? 0), + }; + }); + }); + showToast("Task Reported Successfully.", "success"); if (onSuccessCallback) onSuccessCallback(data); }, onError: (error) => { const msg = - error?.response?.data?.message || error.message || "Error occurred during API call"; - showToast( msg, "error" ); + error?.response?.data?.message || + error.message || + "Error occurred during API call"; + showToast(msg, "error"); }, }); @@ -145,16 +176,12 @@ export const useSubmitTaskComment = ({ actionAllow, onSuccessCallback }) => { return response.data; }, - onSuccess: ( data,variables ) => - { - + onSuccess: (data, variables) => { const workAreaId = variables?.commentsData?.workItem?.workArea?.id; queryClient.invalidateQueries({ queryKey: ["taskList"] }); if (actionAllow) { - showToast( "Review submitted successfully.", "success" ); - - } else - { + showToast("Review submitted successfully.", "success"); + } else { showToast("Comment sent successfully.", "success"); } @@ -162,7 +189,10 @@ export const useSubmitTaskComment = ({ actionAllow, onSuccessCallback }) => { }, onError: (error) => { - const msg = error?.response?.data?.message || error.message || "Error during API call"; + const msg = + error?.response?.data?.message || + error.message || + "Error during API call"; showToast(msg, "error"); }, }); @@ -170,24 +200,23 @@ export const useSubmitTaskComment = ({ actionAllow, onSuccessCallback }) => { return { submitComment: mutate, isPending }; }; -export const useCreateTask = ( {onSuccessCallback, onErrorCallback} = {} ) => -{ +export const useCreateTask = ({ onSuccessCallback, onErrorCallback } = {}) => { const queryClient = useQueryClient(); return useMutation({ - mutationFn: async ({payload,workAreaId}) => { + mutationFn: async ({ payload, workAreaId }) => { return await TasksRepository.assignTask(payload); }, - onSuccess: ( _, variables ) => - { - queryClient.invalidateQueries( {queryKey: [ "taskList" ]} ); - queryClient.invalidateQueries( {queryKey: [ "WorkItems", variables?.workAreaId ]} ); - showToast( "Task Assigned Successfully.", "success" ); + onSuccess: (_, variables) => { + queryClient.invalidateQueries({ queryKey: ["taskList"] }); + queryClient.invalidateQueries({ + queryKey: ["WorkItems", variables?.workAreaId], + }); + showToast("Task Assigned Successfully.", "success"); if (onSuccessCallback) onSuccessCallback(variables); }, - onError: ( error ) => - { + onError: (error) => { showToast("Something went wrong. Please try again.", "error"); if (onErrorCallback) onErrorCallback(error); }, }); -}; \ No newline at end of file +};