'use client' import React, { useRef, useState, useTransition } from 'react' import { useRouter } from 'next/navigation' import { createPastor, updatePastor, uploadLetter } from './actions' export type PastorRow = { id: number name: string email: string | null telephone: string[] city: string | null state: string | null country: string | null countryName: string | null countryFlag: string inTelegram: boolean notes: string | null letterUrl: string | null letterName: string | null } export function PastorsTable({ rows }: { rows: PastorRow[] }) { const [editing, setEditing] = useState(null) const [uploading, setUploading] = useState(null) const [creating, setCreating] = useState(false) return ( <>
{rows.length === 0 ? (

No se encontraron pastores

Intenta con otro término de búsqueda.

) : (
{rows.map((p) => ( ))}
Nombre Correo Teléfonos Ciudad País Telegram Carta Acciones
{p.name} {p.email ? ( {p.email} ) : ( )} {p.telephone.length ? (
{p.telephone.map((t, i) => ( {t} ))}
) : ( )}
{p.city || } {p.countryName ? ( {p.countryFlag} {p.countryName} ) : ( )} {p.inTelegram ? ( ) : ( No )} {p.letterUrl ? ( Descargar ) : ( )}
)} {creating && setCreating(false)} />} {editing && ( setEditing(null)} /> )} {uploading && ( setUploading(null)} /> )} ) } function PastorModal({ pastor, onClose }: { pastor: PastorRow | null; onClose: () => void }) { const router = useRouter() const isCreate = pastor === null const [pending, startTransition] = useTransition() const [error, setError] = useState(null) const [name, setName] = useState(pastor?.name ?? '') const [email, setEmail] = useState(pastor?.email ?? '') const [phones, setPhones] = useState( pastor?.telephone.length ? pastor.telephone : [''], ) const [city, setCity] = useState(pastor?.city ?? '') const [state, setState] = useState(pastor?.state ?? '') const [country, setCountry] = useState(pastor?.country ?? '') const [inTelegram, setInTelegram] = useState(pastor?.inTelegram ?? false) const [notes, setNotes] = useState(pastor?.notes ?? '') const setPhone = (i: number, v: string) => setPhones((prev) => prev.map((p, idx) => (idx === i ? v : p))) const addPhone = () => setPhones((prev) => [...prev, '']) const removePhone = (i: number) => setPhones((prev) => prev.filter((_, idx) => idx !== i)) const onSubmit = (e: React.FormEvent) => { e.preventDefault() setError(null) startTransition(async () => { const input = { name, email, telephone: phones, city, state, country, inTelegram, notes } const res = isCreate ? await createPastor(input) : await updatePastor(pastor.id, input) if (res?.ok) { router.refresh() onClose() } else { setError( ('error' in res && res.error) || (isCreate ? 'No se pudo crear el pastor.' : 'No se pudieron guardar los cambios.'), ) } }) } return (

{isCreate ? 'Crear pastor' : 'Editar pastor'}

setName(e.target.value)} required /> setEmail(e.target.value)} /> Teléfonos
{phones.map((p, i) => (
setPhone(i, e.target.value)} />
))}
setCity(e.target.value)} /> setState(e.target.value)} />
setCountry(e.target.value)} /> Código ISO de 2 letras
En Telegram setInTelegram(e.target.checked)} />