mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(onboarding): surface errors and supply default schedule (#87)
Issue #87 (kevinpapst): medications added during onboarding were never visible afterwards. Root cause: the createMedication schema requires schedules.min(1) ("Mindestens ein Zeitfenster"), but the onboarding wizard sent schedules:[]. The 422 was silently swallowed and the user was redirected to the dashboard as if everything had worked. This patch is the minimal-impact fix. The proper fix lives in Phase 3.5 of the audit plan, which replaces the wizard entirely with a dashboard-first empty-state flow. Until then: - finishOnboarding() wraps each call in a try/catch, checks response.ok explicitly, and surfaces a sonner toast on failure. setSaving(false) on error so the user can retry instead of being stuck on a spinner. - Each medication is sent with a default schedule (08:00-09:00) so it is actually persisted. A new medScheduleHint i18n key is shown under the meds list while the user is in the wizard, so the default schedule is not surprising. - Errors are translated per stage (errorProfile, errorMedication with {name}, errorTargets) plus an errorGeneric fallback. en.json and de.json kept symmetric. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
be7df4bc17
commit
5e721e0791
3 files changed
+99
-53
No files matched your search
+6
-1
@@ -819,7 +819,12 @@
|
|||||||
"targetsDescription": "Setze optionale Zielwerte für Blutdruck und Gewicht.",
|
"targetsDescription": "Setze optionale Zielwerte für Blutdruck und Gewicht.",
|
||||||
"targetSys": "Systolisch (mmHg)",
|
"targetSys": "Systolisch (mmHg)",
|
||||||
"targetDia": "Diastolisch (mmHg)",
|
"targetDia": "Diastolisch (mmHg)",
|
||||||
"targetWeight": "Zielgewicht (kg)"
|
"targetWeight": "Zielgewicht (kg)",
|
||||||
|
"errorProfile": "Dein Profil konnte nicht gespeichert werden. Bitte erneut versuchen.",
|
||||||
|
"errorMedication": "Das Medikament „{name}\" konnte nicht angelegt werden. Bitte erneut versuchen oder später unter „Medikamente\" hinzufügen.",
|
||||||
|
"errorTargets": "Deine Zielwerte konnten nicht gespeichert werden. Bitte erneut versuchen.",
|
||||||
|
"errorGeneric": "Beim Abschluss des Setups ist etwas schiefgelaufen. Bitte erneut versuchen.",
|
||||||
|
"medScheduleHint": "Wir legen ein Standard-Erinnerungsfenster an (08:00–09:00 täglich). Du kannst es jederzeit unter „Medikamente\" anpassen."
|
||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"title": "Einstellungen",
|
"title": "Einstellungen",
|
||||||
|
|||||||
+6
-1
@@ -819,7 +819,12 @@
|
|||||||
"targetsDescription": "Set optional target values for blood pressure and weight.",
|
"targetsDescription": "Set optional target values for blood pressure and weight.",
|
||||||
"targetSys": "Systolic (mmHg)",
|
"targetSys": "Systolic (mmHg)",
|
||||||
"targetDia": "Diastolic (mmHg)",
|
"targetDia": "Diastolic (mmHg)",
|
||||||
"targetWeight": "Target weight (kg)"
|
"targetWeight": "Target weight (kg)",
|
||||||
|
"errorProfile": "Could not save your profile. Please try again.",
|
||||||
|
"errorMedication": "Could not add the medication \"{name}\". Please try again or add it later in Medications.",
|
||||||
|
"errorTargets": "Could not save your target values. Please try again.",
|
||||||
|
"errorGeneric": "Something went wrong while finishing setup. Please try again.",
|
||||||
|
"medScheduleHint": "We'll add a default reminder window (08:00–09:00 daily). You can adjust it anytime in Medications."
|
||||||
},
|
},
|
||||||
"settings": {
|
"settings": {
|
||||||
"title": "Settings",
|
"title": "Settings",
|
||||||
|
|||||||
+87
-51
@@ -3,6 +3,7 @@
|
|||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useQueryClient } from "@tanstack/react-query";
|
import { useQueryClient } from "@tanstack/react-query";
|
||||||
|
import { toast } from "sonner";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { Label } from "@/components/ui/label";
|
import { Label } from "@/components/ui/label";
|
||||||
@@ -39,45 +40,63 @@ export default function OnboardingPage() {
|
|||||||
async function finishOnboarding() {
|
async function finishOnboarding() {
|
||||||
setSaving(true);
|
setSaving(true);
|
||||||
|
|
||||||
const body: Record<string, unknown> = {};
|
try {
|
||||||
if (heightCm) body.heightCm = parseFloat(heightCm);
|
const body: Record<string, unknown> = {};
|
||||||
if (dateOfBirth) body.dateOfBirth = dateOfBirth;
|
if (heightCm) body.heightCm = parseFloat(heightCm);
|
||||||
if (gender) body.gender = gender;
|
if (dateOfBirth) body.dateOfBirth = dateOfBirth;
|
||||||
|
if (gender) body.gender = gender;
|
||||||
|
|
||||||
await fetch("/api/onboarding/complete", {
|
const profileRes = await fetch("/api/onboarding/complete", {
|
||||||
method: "POST",
|
|
||||||
headers: { "Content-Type": "application/json" },
|
|
||||||
body: JSON.stringify(body),
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create medications if any
|
|
||||||
for (const med of meds) {
|
|
||||||
await fetch("/api/medications", {
|
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({
|
body: JSON.stringify(body),
|
||||||
name: med.name,
|
|
||||||
dose: med.dose,
|
|
||||||
schedules: [],
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
}
|
if (!profileRes.ok) {
|
||||||
|
throw new Error(t("onboarding.errorProfile"));
|
||||||
|
}
|
||||||
|
|
||||||
// Save targets if set
|
// Create medications. Server validation requires at least one schedule,
|
||||||
if (targetSys || targetDia || targetWeight) {
|
// so we attach a sensible default — the user is informed via medScheduleHint
|
||||||
const targets: Record<string, unknown> = {};
|
// and can refine it in Medications later. Phase 3.5 of the audit replaces
|
||||||
if (targetSys) targets.bpSysTarget = parseInt(targetSys);
|
// this entire wizard with an empty-state-driven flow.
|
||||||
if (targetDia) targets.bpDiaTarget = parseInt(targetDia);
|
for (const med of meds) {
|
||||||
if (targetWeight) targets.weightTarget = parseFloat(targetWeight);
|
const medRes = await fetch("/api/medications", {
|
||||||
await fetch("/api/insights/targets", {
|
method: "POST",
|
||||||
method: "PUT",
|
headers: { "Content-Type": "application/json" },
|
||||||
headers: { "Content-Type": "application/json" },
|
body: JSON.stringify({
|
||||||
body: JSON.stringify(targets),
|
name: med.name,
|
||||||
});
|
dose: med.dose,
|
||||||
}
|
schedules: [{ windowStart: "08:00", windowEnd: "09:00" }],
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
if (!medRes.ok) {
|
||||||
|
throw new Error(t("onboarding.errorMedication", { name: med.name }));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await queryClient.invalidateQueries({ queryKey: ["auth"] });
|
if (targetSys || targetDia || targetWeight) {
|
||||||
router.replace("/");
|
const targets: Record<string, unknown> = {};
|
||||||
|
if (targetSys) targets.bpSysTarget = parseInt(targetSys);
|
||||||
|
if (targetDia) targets.bpDiaTarget = parseInt(targetDia);
|
||||||
|
if (targetWeight) targets.weightTarget = parseFloat(targetWeight);
|
||||||
|
const targetsRes = await fetch("/api/insights/targets", {
|
||||||
|
method: "PUT",
|
||||||
|
headers: { "Content-Type": "application/json" },
|
||||||
|
body: JSON.stringify(targets),
|
||||||
|
});
|
||||||
|
if (!targetsRes.ok) {
|
||||||
|
throw new Error(t("onboarding.errorTargets"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
await queryClient.invalidateQueries({ queryKey: ["auth"] });
|
||||||
|
router.replace("/");
|
||||||
|
} catch (err) {
|
||||||
|
const message =
|
||||||
|
err instanceof Error ? err.message : t("onboarding.errorGeneric");
|
||||||
|
toast.error(message);
|
||||||
|
setSaving(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function addMedication() {
|
function addMedication() {
|
||||||
@@ -191,27 +210,40 @@ export default function OnboardingPage() {
|
|||||||
placeholder={t("onboarding.medDosePlaceholder")}
|
placeholder={t("onboarding.medDosePlaceholder")}
|
||||||
className="w-24"
|
className="w-24"
|
||||||
/>
|
/>
|
||||||
<Button type="button" variant="outline" size="sm" onClick={addMedication}>
|
<Button
|
||||||
|
type="button"
|
||||||
|
variant="outline"
|
||||||
|
size="sm"
|
||||||
|
onClick={addMedication}
|
||||||
|
>
|
||||||
+
|
+
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>
|
||||||
{meds.length > 0 && (
|
{meds.length > 0 && (
|
||||||
<ul className="space-y-1 text-sm">
|
<>
|
||||||
{meds.map((m, i) => (
|
<ul className="space-y-1 text-sm">
|
||||||
<li key={i} className="bg-muted/50 flex items-center justify-between rounded px-3 py-1.5">
|
{meds.map((m, i) => (
|
||||||
<span>
|
<li
|
||||||
{m.name} {m.dose && `(${m.dose})`}
|
key={i}
|
||||||
</span>
|
className="bg-muted/50 flex items-center justify-between rounded px-3 py-1.5"
|
||||||
<button
|
|
||||||
type="button"
|
|
||||||
className="text-muted-foreground hover:text-destructive text-xs"
|
|
||||||
onClick={() => setMeds(meds.filter((_, j) => j !== i))}
|
|
||||||
>
|
>
|
||||||
✕
|
<span>
|
||||||
</button>
|
{m.name} {m.dose && `(${m.dose})`}
|
||||||
</li>
|
</span>
|
||||||
))}
|
<button
|
||||||
</ul>
|
type="button"
|
||||||
|
className="text-muted-foreground hover:text-destructive text-xs"
|
||||||
|
onClick={() => setMeds(meds.filter((_, j) => j !== i))}
|
||||||
|
>
|
||||||
|
✕
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
<p className="text-muted-foreground text-xs">
|
||||||
|
{t("onboarding.medScheduleHint")}
|
||||||
|
</p>
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
@@ -226,8 +258,12 @@ export default function OnboardingPage() {
|
|||||||
{t("onboarding.notificationsDescription")}
|
{t("onboarding.notificationsDescription")}
|
||||||
</p>
|
</p>
|
||||||
<div className="bg-muted/50 space-y-2 rounded-lg p-4 text-sm">
|
<div className="bg-muted/50 space-y-2 rounded-lg p-4 text-sm">
|
||||||
<p>📱 <strong>Telegram</strong> — {t("onboarding.telegramHint")}</p>
|
<p>
|
||||||
<p>🔔 <strong>Web Push</strong> — {t("onboarding.webPushHint")}</p>
|
📱 <strong>Telegram</strong> — {t("onboarding.telegramHint")}
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
🔔 <strong>Web Push</strong> — {t("onboarding.webPushHint")}
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<p className="text-muted-foreground text-xs">
|
<p className="text-muted-foreground text-xs">
|
||||||
{t("onboarding.notificationsLater")}
|
{t("onboarding.notificationsLater")}
|
||||||
|
|||||||
Reference in new issue
Block a user