import { useEffect, useState } from 'react'; import { httpGet, httpPatch } from '../lib/http'; import { pageProgressMessage } from '../stores/page'; import type { TeamDocument } from './CreateTeam/CreateTeamForm'; import type { AllowedRoles } from './CreateTeam/RoleDropdown'; import type { AllowedMemberStatus } from './TeamDropdown/TeamDropdown'; import { isLoggedIn } from '../lib/jwt'; import { showLoginPopup } from '../lib/popup'; import { getUrlParams } from '../lib/browser'; import { ErrorIcon2 } from './ReactIcons/ErrorIcon2'; import { BuildingIcon } from './ReactIcons/BuildingIcon'; type InvitationResponse = { team: TeamDocument; invite: { _id?: string; userId?: string; invitedEmail?: string; teamId: string; role: AllowedRoles; status: AllowedMemberStatus; createdAt: Date; updatedAt: Date; }; }; export function RespondInviteForm() { const { i: inviteId } = getUrlParams(); const [isLoadingInvite, setIsLoadingInvite] = useState(true); const [error, setError] = useState(''); const [invite, setInvite] = useState(); const isAuthenticated = isLoggedIn(); async function loadInvitation(inviteId: string) { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-invitation/${inviteId}` ); if (error || !response) { setError(error?.message || 'Something went wrong'); return; } setInvite(response); } useEffect(() => { if (inviteId) { loadInvitation(inviteId).finally(() => { pageProgressMessage.set(''); setIsLoadingInvite(false); }); } else { setIsLoadingInvite(false); setError('Missing invite ID in URL'); pageProgressMessage.set(''); } }, [inviteId]); async function respondInvitation(status: 'accept' | 'reject') { pageProgressMessage.set('Please wait...'); setError(''); const { response, error } = await httpPatch<{ teamId: string }>( `${import.meta.env.PUBLIC_API_URL}/v1-respond-invite/${inviteId}`, { status, } ); if (error || !response) { setError(error?.message || 'Something went wrong'); return; } if (status === 'reject') { window.location.href = '/'; return; } window.location.href = `/team/progress?t=${response.teamId}`; } if (isLoadingInvite) { return null; } if (!invite) { return (

Error

{error || 'There was a problem, please try again.'}

Back to home
); } return (

Join Team

You have been invited to join the team{' '} {invite?.team?.name}.

{!isAuthenticated && (
)} {isAuthenticated && (
{error && (

{error}

)}
)}
); }