@@ -3,7 +3,7 @@ import { Permission } from "@/models/Permission";
33import { Position } from "@/models/Position" ;
44import { StaffAPIData , StaffUser } from "@/models/Staff" ;
55import { OnSuccessType , request , requestAuthAPI } from "@/utils/axiosUtils" ;
6- import { useMutation , useQuery } from "@tanstack/react-query" ;
6+ import { useMutation , useQuery , useQueryClient } from "@tanstack/react-query" ;
77import { useStaticQuery } from "@/hooks/useCustomQueries" ;
88import { STAFF_USER_POSITION } from "@/utils/constants" ;
99
@@ -80,14 +80,65 @@ export const usePermissionsData = () => {
8080 } ) ;
8181} ;
8282
83- export const useAddStaff = ( onSuccess : OnSuccessType ) => {
84- return useMutation ( { mutationFn : addStaff , onSuccess } ) ;
85- } ;
83+ export const useAddStaff = ( onSuccess ?: OnSuccessType ) => {
84+ const queryClient = useQueryClient ( ) ;
85+
86+ return useMutation ( {
87+ mutationFn : addStaff ,
88+ onSuccess : async ( data ) => {
89+ // Invalidate all staff-users queries
90+ await queryClient . invalidateQueries ( {
91+ queryKey : [ "staff-users" ] ,
92+ refetchType : 'active'
93+ } ) ;
8694
87- export const useUpdateStaff = ( onSuccess : OnSuccessType ) => {
88- return useMutation ( { mutationFn : updateStaff , onSuccess } ) ;
95+ // Call the provided callback if it exists
96+ if ( onSuccess ) {
97+ onSuccess ( data ) ;
98+ }
99+ } ,
100+ } ) ;
89101} ;
90102
91- export const useDeleteStaff = ( onSuccess : OnSuccessType ) => {
92- return useMutation ( { mutationFn : deleteStaff , onSuccess } ) ;
103+ export const useUpdateStaff = ( onSuccess ?: OnSuccessType ) => {
104+ const queryClient = useQueryClient ( ) ;
105+
106+ return useMutation ( {
107+ mutationFn : updateStaff ,
108+ onSuccess : async ( data ) => {
109+ // Invalidate all staff-users queries
110+ await queryClient . invalidateQueries ( {
111+ queryKey : [ "staff-users" ] ,
112+ refetchType : 'active'
113+ } ) ;
114+
115+ // Call the provided callback if it exists
116+ if ( onSuccess ) {
117+ onSuccess ( data ) ;
118+ }
119+ } ,
120+ } ) ;
93121} ;
122+
123+ export const useDeleteStaff = ( onSuccess ?: OnSuccessType ) => {
124+ const queryClient = useQueryClient ( ) ;
125+
126+ return useMutation ( {
127+ mutationFn : deleteStaff ,
128+ onSuccess : async ( data , ) => {
129+ // Invalidate all staff-users queries
130+ await queryClient . invalidateQueries ( {
131+ queryKey : [ "staff-users" ] ,
132+ refetchType : 'active'
133+ } ) ;
134+
135+ // Invalidate validation
136+ queryClient . invalidateQueries ( { queryKey : [ "staff-user-validation" ] } ) ;
137+
138+ // Call the provided callback if it exists
139+ if ( onSuccess ) {
140+ onSuccess ( data , ) ;
141+ }
142+ } ,
143+ } ) ;
144+ } ;
0 commit comments