'use client'; import React, { useState } from 'react'; import { useRouter } from 'next/navigation'; import { useEvents } from '../../../../hooks/useEvents'; import Link from 'next/link'; interface EventFormData { title: string; description: string; date: string; time: string; location: string; category: 'social' | 'athletic' | 'board' | 'general' | 'educational'; status: 'published' | 'draft' | 'archived'; registrationRequired: boolean; maxAttendees?: number; recurring: boolean; recurrencePattern?: string; tags: string; } interface EventFormErrors { title?: string; description?: string; date?: string; time?: string; location?: string; category?: string; status?: string; maxAttendees?: string; recurrencePattern?: string; } export default function CreateEventPage() { const router = useRouter(); const { createEvent } = useEvents(); const [formData, setFormData] = useState({ title: '', description: '', date: '', time: '', location: '', category: 'social', status: 'draft', registrationRequired: false, maxAttendees: undefined, recurring: false, recurrencePattern: '', tags: '', }); const [errors, setErrors] = useState({}); const [isSubmitting, setIsSubmitting] = useState(false); const [submitError, setSubmitError] = useState(''); const validateForm = (): boolean => { const newErrors: EventFormErrors = {}; // Title validation if (!formData.title.trim()) { newErrors.title = 'Title is required'; } else if (formData.title.length < 3) { newErrors.title = 'Title must be at least 3 characters'; } // Description validation if (!formData.description.trim()) { newErrors.description = 'Description is required'; } // Date validation if (!formData.date) { newErrors.date = 'Date is required'; } else { const selectedDate = new Date(formData.date); const today = new Date(); today.setHours(0, 0, 0, 0); if (selectedDate < today) { newErrors.date = 'Date cannot be in the past'; } } // Time validation if (!formData.time.trim()) { newErrors.time = 'Time is required'; } // Location validation if (!formData.location.trim()) { newErrors.location = 'Location is required'; } // Category validation if (!formData.category) { newErrors.category = 'Category is required'; } // Status validation if (!formData.status) { newErrors.status = 'Status is required'; } // Max attendees validation (if registration is required) if (formData.registrationRequired && formData.maxAttendees !== undefined) { if (formData.maxAttendees <= 0) { newErrors.maxAttendees = 'Maximum attendees must be greater than 0'; } } // Recurrence pattern validation (if event is recurring) if (formData.recurring && !formData.recurrencePattern?.trim()) { newErrors.recurrencePattern = 'Recurrence pattern is required for recurring events'; } setErrors(newErrors); return Object.keys(newErrors).length === 0; }; const handleChange = ( e: React.ChangeEvent ) => { const { name, value, type } = e.target as HTMLInputElement; if (type === 'checkbox') { const checked = (e.target as HTMLInputElement).checked; setFormData({ ...formData, [name]: checked, }); } else if (name === 'maxAttendees') { setFormData({ ...formData, [name]: value ? parseInt(value, 10) : undefined, }); } else { setFormData({ ...formData, [name]: value, }); } // Clear error when user types if (errors[name as keyof EventFormErrors]) { setErrors({ ...errors, [name]: undefined, }); } }; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); if (!validateForm()) { return; } setIsSubmitting(true); setSubmitError(''); try { // Convert form data to the format expected by the API const eventData = { ...formData, tags: formData.tags.split(',').map(tag => tag.trim()).filter(tag => tag), date: new Date(formData.date).toISOString(), registeredCount: 0, // Initialize with zero registrations }; const result = await createEvent(eventData); if (result) { // Redirect to events list page on success router.push('/admin/events'); } else { setSubmitError('Failed to create event. Please try again.'); } } catch (error) { console.error('Error creating event:', error); setSubmitError('An unexpected error occurred. Please try again later.'); } finally { setIsSubmitting(false); } }; return (

Create New Event

Cancel
{/* Error message */} {submitError && (

Error

{submitError}

)}
{/* Title field */}
{errors.title && (

{errors.title}

)}
{/* Description field */}
{errors.description && (

{errors.description}

)}
{/* Date and Time fields */}
{errors.date && (

{errors.date}

)}
{errors.time && (

{errors.time}

)}
{/* Location field */}
{errors.location && (

{errors.location}

)}
{/* Category and Status fields */}
{errors.category && (

{errors.category}

)}
{errors.status && (

{errors.status}

)}
{/* Registration fields */}
{formData.registrationRequired && (
{errors.maxAttendees && (

{errors.maxAttendees}

)}
)}
{/* Recurring event fields */}
{formData.recurring && (
{errors.recurrencePattern && (

{errors.recurrencePattern}

)}
)}
{/* Tags field */}
{/* Submit button */}
Cancel

* Required fields

); }