"use client" import { useState, useEffect } from "react" import { authClient } from "@/lib/auth-client" import { useRouter } from "next/navigation" import { GatoMascota } from "@/components/child/gato-mascota" interface ChildSummary { id: string name: string | null } export default function PairingPage() { const [code, setCode] = useState("") const [loading, setLoading] = useState(false) const [error, setError] = useState("") const [paired, setPaired] = useState(false) const [children, setChildren] = useState([]) const [selectedChildId, setSelectedChildId] = useState("") const { data: session } = authClient.useSession() const router = useRouter() useEffect(() => { fetch("/api/children") .then((r) => r.json()) .then((list) => { if (Array.isArray(list)) { setChildren(list) if (list.length > 0) setSelectedChildId(list[0].id) } }) .catch(() => {}) }, []) const handlePairing = async (e: React.FormEvent) => { e.preventDefault() setLoading(true) setError("") if (!selectedChildId) { setError("Seleccioná un niño") setLoading(false) return } try { const res = await fetch("/api/pairing", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ pairingCode: code.toUpperCase(), childId: selectedChildId, }), }) const data = await res.json() if (data.error) { setError(data.error) setLoading(false) return } setPaired(true) } catch { setError("Error de conexión") } setLoading(false) } if (!session) { return (

Iniciá sesión primero para emparejar

) } return (

Emparejar iPad

{paired ? (
) : (

Ingresá el código de 8 caracteres que aparece en el iPad

{children.length > 0 && (
{children.map((c) => ( ))}
)} setCode(e.target.value.toUpperCase())} placeholder="Código de 8 letras" maxLength={8} className="w-full rounded-xl border border-border bg-white px-4 py-3 text-center text-2xl tracking-widest uppercase focus:outline-none focus:ring-2 focus:ring-primary" autoFocus /> {error &&

{error}

}
)}
) }