✨(app-desk) add useCreateInvitation react-query hook
Add the hook useCreateInvitation, it will be used to create an invitation.
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
export * from './useCreateInvitation';
|
||||
export * from './useTeamsAccesses';
|
||||
export * from './useUpdateTeamAccess';
|
||||
export * from './useUsers';
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user