'use client'; import { useState, FormEvent } from 'react'; import { useRouter } from 'next/navigation'; import Input from '../ui/Input'; import Button from '../ui/Button'; import { Driver } from '@gridpilot/racing/domain/entities/Driver'; import { getDriverRepository } from '../../lib/di-container'; interface FormErrors { name?: string; iracingId?: string; country?: string; bio?: string; submit?: string; } export default function CreateDriverForm() { const router = useRouter(); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState({}); const [formData, setFormData] = useState({ name: '', iracingId: '', country: '', bio: '' }); const validateForm = async (): Promise => { const newErrors: FormErrors = {}; if (!formData.name.trim()) { newErrors.name = 'Name is required'; } if (!formData.iracingId.trim()) { newErrors.iracingId = 'iRacing ID is required'; } else { const driverRepo = getDriverRepository(); const exists = await driverRepo.existsByIRacingId(formData.iracingId); if (exists) { newErrors.iracingId = 'This iRacing ID is already registered'; } } if (!formData.country.trim()) { newErrors.country = 'Country is required'; } else if (!/^[A-Z]{2,3}$/i.test(formData.country)) { newErrors.country = 'Invalid country code (use 2-3 letter ISO code)'; } if (formData.bio && formData.bio.length > 500) { newErrors.bio = 'Bio must be 500 characters or less'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleSubmit = async (e: FormEvent) => { e.preventDefault(); if (loading) return; const isValid = await validateForm(); if (!isValid) return; setLoading(true); try { const driverRepo = getDriverRepository(); const driver = Driver.create({ id: crypto.randomUUID(), iracingId: formData.iracingId.trim(), name: formData.name.trim(), country: formData.country.trim().toUpperCase(), bio: formData.bio.trim() || undefined, }); await driverRepo.create(driver); router.push('/profile'); router.refresh(); } catch (error) { setErrors({ submit: error instanceof Error ? error.message : 'Failed to create profile' }); setLoading(false); } }; return ( <>
setFormData({ ...formData, name: e.target.value })} error={!!errors.name} errorMessage={errors.name} placeholder="Alex Vermeer" disabled={loading} />
setFormData({ ...formData, iracingId: e.target.value })} error={!!errors.iracingId} errorMessage={errors.iracingId} placeholder="123456" disabled={loading} />
setFormData({ ...formData, country: e.target.value })} error={!!errors.country} errorMessage={errors.country} placeholder="NL" maxLength={3} disabled={loading} />

Use ISO 3166-1 alpha-2 or alpha-3 code