import { useCallback, useEffect, useState } from "react"; import { deleteAdminRsvp, fetchAdminRsvps, getExportCsvUrl, updateAdminRsvp, } from "../api/client"; import { Button } from "../components/Button"; import { Spinner } from "../components/Spinner"; import type { RSVPAdminItem } from "../types/config"; export function RsvpTable() { const [rsvps, setRsvps] = useState([]); const [count, setCount] = useState(0); const [search, setSearch] = useState(""); const [loading, setLoading] = useState(true); const [deletingId, setDeletingId] = useState(null); const [editingId, setEditingId] = useState(null); const [editForm, setEditForm] = useState<{ name: string; phone: string; email: string }>({ name: "", phone: "", email: "", }); const [savingId, setSavingId] = useState(null); const [error, setError] = useState(null); const loadData = useCallback(async (searchTerm = "") => { setLoading(true); setError(null); try { const data = await fetchAdminRsvps(searchTerm); setRsvps(data.rsvps); setCount(data.count); } catch (err) { setError(err instanceof Error ? err.message : "Error al cargar las respuestas."); } finally { setLoading(false); } }, []); useEffect(() => { loadData(search); }, [search, loadData]); function formatDate(isoStr: string) { if (!isoStr) return "—"; try { const d = new Date(isoStr); return d.toLocaleDateString("es-CO", { year: "numeric", month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", }); } catch { return isoStr; } } function formatPhone(phone: string) { if (phone.length === 10) { return `${phone.slice(0, 3)} ${phone.slice(3, 6)} ${phone.slice(6)}`; } return phone; } async function handleDelete(id: number, name: string) { const confirmed = window.confirm( `¿Estás seguro de que deseas eliminar la confirmación de "${name}"? Esta acción no se puede deshacer.`, ); if (!confirmed) return; setDeletingId(id); setError(null); try { await deleteAdminRsvp(id); setRsvps((prev) => prev.filter((r) => r.id !== id)); setCount((prev) => Math.max(0, prev - 1)); } catch (err) { setError( err instanceof Error ? err.message : "Error al eliminar el asistente.", ); } finally { setDeletingId(null); } } function startEditing(row: RSVPAdminItem) { setEditingId(row.id); setEditForm({ name: row.name, phone: row.phone, email: row.email || "", }); setError(null); } function cancelEditing() { setEditingId(null); setError(null); } async function handleSaveEdit(id: number) { if (!editForm.name.trim()) { setError("El nombre no puede estar vacío."); return; } if (!editForm.phone.trim()) { setError("El teléfono no puede estar vacío."); return; } setSavingId(id); setError(null); try { const updated = await updateAdminRsvp(id, { name: editForm.name.trim(), phone: editForm.phone.trim(), email: editForm.email.trim() || null, }); setRsvps((prev) => prev.map((r) => (r.id === id ? updated : r))); setEditingId(null); } catch (err) { setError( err instanceof Error ? err.message : "Error al actualizar el asistente.", ); } finally { setSavingId(null); } } return (
{/* Top action bar: Count, Search, Export */}

Asistentes Confirmados

Total de registros:{" "} {count}

setSearch(e.target.value)} style={{ padding: "0.6rem 1rem", borderRadius: "4px", backgroundColor: "rgba(255, 255, 255, 0.08)", border: "1px solid rgba(255, 255, 255, 0.2)", color: "#ffffff", fontSize: "0.88rem", outline: "none", minWidth: "220px", }} />
{error && (
{error}
)} {/* Table container */}
{loading ? ( ) : rsvps.length === 0 ? ( ) : ( rsvps.map((row) => ( { if (editingId !== row.id) { e.currentTarget.style.backgroundColor = "rgba(255, 255, 255, 0.04)"; } }} onMouseLeave={(e) => { if (editingId !== row.id) { e.currentTarget.style.backgroundColor = "transparent"; } }} > )) )}
Nombre Teléfono Correo Fecha de Registro Acciones
{search ? "No se encontraron coincidencias." : "Aún no hay confirmaciones registradas."}
{editingId === row.id ? ( setEditForm((prev) => ({ ...prev, name: e.target.value })) } disabled={savingId === row.id} placeholder="Nombre completo" style={{ width: "100%", minWidth: "120px", backgroundColor: "rgba(0, 0, 0, 0.5)", border: "1px solid var(--color-gold)", borderRadius: "4px", color: "#ffffff", padding: "0.4rem 0.6rem", fontSize: "0.85rem", boxSizing: "border-box", }} /> ) : ( row.name )} {editingId === row.id ? ( setEditForm((prev) => ({ ...prev, phone: e.target.value })) } disabled={savingId === row.id} placeholder="300 123 4567" style={{ width: "100%", minWidth: "110px", backgroundColor: "rgba(0, 0, 0, 0.5)", border: "1px solid var(--color-gold)", borderRadius: "4px", color: "var(--color-gold-light)", padding: "0.4rem 0.6rem", fontSize: "0.85rem", boxSizing: "border-box", }} /> ) : ( formatPhone(row.phone) )} {editingId === row.id ? ( setEditForm((prev) => ({ ...prev, email: e.target.value })) } disabled={savingId === row.id} placeholder="Opcional" style={{ width: "100%", minWidth: "120px", backgroundColor: "rgba(0, 0, 0, 0.5)", border: "1px solid var(--color-gold)", borderRadius: "4px", color: "rgba(255, 255, 255, 0.9)", padding: "0.4rem 0.6rem", fontSize: "0.85rem", boxSizing: "border-box", }} /> ) : ( row.email || "—" )} {formatDate(row.created_at)} {editingId === row.id ? (
) : (
)}
); }