'use client' import 'react-datepicker/dist/react-datepicker.css' import clsx from 'clsx' import {APIError} from 'common/api/utils' import {useEffect, useState} from 'react' import DatePicker from 'react-datepicker' import {Col} from 'web/components/layout/col' import {Modal, MODAL_CLASS, SCROLLABLE_MODAL_CLASS} from 'web/components/layout/modal' import {Event} from 'web/hooks/use-events' import {api} from 'web/lib/api' import {useLocale, useT} from 'web/lib/locale' export function CreateEventModal(props: { open: boolean setOpen: (open: boolean) => void onClose: () => void onSuccess: () => void event?: Event | null | undefined }) { const {open, setOpen, onClose, onSuccess, event} = props const isEditing = !!event const t = useT() const {locale} = useLocale() const [loading, setLoading] = useState(false) const [error, setError] = useState(null) const [formData, setFormData] = useState({ title: '', description: '', locationType: 'in_person' as 'in_person' | 'online', locationAddress: '', locationUrl: '', eventStartTime: null as Date | null, eventEndTime: null as Date | null, maxParticipants: '', }) // Update form data when event prop changes (for editing) useEffect(() => { if (event) { setFormData({ title: event.title || '', description: event.description || '', locationType: event.location_type || ('in_person' as 'in_person' | 'online'), locationAddress: event.location_address || '', locationUrl: event.location_url || '', eventStartTime: event.event_start_time ? new Date(event.event_start_time) : null, eventEndTime: event.event_end_time ? new Date(event.event_end_time) : null, maxParticipants: event.max_participants?.toString() || '', }) } else { // Reset form for create mode setFormData({ title: '', description: '', locationType: 'in_person' as 'in_person' | 'online', locationAddress: '', locationUrl: '', eventStartTime: null, eventEndTime: null, maxParticipants: '', }) } }, [event]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError(null) try { if (isEditing && event) { await api('update-event', { eventId: event.id, title: formData.title, description: formData.description || undefined, locationType: formData.locationType, locationAddress: (formData.locationType === 'in_person' && formData.locationAddress) || undefined, locationUrl: (formData.locationType === 'online' && formData.locationUrl) || undefined, eventStartTime: formData.eventStartTime!.toISOString(), eventEndTime: formData.eventEndTime ? formData.eventEndTime.toISOString() : undefined, maxParticipants: formData.maxParticipants ? parseInt(formData.maxParticipants) : undefined, }) } else { await api('create-event', { title: formData.title, description: formData.description || undefined, locationType: formData.locationType, locationAddress: (formData.locationType === 'in_person' && formData.locationAddress) || undefined, locationUrl: (formData.locationType === 'online' && formData.locationUrl) || undefined, eventStartTime: formData.eventStartTime!.toISOString(), eventEndTime: formData.eventEndTime ? formData.eventEndTime.toISOString() : undefined, maxParticipants: formData.maxParticipants ? parseInt(formData.maxParticipants) : undefined, }) } onSuccess() onClose() // Reset form only for create, not edit if (!isEditing) { setFormData({ title: '', description: '', locationType: 'in_person', locationAddress: '', locationUrl: '', eventStartTime: null, eventEndTime: null, maxParticipants: '', }) } } catch (err) { if (err instanceof APIError) { setError(err.message) } else { setError(t('events.failed_create_event', 'Failed to save event. Please try again.')) } } finally { setLoading(false) } } const handleChange = ( e: React.ChangeEvent, ) => { const {name, value} = e.target setFormData((prev) => ({...prev, [name]: value})) } const dateFormat = locale === 'en' ? 'MMM d, yyyy h:mm aa' : 'dd MMM yyyy, HH:mm' const timeFormat = 'HH:mm' return (

{isEditing ? t('events.edit_event', 'Edit Event') : t('events.create_new_event', 'Create New Event')}