import { DateTime, formatDateOnly, getWeekStart, toDateOnly } from "@batch-cooking/date-tools"; import { MEALS, type Meal, type PlanningItemView, type PlanningView, WEEK_DAYS, } from "@batch-cooking/shared"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { apiClient } from "../../api/client"; import { type PlanningSlot, RecipePickerDialog } from "../../features/planning/RecipePickerDialog"; import { WeekNavigator } from "../../features/planning/WeekNavigator"; import "./planning-page.scss"; /** Load state for the `GET /planning` call — a discriminated union so a stale/impossible combination (e.g. "loading" with data) can't be represented. */ type PlanningState = | { status: "loading" } | { status: "loaded"; planning: PlanningView | null } | { status: "error" }; /** Meals that close out a "moment of the day" group (Matin/Midi/Après-midi/Soir) — see `.band-end` in planning-page.scss for the resulting border treatment. */ const BAND_END_MEALS: ReadonlySet = new Set(["collation", "dejeuner", "gouter"]); /** * Landing page for an authenticated visitor — the household's planning for * a selectable week, laid out as a grid (days × meals). Behind * {@link RequireAuth} (via `AppLayout`), so this only renders once a * session is confirmed. * * `null` from the API is a normal, common state (no planning for that week * yet) — unlike the previous single-day table view this replaces, it isn't * rendered as a separate "empty" message: the grid itself, with every cell * showing just its "+" button, already communicates that. The "+" itself * isn't wired to anything yet (no recipe catalog to search — see the * planning page's plan/PR description) — a future task. */ export function PlanningPage() { const { t } = useTranslation(); const [weekStart, setWeekStart] = useState(() => getWeekStart(DateTime.utc())); const [state, setState] = useState({ status: "loading" }); // The slot a `RecipePickerDialog` is currently open for — `null` means // closed. Mounting the dialog only while this is set (rather than an // always-mounted `isOpen` toggle) resets its internal filter/search // state for free on every open, same convention as `WeekNavigator`'s own // `CalendarPopover` (features/planning/WeekNavigator.tsx). const [openSlot, setOpenSlot] = useState(null); useEffect(() => { let cancelled = false; setState({ status: "loading" }); apiClient .getPlanningForWeek(formatDateOnly(weekStart)) .then((planning) => { if (!cancelled) setState({ status: "loaded", planning }); }) .catch(() => { if (!cancelled) setState({ status: "error" }); }); return () => { cancelled = true; }; }, [weekStart]); /** * Applies `updater` to the currently loaded planning's items, patching * local state without a full `GET /planning` refetch — same philosophy * as `RecipesPage`'s `handleFavoriteToggled`/`handleDeleted`. Builds a * placeholder parent `Planning` if none existed yet (the week's very * first add, see `addPlanningItem`'s `findOrCreatePlanningForWeek`) — * its `id`/`startDate`/`finishDate` are never read anywhere on this page * (only `planning.items` is), so a placeholder id is harmless; the next * week-navigation away and back re-fetches the real row anyway. */ function patchPlanningItems(updater: (items: PlanningItemView[]) => PlanningItemView[]) { setState((prev) => { if (prev.status !== "loaded") return prev; const planning = prev.planning ?? { id: -1, startDate: weekStart.toISO() ?? "", finishDate: weekStart.plus({ days: 6 }).toISO() ?? "", items: [], }; return { status: "loaded", planning: { ...planning, items: updater(planning.items) } }; }); } function handleAdded(item: PlanningItemView) { patchPlanningItems((items) => [...items, item]); } /** Optimistic removal (same rollback-on-failure idea as `FavoriteStarButton`) — the API call already happened by the time this needs to roll back, `removePlanningItem` having already rejected. */ async function handleRemove(item: PlanningItemView) { patchPlanningItems((items) => items.filter((i) => i.id !== item.id)); try { await apiClient.removePlanningItem(item.id); } catch { patchPlanningItems((items) => [...items, item]); } } return (

{t("planning.title")}

{state.status === "loading" && (

{t("planning.loading")}

)} {state.status === "error" && (

{t("common.loadError")}

)} {state.status === "loaded" && ( )} {openSlot && ( setOpenSlot(null)} onAdded={handleAdded} /> )}
); } /** The week grid itself — 7 day columns × 5 meal rows. */ function PlanningGrid({ weekStart, planning, onAddSlot, onRemoveItem, }: { weekStart: DateTime; planning: PlanningView | null; onAddSlot: (slot: PlanningSlot) => void; onRemoveItem: (item: PlanningItemView) => void; }) { const { t } = useTranslation(); const today = toDateOnly(DateTime.utc()); const days = WEEK_DAYS.map((weekDay, i) => ({ weekDay, date: weekStart.plus({ days: i }) })); const items = planning?.items ?? []; return (
))} {MEALS.map((meal) => ( {days.map(({ weekDay, date }) => ( item.weekDay === weekDay && item.meal === meal)} onAdd={() => onAddSlot({ date: formatDateOnly(date), weekDay, meal })} onRemove={onRemoveItem} /> ))} ))}
{days.map(({ weekDay, date }) => ( {t(`common.days.${weekDay}`)} {date.day}
{t(`planning.meals.${meal}`)}
); } /** One (day, meal) cell: the recipes already planned for it (as pills, each showing its portion count) plus the "+" to add another. */ function MealCell({ isToday, items, onAdd, onRemove, }: { isToday: boolean; items: PlanningItemView[]; onAdd: () => void; onRemove: (item: PlanningItemView) => void; }) { const { t } = useTranslation(); return (
{items.length > 0 && (
{items.map((item) => ( {item.recipe.name} · ×{item.portions} ))}
)}
); }