From 016232ad2d320eef2ec482b709cd1a5d2f38de42 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Mon, 25 Mar 2024 11:14:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(app-desk)=20add=20useDeleteTeamAccess?= =?UTF-8?q?=20react-query=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the hook useDeleteTeamAccess, it will be used to delete member from a team. --- .../desk/src/features/members/api/index.ts | 1 + .../members/api/useDeleteTeamAccess.ts | 64 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/frontend/apps/desk/src/features/members/api/useDeleteTeamAccess.ts diff --git a/src/frontend/apps/desk/src/features/members/api/index.ts b/src/frontend/apps/desk/src/features/members/api/index.ts index 1590028..1a8460f 100644 --- a/src/frontend/apps/desk/src/features/members/api/index.ts +++ b/src/frontend/apps/desk/src/features/members/api/index.ts @@ -1,2 +1,3 @@ +export * from './useDeleteTeamAccess'; export * from './useTeamsAccesses'; export * from './useUpdateTeamAccess'; diff --git a/src/frontend/apps/desk/src/features/members/api/useDeleteTeamAccess.ts b/src/frontend/apps/desk/src/features/members/api/useDeleteTeamAccess.ts new file mode 100644 index 0000000..0a81cfb --- /dev/null +++ b/src/frontend/apps/desk/src/features/members/api/useDeleteTeamAccess.ts @@ -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 => { + 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({ + 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); + } + }, + }); +};