(app-desk) add useUpdateTeam react-query hook

Add the hook useUpdateTeam, it will be used to
update the name of a team.
This commit is contained in:
Anthony LC
2024-03-20 17:24:01 +01:00
committed by Anthony LC
parent 2d50920a48
commit 7347565f8d
2 changed files with 51 additions and 0 deletions

View File

@@ -2,3 +2,4 @@ export * from './types';
export * from './useCreateTeam';
export * from './useTeam';
export * from './useTeams';
export * from './useUpdateTeam';

View File

@@ -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<Team, 'name' | 'id'>;
export const updateTeam = async ({
name,
id,
}: UpdateTeamProps): Promise<Team> => {
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<Team>;
};
interface UseUpdateTeamProps {
onSuccess: (data: Team) => void;
}
export function useUpdateTeam({ onSuccess }: UseUpdateTeamProps) {
const queryClient = useQueryClient();
return useMutation<Team, APIError, UpdateTeamProps>({
mutationFn: updateTeam,
onSuccess: (data) => {
void queryClient.invalidateQueries({
queryKey: [KEY_LIST_TEAM],
});
void queryClient.invalidateQueries({
queryKey: [KEY_TEAM],
});
onSuccess(data);
},
});
}