prevent unnwanted api calling

This commit is contained in:
Pramod Mahajan 2025-06-28 10:38:01 +05:30
parent cfa1c6366d
commit 22e65c167e
3 changed files with 18 additions and 11 deletions

View File

@ -7,6 +7,7 @@ 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";
import {store} from "../store/store"; import {store} from "../store/store";
import {queryClient} from "../layouts/AuthLayout";
// export const useAllEmployees = (showInactive) => { // export const useAllEmployees = (showInactive) => {
@ -397,7 +398,8 @@ export const useEmployeesAllOrByProjectId = (projectId, showInactive) => {
}; };
// ManageEmployee.jsx // ManageEmployee.jsx
export const useEmployeeProfile = (employeeId) => { export const useEmployeeProfile = ( employeeId ) =>
{
const isEnabled = !!employeeId; const isEnabled = !!employeeId;
const { const {
@ -412,6 +414,11 @@ export const useEmployeeProfile = (employeeId) => {
return res.data; return res.data;
}, },
enabled: isEnabled, enabled: isEnabled,
// initialData: () => {
// if (!queryClient) return null;
// return queryClient.getQueryData(['employeeProfile', employeeId]) || null;
// },
}); });
return { return {
@ -429,19 +436,19 @@ export const useEmployeeProfile = (employeeId) => {
export const useUpdateEmployee = () => export const useUpdateEmployee = () =>
{ {
const selectedProject = useSelector((store)=>store.localVariables.projectId) const selectedProject = useSelector((store)=>store.localVariables.projectId)
const queryClient = useQueryClient(); const queryClient = useQueryClient();
return useMutation({ return useMutation({
mutationFn: (employeeData) => EmployeeRepository.manageEmployee(employeeData), mutationFn: (employeeData) => EmployeeRepository.manageEmployee(employeeData),
onSuccess: (_, variables) => { onSuccess: (_, variables) => {
const id = variables.id || variables.employeeId; const id = variables.id || variables.employeeId;
const isAllEmployee = variables.IsAllEmployee;
// Cache invalidation // Cache invalidation
queryClient.invalidateQueries( ['allEmployee',false]); queryClient.invalidateQueries( [ 'allEmployee', isAllEmployee ] );
queryClient.invalidateQueries(['employeeProfile', id]); // queryClient.invalidateQueries(['employeeProfile', id]);
queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} ); // queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} );
queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] ); // queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] );
showToast( `Employee ${ id ? 'updated' : 'created' } successfully`, 'success' ); showToast( `Employee ${ id ? 'updated' : 'created' } successfully`, 'success' );
}, },
onError: (error) => { onError: (error) => {
@ -465,7 +472,7 @@ export const useSuspendEmployee = ({ setIsDeleteModalOpen, setemployeeLodaing })
onSuccess: () => { onSuccess: () => {
showToast("Employee deleted successfully.", "success"); showToast("Employee deleted successfully.", "success");
queryClient.invalidateQueries( ['allEmployee',false]); // queryClient.invalidateQueries( ['allEmployee',false]);
queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} ); queryClient.invalidateQueries( {queryKey: [ 'projectEmployees' ]} );
queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] ); queryClient.invalidateQueries( [ 'employeeListByProject' ,selectedProject] );

View File

@ -79,7 +79,7 @@ export const useProfile = () => {
return response.data; return response.data;
}, },
initialData: loggedUser || undefined, initialData: loggedUser || undefined,
enabled: true, enabled: !loggedUser,
staleTime: 10 * 60 * 1000, staleTime: 10 * 60 * 1000,
}); });

View File

@ -6,8 +6,8 @@ export const queryClient = new QueryClient({
defaultOptions: { defaultOptions: {
queries: { queries: {
staleTime: 5 * 60 * 1000, // 5 min: data considered fresh staleTime: 5 * 60 * 1000, // 5 min: data considered fresh
refetchOnWindowFocus: false, // refresh on tab switch refetchOnWindowFocus: true, // refresh on tab switch
refetchOnReconnect: false, // re-fetch if network was lost refetchOnReconnect: true, // re-fetch if network was lost
retry: false, retry: false,
}, },
}, },