dynamically update planned and completed work in ProjectInfra and project list card on Task reported

This commit is contained in:
pramod mahajan 2025-07-14 20:19:25 +05:30
parent 788f0baee4
commit d2b80b4f02
2 changed files with 86 additions and 52 deletions

View File

@ -62,7 +62,12 @@ export const ReportTask = ({ report, closeModal }) => {
checkList: [], 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 = () => { const handleClose = () => {
closeModal(); closeModal();

View File

@ -1,11 +1,10 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { TasksRepository } from "../repositories/TaskRepository"; import { TasksRepository } from "../repositories/TaskRepository";
import { cacheData, getCachedData } from "../slices/apiDataManager"; 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 { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import showToast from "../services/toastService"; import showToast from "../services/toastService";
import {useSelector} from "react-redux"; import { useSelector } from "react-redux";
// ---------Query--------------------------------- // ---------Query---------------------------------
@ -28,7 +27,6 @@ export const useTaskList = (projectId, dateFrom, toDate) => {
return response.data; return response.data;
}, },
enabled, enabled,
}); });
return { TaskList, loading, error, refetch }; return { TaskList, loading, error, refetch };
@ -46,7 +44,7 @@ export const useTaskById = (TaskId) => {
const res = await TasksRepository.getTaskById(TaskId); const res = await TasksRepository.getTaskById(TaskId);
return res.data; return res.data;
}, },
enabled: !!TaskId, enabled: !!TaskId,
}); });
return { Task, loading, error, refetch }; return { Task, loading, error, refetch };
@ -75,47 +73,80 @@ export const useAuditStatus = () => {
const res = await MasterRespository.getAuditStatus(); const res = await MasterRespository.getAuditStatus();
return res.data; return res.data;
}, },
}); });
return { status, loading, error, refetch }; return { status, loading, error, refetch };
}; };
// -----------------------Mutation------------------------ // -----------------------Mutation------------------------
const toDate = new Date().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]; 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 queryClient = useQueryClient();
const { const selectedProject = useSelector(
mutate, (store) => store.localVariables.projectId
isPending, );
isSuccess, const { mutate, isPending, isSuccess, isError, error } = useMutation({
isError, mutationFn: async ({ reportData, workAreaId }) => {
error,
} = useMutation({
mutationFn: async ( {reportData,workAreaId} ) =>
{
debugger
return await TasksRepository.reportTask(reportData); return await TasksRepository.reportTask(reportData);
}, },
onSuccess: ( data, variables ) => onSuccess: (data, variables) => {
{ const { workAreaId, buildingId, floorId } = variables;
const {workAreaId} = variables; queryClient.invalidateQueries({ queryKey: ["taskList"] });
queryClient.invalidateQueries( {queryKey: [ "taskList" ]} ); queryClient.invalidateQueries({ queryKey: ["WorkItems", workAreaId] });
queryClient.invalidateQueries( {queryKey: [ "WorkItems", workAreaId ]} ); queryClient.setQueryData(["ProjectInfra", selectedProject], (oldData) => {
queryClient.invalidateQueries( {queryKey: [ 'ProjectsList' ]} ); if (!oldData) return oldData;
showToast( "Task Reported Successfully.", "success" ); 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); if (onSuccessCallback) onSuccessCallback(data);
}, },
onError: (error) => { onError: (error) => {
const msg = const msg =
error?.response?.data?.message || error.message || "Error occurred during API call"; error?.response?.data?.message ||
showToast( msg, "error" ); error.message ||
"Error occurred during API call";
showToast(msg, "error");
}, },
}); });
@ -145,16 +176,12 @@ export const useSubmitTaskComment = ({ actionAllow, onSuccessCallback }) => {
return response.data; return response.data;
}, },
onSuccess: ( data,variables ) => onSuccess: (data, variables) => {
{
const workAreaId = variables?.commentsData?.workItem?.workArea?.id; const workAreaId = variables?.commentsData?.workItem?.workArea?.id;
queryClient.invalidateQueries({ queryKey: ["taskList"] }); queryClient.invalidateQueries({ queryKey: ["taskList"] });
if (actionAllow) { if (actionAllow) {
showToast( "Review submitted successfully.", "success" ); showToast("Review submitted successfully.", "success");
} else {
} else
{
showToast("Comment sent successfully.", "success"); showToast("Comment sent successfully.", "success");
} }
@ -162,7 +189,10 @@ export const useSubmitTaskComment = ({ actionAllow, onSuccessCallback }) => {
}, },
onError: (error) => { 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"); showToast(msg, "error");
}, },
}); });
@ -170,24 +200,23 @@ export const useSubmitTaskComment = ({ actionAllow, onSuccessCallback }) => {
return { submitComment: mutate, isPending }; return { submitComment: mutate, isPending };
}; };
export const useCreateTask = ( {onSuccessCallback, onErrorCallback} = {} ) => export const useCreateTask = ({ onSuccessCallback, onErrorCallback } = {}) => {
{
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: async ({payload,workAreaId}) => { mutationFn: async ({ payload, workAreaId }) => {
return await TasksRepository.assignTask(payload); return await TasksRepository.assignTask(payload);
}, },
onSuccess: ( _, variables ) => onSuccess: (_, variables) => {
{ queryClient.invalidateQueries({ queryKey: ["taskList"] });
queryClient.invalidateQueries( {queryKey: [ "taskList" ]} ); queryClient.invalidateQueries({
queryClient.invalidateQueries( {queryKey: [ "WorkItems", variables?.workAreaId ]} ); queryKey: ["WorkItems", variables?.workAreaId],
showToast( "Task Assigned Successfully.", "success" ); });
showToast("Task Assigned Successfully.", "success");
if (onSuccessCallback) onSuccessCallback(variables); if (onSuccessCallback) onSuccessCallback(variables);
}, },
onError: ( error ) => onError: (error) => {
{
showToast("Something went wrong. Please try again.", "error"); showToast("Something went wrong. Please try again.", "error");
if (onErrorCallback) onErrorCallback(error); if (onErrorCallback) onErrorCallback(error);
}, },
}); });
}; };