diff --git a/src/app/(app)/settings/page.tsx b/src/app/(app)/settings/page.tsx index 360802a..7c1ca56 100644 --- a/src/app/(app)/settings/page.tsx +++ b/src/app/(app)/settings/page.tsx @@ -41,6 +41,9 @@ export default function SettingsPage() { const [pedPhone, setPedPhone] = useState(""); const [pedName, setPedName] = useState(""); const [pedSaving, setPedSaving] = useState(false); + const [pedEditing, setPedEditing] = useState(false); + const [pedSaved, setPedSaved] = useState(false); + const [pedError, setPedError] = useState(""); // Check if can invite more members (client-side pre-check; server enforces) const canInvite = tier === "pro" || memberCount < 2; @@ -60,8 +63,10 @@ export default function SettingsPage() { fetchMembers(); fetchInvites(); fetch("/api/family").then(r => r.json()).then(d => { - if (d.family?.pediatrician_phone) setPedPhone(d.family.pediatrician_phone); - if (d.family?.pediatrician_name) setPedName(d.family.pediatrician_name); + setPedPhone(d.family?.pediatrician_phone || ""); + setPedName(d.family?.pediatrician_name || ""); + // If no data yet, open in edit mode so user can fill it in + if (!d.family?.pediatrician_phone && !d.family?.pediatrician_name) setPedEditing(true); }).catch(() => {}); } }, [familyId]); @@ -96,11 +101,24 @@ export default function SettingsPage() { const savePedInfo = async () => { setPedSaving(true); - await fetch("/api/family", { - method: "PATCH", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ pediatricianPhone: pedPhone, pediatricianName: pedName }), - }).catch(() => {}); + setPedError(""); + try { + const res = await fetch("/api/family", { + method: "PATCH", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ pediatricianPhone: pedPhone, pediatricianName: pedName }), + }); + const data = await res.json(); + if (!res.ok) { + setPedError(data.error || "Failed to save. Please try again."); + } else { + setPedEditing(false); + setPedSaved(true); + setTimeout(() => setPedSaved(false), 3000); + } + } catch { + setPedError("Network error. Please try again."); + } setPedSaving(false); }; @@ -360,21 +378,54 @@ export default function SettingsPage() { {/* Pediatrician */}
-
- 🏥 -
Pediatrician
-
-

Shown on the emergency guide and in AI medical redirects.

- setPedName(e.target.value)} - placeholder="Dr. Priya Sharma" - /> -
- setPedPhone(e.target.value)} placeholder="+91 98765 43210" className="flex-1" /> - +
+
+ 🏥 +
Pediatrician
+
+ {!pedEditing && (pedName || pedPhone) && ( + + )}
+ + {/* Display mode */} + {!pedEditing && (pedName || pedPhone) ? ( +
+ {pedName &&
{pedName}
} + {pedPhone &&
{pedPhone}
} + {pedSaved &&
Saved!
} +
+ ) : ( + /* Edit / first-fill mode */ +
+

Shown on the emergency guide and in AI medical redirects.

+ setPedName(e.target.value)} + placeholder="Dr. Priya Sharma" + /> +
+ setPedPhone(e.target.value)} placeholder="+91 98765 43210" className="flex-1" /> + +
+ {pedEditing && (pedName || pedPhone) && ( + + )} + {pedError &&

{pedError}

} +
+ )} + View Emergency Guide → diff --git a/src/app/api/family/route.ts b/src/app/api/family/route.ts index 471c7a2..cea23c5 100644 --- a/src/app/api/family/route.ts +++ b/src/app/api/family/route.ts @@ -33,9 +33,25 @@ export async function PATCH(request: Request) { const body = await request.json(); const { name, pediatricianPhone, pediatricianName, tier } = body; + // Only update fields that are explicitly present in the request body. + // This avoids COALESCE masking clears and undefined params causing errors. + const setClauses: string[] = []; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const params: any[] = []; + + if (name !== undefined) { setClauses.push(`name = $${params.push(name)}`); } + if (pediatricianPhone !== undefined) { setClauses.push(`pediatrician_phone = $${params.push(pediatricianPhone || null)}`); } + if (pediatricianName !== undefined) { setClauses.push(`pediatrician_name = $${params.push(pediatricianName || null)}`); } + if (tier !== undefined) { setClauses.push(`tier = $${params.push(tier)}`); } + + if (setClauses.length === 0) return NextResponse.json({ success: true }); + + setClauses.push("updated_at = NOW()"); + params.push(auth.session!.familyId); + await sql.unsafe( - `UPDATE families SET name = COALESCE($1, name), pediatrician_phone = COALESCE($2, pediatrician_phone), pediatrician_name = COALESCE($3, pediatrician_name), tier = COALESCE($4, tier), updated_at = NOW() WHERE id = $5`, - [name, pediatricianPhone, pediatricianName, tier, auth.session!.familyId] + `UPDATE families SET ${setClauses.join(", ")} WHERE id = $${params.length}`, + params ); return NextResponse.json({ success: true });