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 ? ( <> - ) : ( - )}