From abbaaf88747a7d36b6a467ab8f9ee2ec4a580ba6 Mon Sep 17 00:00:00 2001 From: Mannu Date: Sun, 17 May 2026 13:58:04 +0530 Subject: [PATCH] fix(growth): fix add button and form submission - Fix + Add button to properly open form (was closing immediately) - Add error handling and saving state to form - Add visual feedback (Saving... text, error messages) - Clear form state properly when opening Co-Authored-By: Claude Sonnet 4.6 --- src/app/growth/page.tsx | 100 +++++++++++++++++++++++++++------------- 1 file changed, 67 insertions(+), 33 deletions(-) diff --git a/src/app/growth/page.tsx b/src/app/growth/page.tsx index 684aefc..628665f 100644 --- a/src/app/growth/page.tsx +++ b/src/app/growth/page.tsx @@ -64,6 +64,8 @@ export default function GrowthPage() { // Chart state const [chartMetric, setChartMetric] = useState<"weight" | "height" | "head">("weight"); + const [saving, setSaving] = useState(false); + const [saveError, setSaveError] = useState(null); // Goals state const [goal, setGoal] = useState({}); @@ -101,37 +103,66 @@ export default function GrowthPage() { }; const handleAdd = async () => { - if (!childId || (!weight && !height && !headCircumference)) return; - await fetch("/api/growth", { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - childId, - measuredAt: new Date(measuredAt).toISOString(), - weightKg: weight ? parseFloat(weight) : null, - heightCm: height ? parseFloat(height) : null, - headCircumferenceCm: headCircumference ? parseFloat(headCircumference) : null, - }), - }); - resetForm(); - fetchGrowthData(); + if (!childId || (!weight && !height && !headCircumference)) { + setSaveError("Please enter at least one measurement"); + return; + } + setSaving(true); + setSaveError(null); + try { + const res = await fetch("/api/growth", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + childId, + measuredAt: new Date(measuredAt).toISOString(), + weightKg: weight ? parseFloat(weight) : null, + heightCm: height ? parseFloat(height) : null, + headCircumferenceCm: headCircumference ? parseFloat(headCircumference) : null, + }), + }); + if (!res.ok) { + const err = await res.json(); + setSaveError(err.error || "Failed to save"); + return; + } + resetForm(); + fetchGrowthData(); + } catch (e: any) { + setSaveError(e.message || "Failed to save"); + } finally { + setSaving(false); + } }; const handleEdit = async (id: string) => { - await fetch("/api/growth", { - method: "PUT", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ - id, - measuredAt: new Date(measuredAt).toISOString(), - weightKg: weight ? parseFloat(weight) : null, - heightCm: height ? parseFloat(height) : null, - headCircumferenceCm: headCircumference ? parseFloat(headCircumference) : null, - }), - }); - setEditingId(null); - resetForm(); - fetchGrowthData(); + setSaving(true); + setSaveError(null); + try { + const res = await fetch("/api/growth", { + method: "PUT", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + id, + measuredAt: new Date(measuredAt).toISOString(), + weightKg: weight ? parseFloat(weight) : null, + heightCm: height ? parseFloat(height) : null, + headCircumferenceCm: headCircumference ? parseFloat(headCircumference) : null, + }), + }); + if (!res.ok) { + const err = await res.json(); + setSaveError(err.error || "Failed to update"); + return; + } + setEditingId(null); + resetForm(); + fetchGrowthData(); + } catch (e: any) { + setSaveError(e.message || "Failed to update"); + } finally { + setSaving(false); + } }; const handleDelete = async (id: string) => { @@ -330,7 +361,7 @@ export default function GrowthPage() { - @@ -547,6 +578,9 @@ export default function GrowthPage() { {showAdd && (

{editingId ? "Edit Record" : "Add New Measurement"}

+ {saveError && ( +
{saveError}
+ )} {editingId ? ( <> - ) : ( - )}