feat: one-click shopping list from meal plan, fix ingredient merge bug

- Meal-plan page now has a direct "Add to shopping list" action for the
  currently-viewed week, instead of requiring a trip to Shopping Lists and
  manually typing the week's Monday date.
- Fixed a real under-shopping bug in shopping-lists/route.ts: when
  generating from a meal-plan week, ingredients appearing in more than one
  recipe were merged by keeping only the FIRST occurrence's quantity and
  silently dropping the rest. New mergeIngredients() (pantry-shopping-match.ts)
  groups by ingredientId (or normalized name+unit) and sums quantities
  across every recipe in the week; incompatible/unparseable units are kept
  as separate line items rather than guessed at.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
Arnaud
2026-07-10 09:45:51 +02:00
parent 9c545a5bb3
commit d62e2a6383
6 changed files with 104 additions and 28 deletions
@@ -10,15 +10,27 @@ import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/u
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
export function NewShoppingListButton() {
export function NewShoppingListButton({
defaultWeekStart,
defaultName,
}: {
/** Pre-fills and locks the source week, e.g. when generating from the currently-viewed meal-plan week. */
defaultWeekStart?: string;
defaultName?: string;
}) {
const t = useTranslations("mealPlan");
const tCommon = useTranslations("common");
const router = useRouter();
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [weekStart, setWeekStart] = useState("");
const [name, setName] = useState(defaultName ?? "");
const [weekStart, setWeekStart] = useState(defaultWeekStart ?? "");
const [saving, setSaving] = useState(false);
function openDialog() {
if (defaultWeekStart) { setName(defaultName ?? ""); setWeekStart(defaultWeekStart); }
setOpen(true);
}
async function create() {
if (!name.trim()) return;
setSaving(true);
@@ -44,8 +56,9 @@ export function NewShoppingListButton() {
return (
<>
<Button size="sm" onClick={() => setOpen(true)}>
<Plus className="h-4 w-4" /> {t("newList")}
<Button size="sm" onClick={openDialog} variant={defaultWeekStart ? "outline" : "default"}>
{defaultWeekStart ? <ShoppingCart className="h-4 w-4" /> : <Plus className="h-4 w-4" />}
{defaultWeekStart ? t("generateFromThisWeek") : t("newList")}
</Button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="max-w-md">
@@ -55,11 +68,16 @@ export function NewShoppingListButton() {
<Label>{t("listNameLabel")}</Label>
<Input value={name} onChange={(e) => setName(e.target.value)} placeholder={t("listNamePlaceholder")} />
</div>
<div className="space-y-2">
<Label>{t("generateFromWeek")}</Label>
<Input type="date" value={weekStart} onChange={(e) => setWeekStart(e.target.value)} />
<p className="text-xs text-muted-foreground">{t("generateFromWeekHint")}</p>
</div>
{!defaultWeekStart && (
<div className="space-y-2">
<Label>{t("generateFromWeek")}</Label>
<Input type="date" value={weekStart} onChange={(e) => setWeekStart(e.target.value)} />
<p className="text-xs text-muted-foreground">{t("generateFromWeekHint")}</p>
</div>
)}
{defaultWeekStart && (
<p className="text-xs text-muted-foreground">{t("generateFromThisWeekHint")}</p>
)}
<div className="flex gap-2 justify-end">
<Button variant="outline" onClick={() => setOpen(false)}>{tCommon("cancel")}</Button>
<Button onClick={create} disabled={!name.trim() || saving}>{saving ? t("listCreating") : t("listCreate")}</Button>