(app-desk) add useRemoveTeam react-query hook

Add the hook useRemoveTeam, it will be used to
remove a team.
This commit is contained in:
Anthony LC
2024-03-25 16:01:14 +01:00
committed by Anthony LC
parent f591c95a92
commit 55fbd661b0
2 changed files with 52 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
export * from './useCreateTeam';
export * from './useRemoveTeam';
export * from './useTeam';
export * from './useTeams';
export * from './useUpdateTeam';

View File

@@ -0,0 +1,51 @@
import {
UseMutationOptions,
useMutation,
useQueryClient,
} from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api';
import { KEY_LIST_TEAM } from './useTeams';
interface RemoveTeamProps {
teamId: string;
}
export const removeTeam = async ({
teamId,
}: RemoveTeamProps): Promise<void> => {
const response = await fetchAPI(`teams/${teamId}/`, {
method: 'DELETE',
});
if (!response.ok) {
throw new APIError(
'Failed to delete the team',
await errorCauses(response),
);
}
};
type UseRemoveTeamOptions = UseMutationOptions<void, APIError, RemoveTeamProps>;
export const useRemoveTeam = (options?: UseRemoveTeamOptions) => {
const queryClient = useQueryClient();
return useMutation<void, APIError, RemoveTeamProps>({
mutationFn: removeTeam,
...options,
onSuccess: (data, variables, context) => {
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);
}
},
});
};