dynamically updated at project list and workarea data whenvere edit or created activity #254
@ -116,7 +116,17 @@ useEffect(() => {
|
|||||||
floorId: floor?.id,
|
floorId: floor?.id,
|
||||||
workAreaId: workArea?.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 (
|
return (
|
||||||
<form className="row g-2 p-2 p-md-1" onSubmit={handleSubmit(onSubmitForm)}>
|
<form className="row g-2 p-2 p-md-1" onSubmit={handleSubmit(onSubmitForm)}>
|
||||||
|
|||||||
@ -97,7 +97,9 @@ const TaskModel = ({ project, onSubmit, onClose }) => {
|
|||||||
|
|
||||||
const onSubmitForm = async (data) => {
|
const onSubmitForm = async (data) => {
|
||||||
const payload = [data];
|
const payload = [data];
|
||||||
CreateTask(payload);
|
CreateTask({payload:payload,buildingId: data.buildingID,
|
||||||
|
floorId: data.floorId,
|
||||||
|
workAreaId: data.workAreaId, PreviousPlannedWork:data.plannedWork,previousCompletedWork:data.completedWork});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -263,14 +265,15 @@ const TaskModel = ({ project, onSubmit, onClose }) => {
|
|||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
className="btn btn-sm btn-primary me-3"
|
className="btn btn-sm btn-primary me-3"
|
||||||
disabled={isSubmitting}
|
disabled={isPending}
|
||||||
>
|
>
|
||||||
{isSubmitting ? "Please Wait..." : "Add Task"}
|
{isPending ? "Please Wait..." : "Add Task"}
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
className="btn btn-sm btn-label-secondary"
|
className="btn btn-sm btn-label-secondary"
|
||||||
onClick={onClose}
|
onClick={onClose}
|
||||||
|
disabled={isPending}
|
||||||
>
|
>
|
||||||
Cancel
|
Cancel
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@ -6,7 +6,12 @@ import { useDispatch, useSelector } from "react-redux";
|
|||||||
import { setProjectId } from "../slices/localVariablesSlice";
|
import { setProjectId } from "../slices/localVariablesSlice";
|
||||||
import EmployeeList from "../components/Directory/EmployeeList";
|
import EmployeeList from "../components/Directory/EmployeeList";
|
||||||
import eventBus from "../services/eventBus";
|
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";
|
import showToast from "../services/toastService";
|
||||||
|
|
||||||
// export const useProjects = () => {
|
// export const useProjects = () => {
|
||||||
@ -211,7 +216,7 @@ export const useProjects = () => {
|
|||||||
error,
|
error,
|
||||||
refetch,
|
refetch,
|
||||||
} = useQuery({
|
} = useQuery({
|
||||||
queryKey: ['ProjectsList'],
|
queryKey: ["ProjectsList"],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const response = await ProjectRepository.getProjectList();
|
const response = await ProjectRepository.getProjectList();
|
||||||
return response.data;
|
return response.data;
|
||||||
@ -227,97 +232,112 @@ export const useProjects = () => {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useEmployeesByProjectAllocated = (selectedProject) =>
|
export const useEmployeesByProjectAllocated = (selectedProject) => {
|
||||||
{
|
const {
|
||||||
const {data = [], isLoading, refetch, error} = useQuery( {
|
data = [],
|
||||||
queryKey: ["empListByProjectAllocated", selectedProject ],
|
isLoading,
|
||||||
queryFn: async () =>
|
refetch,
|
||||||
{
|
error,
|
||||||
const res = await ProjectRepository.getProjectAllocation( selectedProject );
|
} = useQuery({
|
||||||
return res.data || res
|
queryKey: ["empListByProjectAllocated", selectedProject],
|
||||||
|
queryFn: async () => {
|
||||||
|
const res = await ProjectRepository.getProjectAllocation(selectedProject);
|
||||||
|
return res.data || res;
|
||||||
},
|
},
|
||||||
enabled: !!selectedProject,
|
enabled: !!selectedProject,
|
||||||
onError: ( error ) =>
|
onError: (error) => {
|
||||||
{
|
showToast(
|
||||||
showToast(error.message || "Error while Fetching project Allocated Employees", "error");
|
error.message || "Error while Fetching project Allocated Employees",
|
||||||
}
|
"error"
|
||||||
} )
|
);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
projectEmployees: data,
|
projectEmployees: data,
|
||||||
loading:isLoading,
|
loading: isLoading,
|
||||||
error,
|
error,
|
||||||
refetch
|
refetch,
|
||||||
}
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
export const useProjectDetails = ( projectId,isAuto = true ) =>
|
export const useProjectDetails = (projectId, isAuto = true) => {
|
||||||
{
|
const {
|
||||||
const {data: projects_Details, isLoading, error, refetch} = useQuery( {
|
data: projects_Details,
|
||||||
queryKey: [ "projectInfo", projectId ],
|
isLoading,
|
||||||
queryFn: async () =>
|
error,
|
||||||
{
|
refetch,
|
||||||
const res = await ProjectRepository.getProjectByprojectId( projectId );
|
} = useQuery({
|
||||||
|
queryKey: ["projectInfo", projectId],
|
||||||
|
queryFn: async () => {
|
||||||
|
const res = await ProjectRepository.getProjectByprojectId(projectId);
|
||||||
return res.data || res;
|
return res.data || res;
|
||||||
},
|
},
|
||||||
enabled: !!projectId && isAuto,
|
enabled: !!projectId && isAuto,
|
||||||
onError: ( error ) =>
|
onError: (error) => {
|
||||||
{
|
showToast(
|
||||||
showToast(error.message || "Error while Fetching project Details", "error");
|
error.message || "Error while Fetching project Details",
|
||||||
}
|
"error"
|
||||||
} )
|
);
|
||||||
return { projects_Details, loading:isLoading, error, refetch };
|
},
|
||||||
}
|
});
|
||||||
|
return { projects_Details, loading: isLoading, error, refetch };
|
||||||
|
};
|
||||||
|
|
||||||
export const useProjectsByEmployee = (employeeId) =>
|
export const useProjectsByEmployee = (employeeId) => {
|
||||||
{
|
const {
|
||||||
const { data:projectNameList =[],isLoading,error,refetch} = useQuery( {
|
data: projectNameList = [],
|
||||||
queryKey: [ "ProjectsByEmployee", employeeId ],
|
isLoading,
|
||||||
queryFn: async () =>
|
error,
|
||||||
{
|
refetch,
|
||||||
const res = await ProjectRepository.getProjectsByEmployee( employeeId );
|
} = useQuery({
|
||||||
|
queryKey: ["ProjectsByEmployee", employeeId],
|
||||||
|
queryFn: async () => {
|
||||||
|
const res = await ProjectRepository.getProjectsByEmployee(employeeId);
|
||||||
return res.data || res;
|
return res.data || res;
|
||||||
},
|
},
|
||||||
enabled: !!employeeId,
|
enabled: !!employeeId,
|
||||||
onError: ( error ) =>
|
onError: (error) => {
|
||||||
{
|
showToast(
|
||||||
showToast(error.message || "Error while Fetching project Employee", "error");
|
error.message || "Error while Fetching project Employee",
|
||||||
}
|
"error"
|
||||||
})
|
);
|
||||||
return {projectList, loading:isLoading,error,refetch }
|
},
|
||||||
}
|
});
|
||||||
|
return { projectList, loading: isLoading, error, refetch };
|
||||||
|
};
|
||||||
|
|
||||||
export const useProjectName = () =>
|
export const useProjectName = () => {
|
||||||
{
|
const {
|
||||||
const {data = [],isLoading,error,refetch} = useQuery( {
|
data = [],
|
||||||
queryKey: [ "basicProjectNameList" ],
|
isLoading,
|
||||||
queryFn: async () =>
|
error,
|
||||||
{
|
refetch,
|
||||||
|
} = useQuery({
|
||||||
|
queryKey: ["basicProjectNameList"],
|
||||||
|
queryFn: async () => {
|
||||||
const res = await ProjectRepository.projectNameList();
|
const res = await ProjectRepository.projectNameList();
|
||||||
return res.data || res;
|
return res.data || res;
|
||||||
},
|
},
|
||||||
onError: ( error ) =>
|
onError: (error) => {
|
||||||
{
|
showToast(error.message || "Error while Fetching project Name", "error");
|
||||||
showToast(error.message || "Error while Fetching project Name", "error");
|
},
|
||||||
}
|
});
|
||||||
} )
|
return { projectNames: data, loading: isLoading, Error: error, refetch };
|
||||||
return {projectNames:data,loading:isLoading,Error:error,refetch}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const useProjectInfra = (projectId) => {
|
export const useProjectInfra = (projectId) => {
|
||||||
const {
|
const {
|
||||||
data: projectInfra,
|
data: projectInfra,
|
||||||
isLoading,
|
isLoading,
|
||||||
error,
|
error,
|
||||||
} = useQuery({
|
} = useQuery({
|
||||||
queryKey: ["ProjectInfra", projectId],
|
queryKey: ["ProjectInfra", projectId],
|
||||||
queryFn: async () => {
|
queryFn: async () => {
|
||||||
const res = await ProjectRepository.getProjectInfraByproject(projectId);
|
const res = await ProjectRepository.getProjectInfraByproject(projectId);
|
||||||
return res.data;
|
return res.data;
|
||||||
},
|
},
|
||||||
enabled: !!projectId ,
|
enabled: !!projectId,
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
showToast(error.message || "Error while fetching project infra", "error");
|
showToast(error.message || "Error while fetching project infra", "error");
|
||||||
},
|
},
|
||||||
@ -326,30 +346,27 @@ export const useProjectInfra = (projectId) => {
|
|||||||
return { projectInfra, isLoading, error };
|
return { projectInfra, isLoading, error };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useProjectTasks = (workAreaId, IsExpandedArea = false) => {
|
||||||
export const useProjectTasks = (workAreaId,IsExpandedArea=false) =>
|
const {
|
||||||
{
|
data: ProjectTaskList,
|
||||||
const { data:ProjectTaskList,isLoading,error } = useQuery( {
|
isLoading,
|
||||||
queryKey: [ "WorkItems",workAreaId ],
|
error,
|
||||||
queryFn: async () =>
|
} = useQuery({
|
||||||
{
|
queryKey: ["WorkItems", workAreaId],
|
||||||
|
queryFn: async () => {
|
||||||
const res = await ProjectRepository.getProjectTasksByWorkArea(workAreaId);
|
const res = await ProjectRepository.getProjectTasksByWorkArea(workAreaId);
|
||||||
return res.data;
|
return res.data;
|
||||||
},
|
},
|
||||||
enabled: !!workAreaId && !!IsExpandedArea,
|
enabled: !!workAreaId && !!IsExpandedArea,
|
||||||
onError: ( error ) =>
|
onError: (error) => {
|
||||||
{
|
showToast(error.message || "Error while Fetching project Tasks", "error");
|
||||||
showToast(error.message || "Error while Fetching project Tasks", "error");
|
},
|
||||||
}
|
});
|
||||||
} )
|
return { ProjectTaskList, isLoading, error };
|
||||||
return {ProjectTaskList,isLoading,error}
|
};
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// -- -------------Mutation-------------------------------
|
// -- -------------Mutation-------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const useCreateProject = ({ onSuccessCallback }) => {
|
export const useCreateProject = ({ onSuccessCallback }) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
@ -360,8 +377,8 @@ export const useCreateProject = ({ onSuccessCallback }) => {
|
|||||||
},
|
},
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
// Invalidate the cache
|
// Invalidate the cache
|
||||||
queryClient.invalidateQueries( {queryKey: [ 'ProjectsList' ]} );
|
queryClient.invalidateQueries({ queryKey: ["ProjectsList"] });
|
||||||
queryClient.invalidateQueries({queryKey:['basicProjectNameList']});
|
queryClient.invalidateQueries({ queryKey: ["basicProjectNameList"] });
|
||||||
|
|
||||||
// Emit event for consumers (like useProjects or others)
|
// Emit event for consumers (like useProjects or others)
|
||||||
eventBus.emit("project", {
|
eventBus.emit("project", {
|
||||||
@ -381,27 +398,19 @@ export const useCreateProject = ({ onSuccessCallback }) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const useUpdateProject = ({ onSuccessCallback }) => {
|
export const useUpdateProject = ({ onSuccessCallback }) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const {
|
const { mutate, isPending, isSuccess, isError } = useMutation({
|
||||||
mutate,
|
mutationFn: async ({ projectId, updatedData }) => {
|
||||||
isPending,
|
|
||||||
isSuccess,
|
|
||||||
isError,
|
|
||||||
} = useMutation({
|
|
||||||
mutationFn: async ( {projectId, updatedData} ) =>
|
|
||||||
{
|
|
||||||
return await ProjectRepository.updateProject(projectId, updatedData);
|
return await ProjectRepository.updateProject(projectId, updatedData);
|
||||||
},
|
},
|
||||||
|
|
||||||
onSuccess: ( data, variables ) =>
|
onSuccess: (data, variables) => {
|
||||||
{
|
|
||||||
const { projectId } = variables;
|
const { projectId } = variables;
|
||||||
|
|
||||||
queryClient.invalidateQueries({queryKey:["ProjectsList"]});
|
queryClient.invalidateQueries({ queryKey: ["ProjectsList"] });
|
||||||
queryClient.invalidateQueries( {queryKey: [ "projectInfo", projectId ]} );
|
queryClient.invalidateQueries({ queryKey: ["projectInfo", projectId] });
|
||||||
queryClient.invalidateQueries({queryKey:['basicProjectNameList']});
|
queryClient.invalidateQueries({ queryKey: ["basicProjectNameList"] });
|
||||||
|
|
||||||
eventBus.emit("project", {
|
eventBus.emit("project", {
|
||||||
keyword: "Update_Project",
|
keyword: "Update_Project",
|
||||||
@ -415,9 +424,8 @@ export const useUpdateProject = ({ onSuccessCallback }) => {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
onError: ( error ) =>
|
onError: (error) => {
|
||||||
{
|
console.log(error);
|
||||||
console.log(error)
|
|
||||||
showToast(error?.message || "Error while updating project", "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();
|
const queryClient = useQueryClient();
|
||||||
return useMutation({
|
return useMutation({
|
||||||
mutationFn: async ( {infraObject, projectId} ) =>
|
mutationFn: async ({ infraObject, projectId }) => {
|
||||||
{
|
|
||||||
return await ProjectRepository.manageProjectInfra(infraObject);
|
return await ProjectRepository.manageProjectInfra(infraObject);
|
||||||
},
|
},
|
||||||
onSuccess: ( data, variables ) =>
|
onSuccess: (data, variables) => {
|
||||||
{
|
const { projectId } = variables;
|
||||||
const { projectId } = variables;
|
queryClient.invalidateQueries({ queryKey: ["ProjectInfra", projectId] });
|
||||||
queryClient.invalidateQueries({queryKey:["ProjectInfra", projectId]});
|
if (onSuccessCallback) onSuccessCallback(data, variables);
|
||||||
if (onSuccessCallback) onSuccessCallback(data,variables);
|
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
showToast(error.message || "Failed to update Project Infra", "error");
|
showToast(error.message || "Failed to update Project Infra", "error");
|
||||||
@ -451,39 +455,36 @@ export const useManageProjectInfra = ( {onSuccessCallback} ) =>
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
export const useManageProjectAllocation = ({
|
export const useManageProjectAllocation = ({
|
||||||
onSuccessCallback,
|
onSuccessCallback,
|
||||||
onErrorCallback,
|
onErrorCallback,
|
||||||
}) => {
|
}) => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const {
|
const { mutate, isPending, isSuccess, isError } = useMutation({
|
||||||
mutate,
|
mutationFn: async ({ items }) => {
|
||||||
isPending,
|
|
||||||
isSuccess,
|
|
||||||
isError,
|
|
||||||
} = useMutation({
|
|
||||||
mutationFn: async ( {items} ) =>
|
|
||||||
{
|
|
||||||
const response = await ProjectRepository.manageProjectAllocation(items);
|
const response = await ProjectRepository.manageProjectAllocation(items);
|
||||||
return response.data;
|
return response.data;
|
||||||
},
|
},
|
||||||
onSuccess: (data, variables, context) => {
|
onSuccess: (data, variables, context) => {
|
||||||
queryClient.invalidateQueries({queryKey:['empListByProjectAllocated']});
|
queryClient.invalidateQueries({
|
||||||
queryClient.removeQueries({queryKey:["projectEmployees"]})
|
queryKey: ["empListByProjectAllocated"],
|
||||||
|
});
|
||||||
|
queryClient.removeQueries({ queryKey: ["projectEmployees"] });
|
||||||
if (variables?.added) {
|
if (variables?.added) {
|
||||||
showToast('Employee Assigned Successfully', 'success');
|
showToast("Employee Assigned Successfully", "success");
|
||||||
} else {
|
} else {
|
||||||
showToast('Removed Employee Successfully', 'success');
|
showToast("Removed Employee Successfully", "success");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (onSuccessCallback) onSuccessCallback(data, context);
|
if (onSuccessCallback) onSuccessCallback(data, context);
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
const message =
|
const message =
|
||||||
error?.response?.data?.message || error.message || 'Error occurred during API call';
|
error?.response?.data?.message ||
|
||||||
showToast(message, 'error');
|
error.message ||
|
||||||
|
"Error occurred during API call";
|
||||||
|
showToast(message, "error");
|
||||||
if (onErrorCallback) onErrorCallback(error);
|
if (onErrorCallback) onErrorCallback(error);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -496,48 +497,185 @@ export const useManageProjectAllocation = ({
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export const useManageTask = ({onSuccessCallback}) =>
|
export const useManageTask = ({ onSuccessCallback }) => {
|
||||||
{
|
|
||||||
const queryClient = useQueryClient();
|
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;
|
||||||
|
|
||||||
return useMutation( {
|
queryClient.setQueryData(["ProjectInfra", selectedProject], (oldData) => {
|
||||||
mutationFn: async ( payload ) => await ProjectRepository.manageProjectTasks( payload ),
|
if (!oldData) return oldData;
|
||||||
onSuccess: ( data, variables ) =>
|
const { workAreaId, floorId, buildingId } = variables;
|
||||||
{
|
const updatedData = JSON.parse(JSON.stringify(oldData));
|
||||||
queryClient.invalidateQueries({ queryKey: ["WorkItems"] })
|
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;
|
||||||
|
|
||||||
|
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);
|
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) => {
|
onError: (error) => {
|
||||||
showToast(
|
const message =
|
||||||
error?.response?.data?.message || error.message || "Failed to delete task",
|
error?.response?.data?.message ||
|
||||||
"error"
|
error.message ||
|
||||||
);
|
"Error occurred during API call";
|
||||||
if (onSuccessCallback) onSuccessCallback();
|
showToast(message, "error");
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const useDeleteProjectTask = (onSuccessCallback) => {
|
||||||
|
const queryClient = useQueryClient();
|
||||||
|
const selectedProject = useSelector(
|
||||||
|
(store) => store.localVariables.projectId
|
||||||
|
);
|
||||||
|
return useMutation({
|
||||||
|
mutationFn: async ({ workItemId, workAreaId }) => {
|
||||||
|
return await ProjectRepository.deleteProjectTask(workItemId);
|
||||||
|
},
|
||||||
|
onSuccess: (data, variables) => {
|
||||||
|
|
||||||
|
queryClient.invalidateQueries({
|
||||||
|
queryKey: ["WorkItems", variables.workAreaId],
|
||||||
|
});
|
||||||
|
// debugger
|
||||||
|
// 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,
|
||||||
|
// plannedWork: area.plannedWork ?? 0 - data?.data[0]?.workItem.plannedWork ?? 0,
|
||||||
|
// completedWork: area.completedWork ?? 0 - data?.data[0]?.workItem.completedWork ?? 0,
|
||||||
|
// };
|
||||||
|
// }),
|
||||||
|
// };
|
||||||
|
// }),
|
||||||
|
// };
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
// queryClient.setQueryData(["ProjectsList"], (projects) => {
|
||||||
|
// if (!projects) return projects;
|
||||||
|
|
||||||
|
// return projects.map((project) => {
|
||||||
|
// if (project.id !== selectedProject) return project;
|
||||||
|
// return {
|
||||||
|
// ...project,
|
||||||
|
// plannedWork: project.plannedWork - data?.data[0]?.workItem.plannedWork ?? 0,
|
||||||
|
// completedWork: project.completedWork - data?.data[0]?.workItem.completedWork ?? 0,
|
||||||
|
// };
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
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();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user