|
| 1 | +import { Res } from 'common/types/responses' |
| 2 | +import { Req } from 'common/types/requests' |
| 3 | +import { service } from 'common/service' |
| 4 | +import Constants from 'common/constants' |
| 5 | +import API from 'project/api' |
| 6 | + |
| 7 | +export const invitesService = service |
| 8 | + .enhanceEndpoints({ addTagTypes: ['Invite'] }) |
| 9 | + .injectEndpoints({ |
| 10 | + endpoints: (builder) => ({ |
| 11 | + createUserInvite: builder.mutation< |
| 12 | + Res['createdUserInvite'], |
| 13 | + Req['createUserInvite'] |
| 14 | + >({ |
| 15 | + invalidatesTags: [{ id: 'LIST', type: 'Invite' }], |
| 16 | + query: (query: Req['createUserInvite']) => ({ |
| 17 | + body: { |
| 18 | + frontend_base_url: `${document.location.origin}/email-invite/`, |
| 19 | + invites: query.invites.map((invite) => { |
| 20 | + API.trackEvent(Constants.events.INVITE) |
| 21 | + return invite |
| 22 | + }), |
| 23 | + }, |
| 24 | + method: 'POST', |
| 25 | + url: `organisations/${query.organisationId}/invite/`, |
| 26 | + }), |
| 27 | + }), |
| 28 | + deleteUserInvite: builder.mutation<{}, Req['deleteUserInvite']>({ |
| 29 | + invalidatesTags: [{ id: 'LIST', type: 'Invite' }], |
| 30 | + query: (query: Req['deleteUserInvite']) => { |
| 31 | + API.trackEvent(Constants.events.DELETE_INVITE) |
| 32 | + return { |
| 33 | + method: 'DELETE', |
| 34 | + url: `organisations/${query.organisationId}/invites/${query.inviteId}/`, |
| 35 | + } |
| 36 | + }, |
| 37 | + }), |
| 38 | + getUserInvites: builder.query<Res['userInvites'], Req['getUserInvites']>({ |
| 39 | + providesTags: [{ id: 'LIST', type: 'Invite' }], |
| 40 | + query: (query: Req['getUserInvites']) => ({ |
| 41 | + url: `organisations/${query.organisationId}/invites/`, |
| 42 | + }), |
| 43 | + }), |
| 44 | + resendUserInvite: builder.mutation<{}, Req['resendUserInvite']>({ |
| 45 | + invalidatesTags: [{ id: 'LIST', type: 'Invite' }], |
| 46 | + query: (query: Req['resendUserInvite']) => { |
| 47 | + API.trackEvent(Constants.events.RESEND_INVITE) |
| 48 | + return { |
| 49 | + method: 'POST', |
| 50 | + url: `organisations/${query.organisationId}/invites/${query.inviteId}/resend/`, |
| 51 | + } |
| 52 | + }, |
| 53 | + }), |
| 54 | + }), |
| 55 | + }) |
| 56 | + |
| 57 | +export async function createUserInvite( |
| 58 | + store: any, |
| 59 | + data: Req['createUserInvite'], |
| 60 | + options?: Parameters< |
| 61 | + typeof invitesService.endpoints.createUserInvite.initiate |
| 62 | + >[1], |
| 63 | +) { |
| 64 | + return store.dispatch( |
| 65 | + invitesService.endpoints.createUserInvite.initiate(data, options), |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +export async function deleteUserInvite( |
| 70 | + store: any, |
| 71 | + data: Req['deleteUserInvite'], |
| 72 | + options?: Parameters< |
| 73 | + typeof invitesService.endpoints.deleteUserInvite.initiate |
| 74 | + >[1], |
| 75 | +) { |
| 76 | + return store.dispatch( |
| 77 | + invitesService.endpoints.deleteUserInvite.initiate(data, options), |
| 78 | + ) |
| 79 | +} |
| 80 | + |
| 81 | +export async function getUserInvites( |
| 82 | + store: any, |
| 83 | + data: Req['getUserInvites'], |
| 84 | + options?: Parameters< |
| 85 | + typeof invitesService.endpoints.getUserInvites.initiate |
| 86 | + >[1], |
| 87 | +) { |
| 88 | + return store.dispatch( |
| 89 | + invitesService.endpoints.getUserInvites.initiate(data, options), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +export async function resendUserInvite( |
| 94 | + store: any, |
| 95 | + data: Req['resendUserInvite'], |
| 96 | + options?: Parameters< |
| 97 | + typeof invitesService.endpoints.resendUserInvite.initiate |
| 98 | + >[1], |
| 99 | +) { |
| 100 | + return store.dispatch( |
| 101 | + invitesService.endpoints.resendUserInvite.initiate(data, options), |
| 102 | + ) |
| 103 | +} |
| 104 | + |
| 105 | +export const { |
| 106 | + useCreateUserInviteMutation, |
| 107 | + useDeleteUserInviteMutation, |
| 108 | + useGetUserInvitesQuery, |
| 109 | + useResendUserInviteMutation, |
| 110 | +} = invitesService |
0 commit comments