'use client'; import { useState, FormEvent } from 'react'; import { useRouter } from 'next/navigation'; import Input from '../ui/Input'; import Button from '../ui/Button'; import { useServices } from '@/lib/services/ServiceProvider'; interface FormErrors { name?: string; iracingId?: string; country?: string; bio?: string; submit?: string; } export default function CreateDriverForm() { const router = useRouter(); const { driverService } = useServices(); const [loading, setLoading] = useState(false); const [errors, setErrors] = useState({}); const [formData, setFormData] = useState({ name: '', country: '', bio: '' }); const validateForm = async (): Promise => { const newErrors: FormErrors = {}; if (!formData.name.trim()) { newErrors.name = 'Name is required'; } 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 bio = formData.bio.trim(); const displayName = formData.name.trim(); const parts = displayName.split(' ').filter(Boolean); const firstName = parts[0] ?? displayName; const lastName = parts.slice(1).join(' ') || 'Driver'; await driverService.completeDriverOnboarding({ firstName, lastName, displayName, country: formData.country.trim().toUpperCase(), ...(bio ? { bio } : {}), }); 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, name: e.target.value })} error={!!errors.name} errorMessage={errors.name} placeholder="Alex Vermeer" 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