diff --git a/src/app/api/logs/route.ts b/src/app/api/logs/route.ts index 33a9cc6..a5bae77 100644 --- a/src/app/api/logs/route.ts +++ b/src/app/api/logs/route.ts @@ -76,8 +76,8 @@ export async function GET(request: Request) { const type = searchParams.get("type"); const limit = parseInt(searchParams.get("limit") || "20"); - if (!childId || !type) { - return NextResponse.json({ error: "childId and type required" }, { status: 400 }); + if (!childId) { + return NextResponse.json({ error: "childId required" }, { status: 400 }); } // Verify child belongs to user's family @@ -87,6 +87,26 @@ export async function GET(request: Request) { } try { + // No type → return all log types merged, camelCase (used by activity page) + if (!type) { + const [feeds, diapers, sleeps] = await Promise.all([ + sql.unsafe(`SELECT id, type as sub_type, amount_ml, notes, logged_at FROM feeds WHERE child_id = $1 ORDER BY logged_at DESC LIMIT $2`, [childId, limit]), + sql.unsafe(`SELECT id, type as sub_type, notes, logged_at FROM diapers_logs WHERE child_id = $1 ORDER BY logged_at DESC LIMIT $2`, [childId, limit]), + sql.unsafe(`SELECT id, type as sub_type, notes, logged_at FROM sleeps WHERE child_id = $1 ORDER BY logged_at DESC LIMIT $2`, [childId, limit]), + ]); + + const all = [ + ...(feeds as any[]).map(r => ({ id: r.id, type: "feed", subType: r.sub_type, amount: r.amount_ml ?? null, notes: r.notes, loggedAt: r.logged_at })), + ...(diapers as any[]).map(r => ({ id: r.id, type: "diaper", subType: r.sub_type, amount: null, notes: r.notes, loggedAt: r.logged_at })), + ...(sleeps as any[]).map(r => ({ id: r.id, type: "sleep", subType: r.sub_type, amount: null, notes: r.notes, loggedAt: r.logged_at })), + ] + .sort((a, b) => new Date(b.loggedAt).getTime() - new Date(a.loggedAt).getTime()) + .slice(0, limit); + + return NextResponse.json({ entries: all }); + } + + // Type-specific queries — return raw snake_case rows (used by homepage) let results: any[] = []; if (type === "feed") { results = await sql.unsafe( diff --git a/src/app/page.tsx b/src/app/page.tsx index 9fee7b1..271853d 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -82,6 +82,14 @@ function LogModal({ type, childId, onClose }: { type: "feed" | "diaper" | "sleep const [subType, setSubType] = useState("breast_milk"); const [amountMl, setAmountMl] = useState(""); const [notes, setNotes] = useState(""); + + // Reset subType default whenever the log type changes + useEffect(() => { + if (type === "feed") setSubType("breast_milk"); + else if (type === "diaper") setSubType("wet"); + else if (type === "sleep") setSubType("nap"); + }, [type]); + if (!type) return null; const handleSubmit = async () => {