Files
Epicure/apps/web/components/nutrition/nutrition-goals-form.tsx
T
Arnaud 9c545a5bb3 feat: private accounts, explore/people merge, meal-plan fixes, i18n and theme cleanup
- Private accounts: users.isPrivate hides a user from search and their
  recipes from search/trending/for-you discovery surfaces (follow-aware
  where the route already has session context, blanket exclusion where it
  doesn't); existing followers and direct links are unaffected, no
  follow-request approval flow was built (explicit scope limit)
- Merged /people into the Explore tab (tab=people query param); the old
  standalone route now redirects there
- "Get Ideas" vs "Surprise Me" were doing the same empty-prompt call;
  Surprise Me now injects a real random constraint (5-ingredient, one-pot,
  etc.), matching the existing pattern in the AI recipe-generate dialog
- Meal-plan day cells now link to their recipe (was dead text) and gained
  a one-click "mark as cooked" action
- Theme toggle is now a real three-way light/dark/system control instead
  of a binary flip
- Nutrition goals form had zero i18n wiring; fully localized now

New migration 0027 (users.is_private) generated, left unapplied like the
others. Verified with typecheck, lint, and a clean --no-cache docker build.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-10 09:26:03 +02:00

121 lines
3.6 KiB
TypeScript

"use client";
import { useState } from "react";
import { toast } from "sonner";
import { useTranslations } from "next-intl";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
type NutritionGoals = {
caloriesKcal: number | null;
proteinG: number | null;
carbsG: number | null;
fatG: number | null;
} | null;
interface NutritionGoalsFormProps {
initialGoals: NutritionGoals;
}
export function NutritionGoalsForm({ initialGoals }: NutritionGoalsFormProps) {
const t = useTranslations("settings.nutritionGoals");
const [caloriesKcal, setCaloriesKcal] = useState<string>(
initialGoals?.caloriesKcal != null ? String(initialGoals.caloriesKcal) : ""
);
const [proteinG, setProteinG] = useState<string>(
initialGoals?.proteinG != null ? String(initialGoals.proteinG) : ""
);
const [carbsG, setCarbsG] = useState<string>(
initialGoals?.carbsG != null ? String(initialGoals.carbsG) : ""
);
const [fatG, setFatG] = useState<string>(
initialGoals?.fatG != null ? String(initialGoals.fatG) : ""
);
const [saving, setSaving] = useState(false);
async function handleSubmit(e: React.FormEvent) {
e.preventDefault();
setSaving(true);
const body: Record<string, number> = {};
if (caloriesKcal !== "") body.caloriesKcal = parseInt(caloriesKcal, 10);
if (proteinG !== "") body.proteinG = parseInt(proteinG, 10);
if (carbsG !== "") body.carbsG = parseInt(carbsG, 10);
if (fatG !== "") body.fatG = parseInt(fatG, 10);
try {
const res = await fetch("/api/v1/users/me/nutrition-goals", {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
if (!res.ok) {
toast.error(t("saveError"));
return;
}
toast.success(t("saveSuccess"));
} catch {
toast.error(t("saveError"));
} finally {
setSaving(false);
}
}
return (
<form onSubmit={handleSubmit} className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div className="space-y-2">
<Label htmlFor="caloriesKcal">{t("caloriesLabel")}</Label>
<Input
id="caloriesKcal"
type="number"
min={0}
value={caloriesKcal}
onChange={(e) => setCaloriesKcal(e.target.value)}
placeholder={t("caloriesPlaceholder")}
/>
</div>
<div className="space-y-2">
<Label htmlFor="proteinG">{t("proteinLabel")}</Label>
<Input
id="proteinG"
type="number"
min={0}
value={proteinG}
onChange={(e) => setProteinG(e.target.value)}
placeholder={t("proteinPlaceholder")}
/>
</div>
<div className="space-y-2">
<Label htmlFor="carbsG">{t("carbsLabel")}</Label>
<Input
id="carbsG"
type="number"
min={0}
value={carbsG}
onChange={(e) => setCarbsG(e.target.value)}
placeholder={t("carbsPlaceholder")}
/>
</div>
<div className="space-y-2">
<Label htmlFor="fatG">{t("fatLabel")}</Label>
<Input
id="fatG"
type="number"
min={0}
value={fatG}
onChange={(e) => setFatG(e.target.value)}
placeholder={t("fatPlaceholder")}
/>
</div>
</div>
<Button type="submit" disabled={saving}>
{saving ? t("saving") : t("saveButton")}
</Button>
</form>
);
}