'use client'; import { useState } from 'react'; import { api, Event } from '@/lib/api'; import { useRouter } from 'next/navigation'; interface EventFormProps { event?: Event; } export function EventForm({ event }: EventFormProps) { const [name, setName] = useState(event?.name || ''); const [description, setDescription] = useState(event?.description || ''); const [eventDate, setEventDate] = useState(event ? new Date(event.eventDate).toISOString().slice(0, 16) : ''); const [location, setLocation] = useState(event?.location || ''); const [category, setCategory] = useState(event?.category || ''); const [maxParticipants, setMaxParticipants] = useState(event?.maxParticipants?.toString() || ''); const [tags, setTags] = useState(event?.tags.join(', ') || ''); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const router = useRouter(); const categories = ['Running', 'Cycling', 'Triathlon', 'Trail', 'Road']; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setError(null); setIsSubmitting(true); try { const eventData = { name, description, eventDate: new Date(eventDate).toISOString(), location, category: category || undefined, maxParticipants: maxParticipants ? parseInt(maxParticipants) : undefined, tags: tags.split(',').map(t => t.trim()).filter(t => t), }; if (event) { await api.updateEvent(event.id, eventData); } else { await api.createEvent(eventData); } router.push('/events'); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to save event'); } finally { setIsSubmitting(false); } }; return (
{error && (
{error}
)}
setName(e.target.value)} required className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500" />