import { useState } from 'react' import { Link } from 'react-router-dom' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { getShowEvents, createShowEvent, addShowToEvent } from '../api' import Button from './Button' // Modal launched from a row's "Add to Event" button in the Shows browser. // Lets the user drop the ARBA show into an existing event or spin up a new one // inline (design doc §6 "Adding Shows to an Event Flow"). function AddToEventModal({ show, onClose }) { const queryClient = useQueryClient() const { data: events, isLoading } = useQuery({ queryKey: ['show-events'], queryFn: getShowEvents }) const [mode, setMode] = useState('existing') // 'existing' | 'new' const [selectedId, setSelectedId] = useState('') const [newName, setNewName] = useState('') const [newNotes, setNewNotes] = useState('') const [result, setResult] = useState(null) // { id, name } once added // No events yet → force the create path. const hasEvents = (events?.length ?? 0) > 0 const effectiveMode = hasEvents ? mode : 'new' const mutation = useMutation({ mutationFn: async () => { let eventId let eventName if (effectiveMode === 'new') { const created = await createShowEvent({ name: newName.trim(), notes: newNotes.trim() || null }) eventId = created.id eventName = created.name } else { eventId = Number(selectedId) eventName = events.find((e) => e.id === eventId)?.name ?? 'event' } await addShowToEvent(eventId, show) return { id: eventId, name: eventName } }, onSuccess: (added) => { setResult(added) queryClient.invalidateQueries({ queryKey: ['show-events'] }) queryClient.invalidateQueries({ queryKey: ['show-event', added.id] }) }, }) const canSubmit = effectiveMode === 'new' ? newName.trim().length > 0 : selectedId !== '' return (
{show.club_name} was added to{' '} {result.name}.
{show.club_name} — {show.raw_date || show.date}
{mutation.error.message}
)}