OrganizationInvite.utils.ts96 lines · main
| 1 | import type { OrganizationInviteByToken } from '@/data/organization-members/organization-invitation-token-query' |
| 2 | import type { ResponseError } from '@/types' |
| 3 | |
| 4 | type OrganizationInviteStatusVariables = { |
| 5 | data?: OrganizationInviteByToken |
| 6 | error?: ResponseError | null |
| 7 | isErrorInvitation: boolean |
| 8 | isLoadingInvitation: boolean |
| 9 | isLoadingProfile: boolean |
| 10 | isLoggedIn: boolean |
| 11 | isRouterReady: boolean |
| 12 | isSuccessInvitation: boolean |
| 13 | profileExists: boolean |
| 14 | } |
| 15 | |
| 16 | type OrganizationInviteContentVariables = { |
| 17 | data?: OrganizationInviteByToken |
| 18 | isSignUpEnabled: boolean |
| 19 | status: OrganizationInviteStatus |
| 20 | } |
| 21 | |
| 22 | export type OrganizationInviteStatus = |
| 23 | | 'signed-out' |
| 24 | | 'loading' |
| 25 | | 'ready' |
| 26 | | 'wrong-account' |
| 27 | | 'expired' |
| 28 | | 'invalid' |
| 29 | | 'no-longer-valid' |
| 30 | | 'error' |
| 31 | |
| 32 | export function getOrganizationInviteStatus({ |
| 33 | data, |
| 34 | error, |
| 35 | isErrorInvitation, |
| 36 | isLoadingInvitation, |
| 37 | isLoadingProfile, |
| 38 | isLoggedIn, |
| 39 | isRouterReady, |
| 40 | isSuccessInvitation, |
| 41 | profileExists, |
| 42 | }: OrganizationInviteStatusVariables): OrganizationInviteStatus { |
| 43 | const isSignedOut = !isLoggedIn || (!profileExists && !isLoadingProfile) |
| 44 | |
| 45 | if (isSignedOut) return 'signed-out' |
| 46 | if (isLoadingProfile || isLoadingInvitation || !isRouterReady) return 'loading' |
| 47 | |
| 48 | if (error?.code === 401 && error?.message.includes('Failed to retrieve organization')) { |
| 49 | return 'no-longer-valid' |
| 50 | } |
| 51 | |
| 52 | if ( |
| 53 | (isSuccessInvitation && !!data?.token_does_not_exist) || |
| 54 | (isErrorInvitation && error?.code === 404) |
| 55 | ) { |
| 56 | return 'invalid' |
| 57 | } |
| 58 | |
| 59 | if (isErrorInvitation) return 'error' |
| 60 | if (isSuccessInvitation && !!data?.expired_token) return 'expired' |
| 61 | if (isSuccessInvitation && !!data && !data.email_match) return 'wrong-account' |
| 62 | |
| 63 | return 'ready' |
| 64 | } |
| 65 | |
| 66 | export function getOrganizationInviteContent({ |
| 67 | data, |
| 68 | isSignUpEnabled, |
| 69 | status, |
| 70 | }: OrganizationInviteContentVariables) { |
| 71 | const signedOutDescription = `Sign in${ |
| 72 | isSignUpEnabled ? ' or create an account' : '' |
| 73 | } to view this invitation` |
| 74 | |
| 75 | if (status === 'signed-out') { |
| 76 | return { |
| 77 | title: 'View invitation', |
| 78 | description: signedOutDescription, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | if (status === 'ready') { |
| 83 | return { |
| 84 | title: `Join ${data?.organization_name ?? 'an organization'}`, |
| 85 | description: 'You have been invited to join this Briven organization', |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (status === 'wrong-account') return { title: 'Wrong account' } |
| 90 | if (status === 'expired') return { title: 'Invite expired' } |
| 91 | if (status === 'invalid') return { title: 'Invite invalid' } |
| 92 | if (status === 'no-longer-valid') return { title: 'Invite no longer available' } |
| 93 | if (status === 'error') return { title: 'Unable to load invitation' } |
| 94 | |
| 95 | return {} |
| 96 | } |