import { useEffect, useState } from 'react'; import { useAuth } from '../context/AuthContext'; import { useEvent } from '../lib/useEvent'; import { getMyEventFeedback, submitEventFeedback } from '../lib/api'; import { Spinner } from '../components/ui'; import { useToast } from '../context/ToastContext'; /** * Event-level feedback (distinct from the per-session Feedback screen). * Any registered participant can rate the event overall (1–5) + leave a * comment. Idempotent: one row per (event, contact) — re-submitting updates * the existing row. */ export function EventFeedback({ onDone }: { onDone: () => void }) { const { contact } = useAuth(); const { event, loading } = useEvent(); const [rating, setRating] = useState(0); const [comment, setComment] = useState(''); const [busy, setBusy] = useState(false); const [submitted, setSubmitted] = useState(false); const { toast } = useToast(); // Pre-fill with the user's existing event feedback (if any). useEffect(() => { let active = true; (async () => { try { const existing = await getMyEventFeedback(); if (!active || !existing) return; setRating(existing.rating); setComment(existing.comment ?? ''); setSubmitted(true); } catch { /* non-fatal */ } })(); return () => { active = false; }; }, []); if (loading || !event) { return ( ); } async function submit() { if (rating === 0) return; setBusy(true); try { await submitEventFeedback(rating, comment.trim()); toast('Thanks for your feedback! ⭐', 'ok'); setSubmitted(true); onDone(); } catch (e) { toast(e instanceof Error ? e.message : 'Could not submit feedback', 'err'); } finally { setBusy(false); } } return ( ← Back Rate this event {event.name} {event.location ? `${event.location} · ` : ''} {event.start_date} {[1, 2, 3, 4, 5].map((n) => ( setRating(n)} aria-label={`${n} star${n > 1 ? 's' : ''}`} > ★ ))} setComment(e.target.value)} rows={4} /> {busy ? 'Submitting…' : submitted ? 'Update feedback' : 'Submit feedback'} {contact ? `Signing in as ${contact.full_name}` : ''} ); }