(options: UseProfileMutationsOptions = {})
| 171 | * Hook returning all profile mutation functions |
| 172 | */ |
| 173 | export function useProfileMutations(options: UseProfileMutationsOptions = {}) { |
| 174 | const { organizationId } = options; |
| 175 | const queryClient = useQueryClient(); |
| 176 | const trpc = useTRPC(); |
| 177 | |
| 178 | const invalidateProfiles = async () => { |
| 179 | await queryClient.invalidateQueries({ |
| 180 | queryKey: trpc.agentProfiles.list.queryKey({ organizationId }), |
| 181 | }); |
| 182 | }; |
| 183 | |
| 184 | const invalidateProfile = async (profileId: string) => { |
| 185 | await queryClient.invalidateQueries({ |
| 186 | queryKey: trpc.agentProfiles.get.queryKey({ profileId, organizationId }), |
| 187 | }); |
| 188 | await invalidateProfiles(); |
| 189 | }; |
| 190 | |
| 191 | const createProfile = useMutation( |
| 192 | trpc.agentProfiles.create.mutationOptions({ |
| 193 | onSuccess: async () => { |
| 194 | await invalidateProfiles(); |
| 195 | }, |
| 196 | }) |
| 197 | ); |
| 198 | |
| 199 | const updateProfile = useMutation( |
| 200 | trpc.agentProfiles.update.mutationOptions({ |
| 201 | onSuccess: async (_data, variables) => { |
| 202 | await invalidateProfile(variables.profileId); |
| 203 | }, |
| 204 | }) |
| 205 | ); |
| 206 | |
| 207 | const deleteProfile = useMutation( |
| 208 | trpc.agentProfiles.delete.mutationOptions({ |
| 209 | onSuccess: async (_data, variables) => { |
| 210 | await invalidateProfile(variables.profileId); |
| 211 | }, |
| 212 | }) |
| 213 | ); |
| 214 | |
| 215 | const setAsDefault = useMutation( |
| 216 | trpc.agentProfiles.setAsDefault.mutationOptions({ |
| 217 | onSuccess: async () => { |
| 218 | // Setting a default flips isDefault on both the new and previous default, |
| 219 | // so invalidate every single-profile get query in addition to the list. |
| 220 | await queryClient.invalidateQueries({ |
| 221 | queryKey: trpc.agentProfiles.get.queryKey(), |
| 222 | }); |
| 223 | await invalidateProfiles(); |
| 224 | }, |
| 225 | }) |
| 226 | ); |
| 227 | |
| 228 | const clearDefault = useMutation( |
| 229 | trpc.agentProfiles.clearDefault.mutationOptions({ |
| 230 | onSuccess: async () => { |
no test coverage detected