From 22e65c167e0e262f156c753626f682ec148d2fb5 Mon Sep 17 00:00:00 2001 From: Pramod Mahajan Date: Sat, 28 Jun 2025 10:38:01 +0530 Subject: [PATCH] prevent unnwanted api calling --- src/hooks/useEmployees.js | 23 +++++++++++++++-------- src/hooks/useProfile.js | 2 +- src/layouts/AuthLayout.jsx | 4 ++-- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/hooks/useEmployees.js b/src/hooks/useEmployees.js index bc39a861..4d2cdf41 100644 --- a/src/hooks/useEmployees.js +++ b/src/hooks/useEmployees.js @@ -7,6 +7,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import showToast from "../services/toastService"; import {useSelector} from "react-redux"; import {store} from "../store/store"; +import {queryClient} from "../layouts/AuthLayout"; // export const useAllEmployees = (showInactive) => { @@ -397,7 +398,8 @@ export const useEmployeesAllOrByProjectId = (projectId, showInactive) => { }; // ManageEmployee.jsx -export const useEmployeeProfile = (employeeId) => { +export const useEmployeeProfile = ( employeeId ) => +{ const isEnabled = !!employeeId; const { @@ -412,6 +414,11 @@ export const useEmployeeProfile = (employeeId) => { return res.data; }, enabled: isEnabled, + // initialData: () => { + // if (!queryClient) return null; + // return queryClient.getQueryData(['employeeProfile', employeeId]) || null; + // }, + }); return { @@ -429,19 +436,19 @@ export const useEmployeeProfile = (employeeId) => { export const useUpdateEmployee = () => { const selectedProject = useSelector((store)=>store.localVariables.projectId) - const queryClient = useQueryClient(); + const queryClient = useQueryClient(); return useMutation({ mutationFn: (employeeData) => EmployeeRepository.manageEmployee(employeeData), onSuccess: (_, variables) => { const id = variables.id || variables.employeeId; - + const isAllEmployee = variables.IsAllEmployee; // Cache invalidation - queryClient.invalidateQueries( ['allEmployee',false]); - queryClient.invalidateQueries(['employeeProfile', id]); - queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} ); + queryClient.invalidateQueries( [ 'allEmployee', isAllEmployee ] ); + // queryClient.invalidateQueries(['employeeProfile', id]); + // queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} ); - queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] ); + // queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] ); showToast( `Employee ${ id ? 'updated' : 'created' } successfully`, 'success' ); }, onError: (error) => { @@ -465,7 +472,7 @@ export const useSuspendEmployee = ({ setIsDeleteModalOpen, setemployeeLodaing }) onSuccess: () => { showToast("Employee deleted successfully.", "success"); - queryClient.invalidateQueries( ['allEmployee',false]); + // queryClient.invalidateQueries( ['allEmployee',false]); queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} ); queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] ); diff --git a/src/hooks/useProfile.js b/src/hooks/useProfile.js index 0a222cc2..d9efefd1 100644 --- a/src/hooks/useProfile.js +++ b/src/hooks/useProfile.js @@ -79,7 +79,7 @@ export const useProfile = () => { return response.data; }, initialData: loggedUser || undefined, - enabled: true, + enabled: !loggedUser, staleTime: 10 * 60 * 1000, }); diff --git a/src/layouts/AuthLayout.jsx b/src/layouts/AuthLayout.jsx index 9399b90e..2640081b 100644 --- a/src/layouts/AuthLayout.jsx +++ b/src/layouts/AuthLayout.jsx @@ -6,8 +6,8 @@ export const queryClient = new QueryClient({ defaultOptions: { queries: { staleTime: 5 * 60 * 1000, // 5 min: data considered fresh - refetchOnWindowFocus: false, // refresh on tab switch - refetchOnReconnect: false, // re-fetch if network was lost + refetchOnWindowFocus: true, // refresh on tab switch + refetchOnReconnect: true, // re-fetch if network was lost retry: false, }, },