|
| 1 | +import { href } from 'react-router'; |
| 2 | +import { v4 as uuidv4 } from 'uuid'; |
| 3 | + |
| 4 | +import { userSession } from '~/models'; |
| 5 | +import { insomniaFetch } from '~/ui/insomniaFetch'; |
| 6 | +import { createFetcherLoadHook } from '~/utils/router'; |
| 7 | + |
| 8 | +import type { Route } from './+types/organization.$organizationId.collaborators-check-seats'; |
| 9 | + |
| 10 | +export const needsToUpgrade = 'NEEDS_TO_UPGRADE'; |
| 11 | +export const needsToIncreaseSeats = 'NEEDS_TO_INCREASE_SEATS'; |
| 12 | + |
| 13 | +export interface CheckSeatsResponse { |
| 14 | + isAllowed: boolean; |
| 15 | + code?: typeof needsToUpgrade | typeof needsToIncreaseSeats; |
| 16 | +} |
| 17 | + |
| 18 | +export async function clientLoader({ params }: Route.ClientLoaderArgs) { |
| 19 | + const { id: sessionId } = await userSession.get(); |
| 20 | + |
| 21 | + const { organizationId } = params; |
| 22 | + |
| 23 | + try { |
| 24 | + // Check whether the user can add a new collaborator |
| 25 | + // Use a random email to avoid hitting any existing member emails |
| 26 | + const checkResponseData = await insomniaFetch<CheckSeatsResponse>({ |
| 27 | + method: 'POST', |
| 28 | + path: `/v1/organizations/${organizationId}/check-seats`, |
| 29 | + data: { emails: [`insomnia-mock-check-seats-${uuidv4()}@example.net`] }, |
| 30 | + sessionId, |
| 31 | + onlyResolveOnSuccess: true, |
| 32 | + }); |
| 33 | + return checkResponseData; |
| 34 | + } catch { |
| 35 | + return { isAllowed: true }; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +export const useCollaboratorsCheckSeatsLoaderFetcher = createFetcherLoadHook( |
| 40 | + load => |
| 41 | + ({ organizationId, query }: { organizationId: string; query?: string }) => { |
| 42 | + return load( |
| 43 | + `${href(`/organization/:organizationId/collaborators-check-seats`, { organizationId })}?${encodeURIComponent(query || '')}`, |
| 44 | + ); |
| 45 | + }, |
| 46 | + clientLoader, |
| 47 | +); |
0 commit comments