diff --git a/src/hooks/useEmployees.js b/src/hooks/useEmployees.js index b911d2dc..96b1bd92 100644 --- a/src/hooks/useEmployees.js +++ b/src/hooks/useEmployees.js @@ -176,8 +176,9 @@ export const useEmployeeProfile = (employeeId) => { export const useEmployeesName = (projectId, search) => { return useQuery({ queryKey: ["employees", projectId, search], - queryFn: async() => await EmployeeRepository.getEmployeeName(projectId, search), - + queryFn: async () => + await EmployeeRepository.getEmployeeName(projectId, search), + staleTime: 5 * 60 * 1000, // Optional: cache for 5 minutes }); }; @@ -194,7 +195,6 @@ export const useEmployeesNameByProject = (projectId) => { }); }; - // Mutation------------------------------------------------------------------ export const useUpdateEmployee = () => { @@ -232,36 +232,78 @@ export const useUpdateEmployee = () => { }); }; +// export const useSuspendEmployee = ({ setIsDeleteModalOpen, setemployeeLodaing }) => { +// const queryClient = useQueryClient(); +// const selectedProject = useSelector((store)=>store.localVariables.projectId) +// return useMutation({ +// mutationFn: (id) => { +// setemployeeLodaing(true); +// return EmployeeRepository.deleteEmployee(id); +// }, + +// onSuccess: () => { + +// // queryClient.invalidateQueries( ['allEmployee',false]); +// queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} ); +// queryClient.invalidateQueries( {queryKey:[ 'employeeListByProject' ,selectedProject]} ); +// showToast("Employee deleted successfully.", "success"); +// setIsDeleteModalOpen(false); +// }, + +// onError: (error) => { +// const message = +// error.response?.data?.message || +// error.message || +// "An unexpected error occurred"; +// showToast(message, "error"); +// setIsDeleteModalOpen(false); +// }, + +// onSettled: () => { +// setemployeeLodaing(false); +// }, +// }); +// }; + +// Manage Role + export const useSuspendEmployee = ({ setIsDeleteModalOpen, setemployeeLodaing, }) => { const queryClient = useQueryClient(); - const selectedProject = useSelector( + const selectedProjectId = useSelector( (store) => store.localVariables.projectId ); + return useMutation({ - mutationFn: (id) => { + mutationFn: async (employeeId) => { setemployeeLodaing(true); - return EmployeeRepository.deleteEmployee(id); + return await EmployeeRepository.deleteEmployee(employeeId); }, - onSuccess: () => { - // queryClient.invalidateQueries( ['allEmployee',false]); - queryClient.invalidateQueries({ queryKey: ["projectEmployees"] }); - queryClient.invalidateQueries({ - queryKey: ["employeeListByProject", selectedProject], - }); - showToast("Employee deleted successfully.", "success"); + onSuccess: (_, employeeId) => { + showToast("Employee suspended successfully.", "success"); setIsDeleteModalOpen(false); + + // Invalidate only the required employee-related queries + queryClient.invalidateQueries({ queryKey: ["employee", employeeId] }); + queryClient.invalidateQueries({ queryKey: ["allEmployees"] }); + + if (selectedProjectId) { + queryClient.invalidateQueries({ + queryKey: ["projectEmployees", selectedProjectId], + }); + } }, onError: (error) => { - const message = + showToast( error.response?.data?.message || - error.message || - "An unexpected error occurred"; - showToast(message, "error"); + error.message || + "An unexpected error occurred", + "error" + ); setIsDeleteModalOpen(false); }, @@ -271,8 +313,6 @@ export const useSuspendEmployee = ({ }); }; -// Manage Role - export const useUpdateEmployeeRoles = ({ onClose, resetForm,