fix(activity): repair full log flow — quick log → activity page

Three bugs fixed:

1. GET /api/logs required a `type` param; activity page calls without one,
   always receiving {error} → empty list. Fixed: when type is omitted,
   merge feeds + diapers_logs + sleeps, sort by time, return camelCase
   fields (loggedAt, subType, amount) matching the activity page interface.

2. Type-specific queries (used by homepage) still return raw snake_case
   rows unchanged so the homepage "Recent Activity" section is unaffected.

3. LogModal initialised subType as "breast_milk" regardless of log type.
   Adding a useEffect to reset it (breast_milk / wet / nap) when type
   changes prevents bad enum values from being sent for diaper/sleep logs.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Manohar Gupta 2026-05-18 10:49:00 +05:30
parent a75a543373
commit 515e93aae1
2 changed files with 30 additions and 2 deletions

View file

@ -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(

View file

@ -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 () => {