✨(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:
@@ -1,4 +1,5 @@
|
||||
export * from './useCreateInvitation';
|
||||
export * from './useCreateTeamAccess';
|
||||
export * from './useTeamsAccesses';
|
||||
export * from './useUpdateTeamAccess';
|
||||
export * from './useUsers';
|
||||
|
||||
@@ -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],
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user