import type { TeamDocument } from './CreateTeamForm'; import { NextButton } from './NextButton'; import { TrashIcon } from '../ReactIcons/TrashIcon'; import { type AllowedRoles, RoleDropdown } from './RoleDropdown'; import { useEffect, useRef, useState } from 'react'; import { httpPost } from '../../lib/http'; type Step3Props = { team?: TeamDocument; onNext: () => void; onBack: () => void; }; type InviteType = { id: string; email: string; role: AllowedRoles; }; function generateId() { return `${new Date().getTime()}`; } export function Step3(props: Step3Props) { const { onNext, onBack, team } = props; const [error, setError] = useState(''); const [invitingTeam, setInvitingTeam] = useState(false); const emailInputRef = useRef(null); const [users, setUsers] = useState([ { id: generateId(), email: '', role: 'member', }, ]); async function inviteTeam() { setInvitingTeam(true); const { error, response } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-invite-team/${team?._id}`, { members: users, } ); if (error || !response) { setError(error?.message || 'Something went wrong'); setInvitingTeam(false); return; } onNext(); } function focusLastEmailInput() { if (!emailInputRef.current) { return; } (emailInputRef.current as HTMLInputElement).focus(); } function onSubmit(e: any) { e.preventDefault(); inviteTeam().finally(() => null); } useEffect(() => { focusLastEmailInput(); }, [users.length]); return (

Invite your Team

Use the form below to invite your team members to your team. You can also invite them later.

{users.map((user, userCounter) => { return (
{ const newUsers = users.map((u) => { if (u.id === user.id) { return { ...u, email: (e.target as HTMLInputElement)?.value, }; } return u; }); setUsers(newUsers); }} className="flex-grow rounded-md border border-gray-200 bg-white px-4 py-2 text-gray-900" /> { const newUsers = users.map((u) => { if (u.id === user.id) { return { ...u, role, }; } return u; }); setUsers(newUsers); }} />
); })}
{users.length <= 30 && ( )} {error && (
{error}
)}
); }