(app-desk) add useCreateTeamAccess react-query hook

Add the hook useCreateTeamAccess, it will be used to
add a member to a team.
This commit is contained in:
Anthony LC
2024-03-15 16:24:29 +01:00
committed by Anthony LC
parent da081b9887
commit 904fae469d
2 changed files with 59 additions and 0 deletions

View File

@@ -1,4 +1,5 @@
export * from './useCreateInvitation';
export * from './useCreateTeamAccess';
export * from './useTeamsAccesses';
export * from './useUpdateTeamAccess';
export * from './useUsers';

View File

@@ -0,0 +1,58 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api';
import { User } from '@/features/auth';
import { KEY_LIST_TEAM, KEY_TEAM, Team } from '@/features/teams';
import { Access, Role } from '../types';
import { KEY_LIST_TEAM_ACCESSES } from '.';
interface CreateTeamAccessParams {
name: User['name'];
role: Role;
teamId: Team['id'];
userId: User['id'];
}
export const createTeamAccess = async ({
userId,
name,
role,
teamId,
}: CreateTeamAccessParams): Promise<Access> => {
const response = await fetchAPI(`teams/${teamId}/accesses/`, {
method: 'POST',
body: JSON.stringify({
user: userId,
role,
}),
});
if (!response.ok) {
throw new APIError(
`Failed to add ${name} in the team.`,
await errorCauses(response),
);
}
return response.json() as Promise<Access>;
};
export function useCreateTeamAccess() {
const queryClient = useQueryClient();
return useMutation<Access, APIError, CreateTeamAccessParams>({
mutationFn: createTeamAccess,
onSuccess: () => {
void queryClient.invalidateQueries({
queryKey: [KEY_LIST_TEAM],
});
void queryClient.invalidateQueries({
queryKey: [KEY_LIST_TEAM_ACCESSES],
});
void queryClient.invalidateQueries({
queryKey: [KEY_TEAM],
});
},
});
}