From 7347565f8d5ef0c301af54a96808926570a99e08 Mon Sep 17 00:00:00 2001 From: Anthony LC Date: Wed, 20 Mar 2024 17:24:01 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8(app-desk)=20add=20useUpdateTeam=20rea?= =?UTF-8?q?ct-query=20hook?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add the hook useUpdateTeam, it will be used to update the name of a team. --- .../apps/desk/src/features/teams/api/index.ts | 1 + .../src/features/teams/api/useUpdateTeam.tsx | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/frontend/apps/desk/src/features/teams/api/useUpdateTeam.tsx diff --git a/src/frontend/apps/desk/src/features/teams/api/index.ts b/src/frontend/apps/desk/src/features/teams/api/index.ts index 0b30a56..56a0edd 100644 --- a/src/frontend/apps/desk/src/features/teams/api/index.ts +++ b/src/frontend/apps/desk/src/features/teams/api/index.ts @@ -2,3 +2,4 @@ export * from './types'; export * from './useCreateTeam'; export * from './useTeam'; export * from './useTeams'; +export * from './useUpdateTeam'; diff --git a/src/frontend/apps/desk/src/features/teams/api/useUpdateTeam.tsx b/src/frontend/apps/desk/src/features/teams/api/useUpdateTeam.tsx new file mode 100644 index 0000000..a3891aa --- /dev/null +++ b/src/frontend/apps/desk/src/features/teams/api/useUpdateTeam.tsx @@ -0,0 +1,50 @@ +import { useMutation, useQueryClient } from '@tanstack/react-query'; + +import { APIError, errorCauses, fetchAPI } from '@/api'; + +import { Team } from './types'; +import { KEY_TEAM } from './useTeam'; +import { KEY_LIST_TEAM } from './useTeams'; + +type UpdateTeamProps = Pick; + +export const updateTeam = async ({ + name, + id, +}: UpdateTeamProps): Promise => { + const response = await fetchAPI(`teams/${id}/`, { + method: 'PATCH', + body: JSON.stringify({ + name, + }), + }); + + if (!response.ok) { + throw new APIError( + 'Failed to update the team', + await errorCauses(response), + ); + } + + return response.json() as Promise; +}; + +interface UseUpdateTeamProps { + onSuccess: (data: Team) => void; +} + +export function useUpdateTeam({ onSuccess }: UseUpdateTeamProps) { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: updateTeam, + onSuccess: (data) => { + void queryClient.invalidateQueries({ + queryKey: [KEY_LIST_TEAM], + }); + void queryClient.invalidateQueries({ + queryKey: [KEY_TEAM], + }); + onSuccess(data); + }, + }); +}