"use client"; import { useState, useEffect } from "react"; import { useRouter } from "next/navigation"; interface Child { id: string; name: string; birthDate: string; sex: string; stage?: string; } export default function FamilyPage() { const router = useRouter(); const [children, setChildren] = useState([]); const [loading, setLoading] = useState(true); const [editing, setEditing] = useState(null); const [editName, setEditName] = useState(""); const [editDob, setEditDob] = useState(""); const [showAdd, setShowAdd] = useState(false); const [newName, setNewName] = useState(""); const [newDob, setNewDob] = useState(""); const [newSex, setNewSex] = useState("male"); useEffect(() => { fetchChildren(); }, []); const fetchChildren = async () => { try { const res = await fetch("/api/children?familyId=default"); const data = await res.json(); setChildren(data.children || []); } catch (err) { console.error("Failed to fetch:", err); } setLoading(false); }; const startEdit = (child: Child) => { setEditing(child.id); setEditName(child.name); setEditDob(child.birthDate); }; const saveEdit = async (childId: string) => { try { const res = await fetch("/api/children", { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: childId, name: editName, birthDate: editDob }), }); const data = await res.json(); if (data.success) { setChildren(children.map((c) => (c.id === childId ? { ...c, name: editName, birthDate: editDob } : c))); } } catch (err) { console.error("Failed to save:", err); } setEditing(null); }; const addChild = async () => { if (!newName || !newDob) return; try { const res = await fetch("/api/children", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: newName, birthDate: newDob, sex: newSex, familyId: "default" }), }); const data = await res.json(); if (data.success) { setChildren([data.child, ...children]); setShowAdd(false); setNewName(""); setNewDob(""); } } catch (err) { console.error("Failed to add:", err); } }; return (

Family

{/* Add Child Form */} {showAdd && (
setNewName(e.target.value)} placeholder="Baby's name" className="w-full p-2 border rounded-lg" /> setNewDob(e.target.value)} className="w-full p-2 border rounded-lg" />
)} {loading ? (
Loading...
) : children.length === 0 && !showAdd ? (
👶

No baby added yet

) : ( children.map((child) => (
{editing === child.id ? (
setEditName(e.target.value)} className="w-full p-2 border rounded-lg" /> setEditDob(e.target.value)} className="w-full p-2 border rounded-lg" />
) : (
{child.sex === "male" ? "👦" : "👧"}
{child.name}
Born: {child.birthDate ? new Date(child.birthDate).toLocaleDateString() : "Not set"}
)}
)) )} {/* Add Button */} {!showAdd && children.length > 0 && ( )}
); }