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); + }, + }); +}