import { type FormEvent, useEffect, useRef, useState } from 'react'; import { type AppError, httpPost, httpPut } from '../../lib/http'; import type { ValidTeamType } from './Step0'; import type { TeamDocument } from './CreateTeamForm'; import { NextButton } from './NextButton'; export const validTeamSizes = [ '1-5', '6-10', '11-25', '26-50', '51-100', '101-200', '201-500', '501-1000', '1000+', ] as const; export type ValidTeamSize = (typeof validTeamSizes)[number]; type Step1Props = { team?: TeamDocument; selectedTeamType: ValidTeamType; onStepComplete: (team: TeamDocument) => void; onBack: () => void; }; export function Step1(props: Step1Props) { const { team, selectedTeamType, onBack, onStepComplete } = props; const [error, setError] = useState(''); const nameRef = useRef(null); useEffect(() => { if (!nameRef.current) { return; } nameRef.current.focus(); }, [nameRef]); const [isLoading, setIsLoading] = useState(false); const [name, setName] = useState(team?.name || ''); const [website, setWebsite] = useState(team?.links?.website || ''); const [linkedInUrl, setLinkedInUrl] = useState(team?.links?.linkedIn || ''); const [gitHubUrl, setGitHubUrl] = useState(team?.links?.github || ''); const [teamSize, setTeamSize] = useState( team?.teamSize || ('' as any), ); const handleSubmit = async (e: FormEvent) => { e.preventDefault(); setIsLoading(true); if (!name || !selectedTeamType) { setIsLoading(false); return; } let response: TeamDocument | undefined; let error: AppError | undefined; if (!team?._id) { ({ response, error } = await httpPost( `${import.meta.env.PUBLIC_API_URL}/v1-create-team`, { name, website: website || undefined, type: selectedTeamType, gitHubUrl: gitHubUrl || undefined, ...(selectedTeamType === 'company' && { teamSize, linkedInUrl: linkedInUrl || undefined, }), roadmapIds: [], bestPracticeIds: [], }, )); if (error || !response?._id) { setError(error?.message || 'Something went wrong. Please try again.'); setIsLoading(false); return; } onStepComplete(response as TeamDocument); } else { ({ response, error } = await httpPut( `${import.meta.env.PUBLIC_API_URL}/v1-update-team/${team._id}`, { name, website: website || undefined, type: selectedTeamType, gitHubUrl: gitHubUrl || undefined, ...(selectedTeamType === 'company' && { teamSize, linkedInUrl: linkedInUrl || undefined, }), }, )); if (error || (response as any)?.status !== 'ok') { setError(error?.message || 'Something went wrong. Please try again.'); setIsLoading(false); return; } onStepComplete({ ...team, name, _id: team._id, links: { website: website || team?.links?.website, linkedIn: linkedInUrl || team?.links?.linkedIn, github: gitHubUrl || team?.links?.github, }, type: selectedTeamType, teamSize: teamSize!, }); } }; return (
setName((e.target as HTMLInputElement).value)} />
{selectedTeamType === 'company' && (
setWebsite((e.target as HTMLInputElement).value)} />
)} {selectedTeamType === 'company' && (
setLinkedInUrl((e.target as HTMLInputElement).value) } />
)}
setGitHubUrl((e.target as HTMLInputElement).value)} />
{selectedTeamType === 'company' && (
)} {error && (
{error}
)}
); }