(app-desk) add useDeleteTeamAccess react-query hook

Add the hook useDeleteTeamAccess, it will be used to
delete member from a team.
This commit is contained in:
Anthony LC
2024-03-25 11:14:08 +01:00
committed by Anthony LC
parent 6de24d973b
commit 016232ad2d
2 changed files with 65 additions and 0 deletions

View File

@@ -1,2 +1,3 @@
export * from './useDeleteTeamAccess';
export * from './useTeamsAccesses';
export * from './useUpdateTeamAccess';

View File

@@ -0,0 +1,64 @@
import {
UseMutationOptions,
useMutation,
useQueryClient,
} from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api';
import { KEY_LIST_TEAM, KEY_TEAM } from '@/features/teams/';
import { KEY_LIST_TEAM_ACCESSES } from './useTeamsAccesses';
interface DeleteTeamAccessProps {
teamId: string;
accessId: string;
}
export const deleteTeamAccess = async ({
teamId,
accessId,
}: DeleteTeamAccessProps): Promise<void> => {
const response = await fetchAPI(`teams/${teamId}/accesses/${accessId}/`, {
method: 'DELETE',
});
if (!response.ok) {
throw new APIError(
'Failed to delete the member',
await errorCauses(response),
);
}
};
type UseDeleteTeamAccessOptions = UseMutationOptions<
void,
APIError,
DeleteTeamAccessProps
>;
export const useDeleteTeamAccess = (options?: UseDeleteTeamAccessOptions) => {
const queryClient = useQueryClient();
return useMutation<void, APIError, DeleteTeamAccessProps>({
mutationFn: deleteTeamAccess,
...options,
onSuccess: (data, variables, context) => {
void queryClient.invalidateQueries({
queryKey: [KEY_LIST_TEAM_ACCESSES],
});
void queryClient.invalidateQueries({
queryKey: [KEY_TEAM],
});
void queryClient.invalidateQueries({
queryKey: [KEY_LIST_TEAM],
});
if (options?.onSuccess) {
options.onSuccess(data, variables, context);
}
},
onError: (error, variables, context) => {
if (options?.onError) {
options.onError(error, variables, context);
}
},
});
};