(app-desk) add useCreateInvitation react-query hook

Add the hook useCreateInvitation, it will be used to
create an invitation.
This commit is contained in:
Anthony LC
2024-03-15 11:23:58 +01:00
committed by Anthony LC
parent bbf2695dee
commit bb9edd21da
3 changed files with 54 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,42 @@
import { useMutation } from '@tanstack/react-query';
import { APIError, errorCauses, fetchAPI } from '@/api';
import { User } from '@/features/auth';
import { Team } from '@/features/teams';
import { Invitation, Role } from '../types';
interface CreateInvitationParams {
email: User['email'];
role: Role;
teamId: Team['id'];
}
export const createInvitation = async ({
email,
role,
teamId,
}: CreateInvitationParams): Promise<Invitation> => {
const response = await fetchAPI(`teams/${teamId}/invitations/`, {
method: 'POST',
body: JSON.stringify({
email,
role,
}),
});
if (!response.ok) {
throw new APIError(
`Failed to create the invitation for ${email}`,
await errorCauses(response, email),
);
}
return response.json() as Promise<Invitation>;
};
export function useCreateInvitation() {
return useMutation<Invitation, APIError, CreateInvitationParams>({
mutationFn: createInvitation,
});
}

View File

@@ -1,4 +1,5 @@
import { User } from '@/features/auth/';
import { Team } from '@/features/teams/';
export enum Role {
MEMBER = 'member',
@@ -18,3 +19,13 @@ export interface Access {
set_role_to: Role[];
};
}
export interface Invitation {
id: string;
created_at: string;
email: string;
team: Team['id'];
role: Role;
issuer: User['id'];
is_expired: boolean;
}