import { useEffect, useState } from 'react'; import { activateEvent, getAllEvents, upsertEvent } from '../lib/api'; import type { Event } from '../lib/types'; import { useToast } from '../context/ToastContext'; import { Empty, Modal, Spinner } from '../components/ui'; export function Events() { const { push } = useToast(); const [events, setEvents] = useState([]); const [loaded, setLoaded] = useState(false); const [error, setError] = useState(null); const [modal, setModal] = useState<{ event: Event | null } | null>(null); const [busy, setBusy] = useState(false); async function load() { try { setEvents(await getAllEvents()); setLoaded(true); } catch (e) { setError(e instanceof Error ? e.message : 'Failed to load'); } } useEffect(() => { load(); }, []); async function doActivate(ev: Event) { if (!confirm(`Set "${ev.name}" as the active event? All other events will be deactivated.`)) return; setBusy(true); try { await activateEvent(ev.id); await load(); push('success', `${ev.name} is now active`); } catch (e) { push('error', e instanceof Error ? e.message : 'Failed to activate'); } finally { setBusy(false); } } if (error) return ; if (!loaded) return (
); return (

Events

{events.length} event{events.length === 1 ? '' : 's'} · only one can be active at a time.
{events.length === 0 ? ( ) : (
{events.map((ev) => ( ))}
Event Dates Location Stats Status
{ev.name}
{ev.is_active && active}
{fmtDate(ev.start_date)} – {fmtDate(ev.end_date)} {ev.location || '—'} {ev.num_registrations} reg · {ev.num_sessions} sessions · {ev.num_speakers} speakers {ev.avg_feedback != null && ` · ⭐ ${ev.avg_feedback.toFixed(1)}`} {ev.is_active ? ( Active ) : ( )}
)}
{modal && ( setModal(null)} onSaved={async (msg) => { setModal(null); await load(); push('success', msg); }} /> )}
); } function EventModal({ event, onClose, onSaved, }: { event: Event | null; onClose: () => void; onSaved: (msg: string) => void; }) { const [name, setName] = useState(event?.name ?? ''); const [startDate, setStartDate] = useState(event ? event.start_date.slice(0, 10) : ''); const [endDate, setEndDate] = useState(event ? event.end_date.slice(0, 10) : ''); const [location, setLocation] = useState(event?.location ?? ''); const [busy, setBusy] = useState(false); async function save() { if (!name.trim() || !startDate || !endDate) return; setBusy(true); try { await upsertEvent(event?.id ?? null, { name: name.trim(), start_date: startDate, end_date: endDate, location: location.trim() || null, }); onSaved(event ? 'Event updated' : 'Event created'); } catch (e) { onSaved(e instanceof Error ? e.message : 'Save failed'); } finally { setBusy(false); } } return ( } >
setName(e.target.value)} placeholder="e.g. APPE 2027" />
setStartDate(e.target.value)} />
setEndDate(e.target.value)} />
setLocation(e.target.value)} placeholder="—" />
); } function fmtDate(d: string): string { return new Date(d + 'T00:00:00').toLocaleDateString([], { month: 'short', day: 'numeric', year: 'numeric' }); }