feat(meal-plan): weekly planner, pantry, shopping lists, nutrition tracking

AI-generated weekly meal plans with pantry-awareness. Manual entry per slot.
Pantry inventory management. Auto-generated shopping lists from meal plan.
Weekly nutrition bar chart vs daily goals. Nutrition goals settings.
This commit is contained in:
Arnaud
2026-07-01 08:10:39 +02:00
parent 9d02a69250
commit 3636ab27ae
23 changed files with 1791 additions and 0 deletions
@@ -0,0 +1,118 @@
"use client";
import { useState } from "react";
import { toast } from "sonner";
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 [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("Failed to save nutrition goals");
return;
}
toast.success("Nutrition goals saved");
} catch {
toast.error("Failed to save nutrition goals");
} 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">Calories (kcal/day)</Label>
<Input
id="caloriesKcal"
type="number"
min={0}
value={caloriesKcal}
onChange={(e) => setCaloriesKcal(e.target.value)}
placeholder="e.g. 2000"
/>
</div>
<div className="space-y-2">
<Label htmlFor="proteinG">Protein (g/day)</Label>
<Input
id="proteinG"
type="number"
min={0}
value={proteinG}
onChange={(e) => setProteinG(e.target.value)}
placeholder="e.g. 50"
/>
</div>
<div className="space-y-2">
<Label htmlFor="carbsG">Carbs (g/day)</Label>
<Input
id="carbsG"
type="number"
min={0}
value={carbsG}
onChange={(e) => setCarbsG(e.target.value)}
placeholder="e.g. 250"
/>
</div>
<div className="space-y-2">
<Label htmlFor="fatG">Fat (g/day)</Label>
<Input
id="fatG"
type="number"
min={0}
value={fatG}
onChange={(e) => setFatG(e.target.value)}
placeholder="e.g. 70"
/>
</div>
</div>
<Button type="submit" disabled={saving}>
{saving ? "Saving..." : "Save goals"}
</Button>
</form>
);
}
@@ -0,0 +1,118 @@
"use client";
import { useEffect, useState } from "react";
type NutritionTotals = {
calories: number;
protein: number;
carbs: number;
fat: number;
};
type NutritionGoals = {
caloriesKcal: number | null;
proteinG: number | null;
carbsG: number | null;
fatG: number | null;
} | null;
type NutritionCoverage = {
calories: number;
protein: number;
carbs: number;
fat: number;
};
type NutritionResponse = {
totals: NutritionTotals;
goals: NutritionGoals;
coverage: NutritionCoverage;
};
interface WeeklyNutritionBarProps {
weekStart: string;
}
type BarItem = {
label: string;
value: number;
goal: number | null;
unit: string;
percentage: number;
colorClass: string;
};
export function WeeklyNutritionBar({ weekStart }: WeeklyNutritionBarProps) {
const [data, setData] = useState<NutritionResponse | null>(null);
useEffect(() => {
fetch(`/api/v1/meal-plans/${weekStart}/nutrition`)
.then((res) => (res.ok ? res.json() : null))
.then((json) => setData(json))
.catch(() => setData(null));
}, [weekStart]);
if (!data || !data.goals) return null;
const { totals, goals, coverage } = data;
const bars: BarItem[] = [
{
label: "Calories",
value: totals.calories,
goal: goals.caloriesKcal,
unit: "kcal",
percentage: coverage.calories,
colorClass: "bg-orange-500",
},
{
label: "Protein",
value: totals.protein,
goal: goals.proteinG,
unit: "g",
percentage: coverage.protein,
colorClass: "bg-blue-500",
},
{
label: "Carbs",
value: totals.carbs,
goal: goals.carbsG,
unit: "g",
percentage: coverage.carbs,
colorClass: "bg-green-500",
},
{
label: "Fat",
value: totals.fat,
goal: goals.fatG,
unit: "g",
percentage: coverage.fat,
colorClass: "bg-yellow-500",
},
];
return (
<div className="space-y-3">
{bars.map((bar) => {
if (bar.goal == null) return null;
const width = Math.min(bar.percentage, 100);
return (
<div key={bar.label} className="space-y-1">
<div className="flex justify-between text-sm">
<span className="font-medium">{bar.label}</span>
<span className="text-muted-foreground">
{bar.value} / {bar.goal} {bar.unit}
</span>
</div>
<div className="h-2 w-full rounded bg-muted overflow-hidden">
<div
className={`h-full rounded ${bar.colorClass}`}
style={{ width: `${width}%` }}
/>
</div>
</div>
);
})}
</div>
);
}