feat: Tage als geschlossen markierbar (Feiertage o. Ä.)
Im Wochenplan lässt sich pro Tag ein optionaler, mehrsprachiger Schließungsgrund hinterlegen. Geschlossene Tage werden auf der öffentlichen Speisekarte (Desktop + mobil) und im RSS-Feed als "Geschlossen" statt mit Essenszeiten angezeigt, im Wochenplan bleibt "Mahlzeit hinzufügen" für diesen Tag deaktiviert.
This commit is contained in:
+220
-7
@@ -14,12 +14,19 @@ import {
|
||||
} from "@/lib/menu";
|
||||
import { formatMessage } from "@/lib/i18n/shared";
|
||||
import { localized } from "@/lib/types";
|
||||
import type { Dish, MenuEntry, Section } from "@/lib/types";
|
||||
import { useDict, useLocale } from "@/components/i18n/LocaleProvider";
|
||||
import type { ClosedDay, Dish, MenuEntry, Section } from "@/lib/types";
|
||||
import { useDict, useLanguages, useLocale } from "@/components/i18n/LocaleProvider";
|
||||
|
||||
export default function WeekEditor({ monday }: { monday: string }) {
|
||||
export default function WeekEditor({
|
||||
monday,
|
||||
translateEnabled,
|
||||
}: {
|
||||
monday: string;
|
||||
translateEnabled: boolean;
|
||||
}) {
|
||||
const dict = useDict();
|
||||
const locale = useLocale();
|
||||
const languages = useLanguages().filter((l) => l.code !== "de");
|
||||
const friday = addDays(monday, 4);
|
||||
const days = dict.weekdays.long.map((weekday, i) => ({
|
||||
weekday,
|
||||
@@ -32,6 +39,7 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
const [sections, setSections] = useState<Section[]>([]);
|
||||
const [dishes, setDishes] = useState<Dish[]>([]);
|
||||
const [entries, setEntries] = useState<MenuEntry[]>([]);
|
||||
const [closedDays, setClosedDays] = useState<ClosedDay[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// Zelle, in der gerade ein Gericht hinzugefügt wird
|
||||
@@ -43,6 +51,19 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
// Mobil: ein Tag auf einmal statt aller fünf gleichzeitig
|
||||
const [selectedDayIdx, setSelectedDayIdx] = useState(todayIdx >= 0 ? todayIdx : 0);
|
||||
|
||||
// Tag, für den gerade das Schließen-Formular offen ist
|
||||
const [closingIso, setClosingIso] = useState<string | null>(null);
|
||||
const [closeReason, setCloseReason] = useState("");
|
||||
const [closeTranslations, setCloseTranslations] = useState<Record<string, string>>({});
|
||||
const [closeTranslateState, setCloseTranslateState] = useState<"idle" | "busy" | "done">(
|
||||
"idle"
|
||||
);
|
||||
const [closeBusy, setCloseBusy] = useState(false);
|
||||
|
||||
function closedDayFor(iso: string): ClosedDay | undefined {
|
||||
return closedDays.find((c) => c.closed_date === iso);
|
||||
}
|
||||
|
||||
function startAdding(iso: string, section: string) {
|
||||
setAdding({ iso, section });
|
||||
setSelectedDish("");
|
||||
@@ -57,17 +78,19 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setError(null);
|
||||
const [se, di, en] = await Promise.all([
|
||||
const [se, di, en, cd] = await Promise.all([
|
||||
fetch("/api/sections"),
|
||||
fetch("/api/dishes"),
|
||||
fetch(`/api/menu-entries?from=${monday}&to=${friday}`),
|
||||
fetch(`/api/closed-days?from=${monday}&to=${friday}`),
|
||||
]);
|
||||
if (!se.ok || !di.ok || !en.ok) {
|
||||
if (!se.ok || !di.ok || !en.ok || !cd.ok) {
|
||||
setError(formatMessage(dict.weekEditor.loadError, { message: "load failed" }));
|
||||
} else {
|
||||
setSections(await se.json());
|
||||
setDishes(await di.json());
|
||||
setEntries(await en.json());
|
||||
setClosedDays(await cd.json());
|
||||
}
|
||||
setLoading(false);
|
||||
}, [monday, friday, dict]);
|
||||
@@ -107,6 +130,77 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
await load();
|
||||
}
|
||||
|
||||
function startClosing(iso: string) {
|
||||
const existing = closedDayFor(iso);
|
||||
setClosingIso(iso);
|
||||
setCloseReason(existing?.reason ?? "");
|
||||
setCloseTranslations({ ...(existing?.translations ?? {}) });
|
||||
setCloseTranslateState("idle");
|
||||
}
|
||||
|
||||
function cancelClosing() {
|
||||
setClosingIso(null);
|
||||
}
|
||||
|
||||
async function saveClosing() {
|
||||
if (!closingIso) return;
|
||||
setCloseBusy(true);
|
||||
setError(null);
|
||||
const res = await fetch("/api/closed-days", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
closed_date: closingIso,
|
||||
reason: closeReason.trim() || null,
|
||||
translations: closeTranslations,
|
||||
}),
|
||||
});
|
||||
setCloseBusy(false);
|
||||
if (!res.ok) {
|
||||
setError(formatMessage(dict.weekEditor.saveError, { message: String(res.status) }));
|
||||
return;
|
||||
}
|
||||
setClosingIso(null);
|
||||
await load();
|
||||
}
|
||||
|
||||
async function reopenDay(iso: string) {
|
||||
setCloseBusy(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/closed-days/${iso}`, { method: "DELETE" });
|
||||
setCloseBusy(false);
|
||||
if (!res.ok) {
|
||||
setError(formatMessage(dict.weekEditor.deleteError, { message: String(res.status) }));
|
||||
return;
|
||||
}
|
||||
if (closingIso === iso) setClosingIso(null);
|
||||
await load();
|
||||
}
|
||||
|
||||
async function autoTranslateClosing() {
|
||||
if (!closingIso || !closeReason.trim()) return;
|
||||
setCloseTranslateState("busy");
|
||||
setError(null);
|
||||
try {
|
||||
const res = await fetch("/api/translate", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
table: "closed_days",
|
||||
id: closedDayFor(closingIso) ? closingIso : null,
|
||||
text: closeReason.trim(),
|
||||
}),
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data?.error ?? `Fehler (${res.status})`);
|
||||
setCloseTranslations((prev) => ({ ...prev, ...(data.translations ?? {}) }));
|
||||
setCloseTranslateState("done");
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : String(err));
|
||||
setCloseTranslateState("idle");
|
||||
}
|
||||
}
|
||||
|
||||
const [hintBefore, hintAfter] = dict.weekEditor.newDishesHint.split("{link}");
|
||||
const selectedDishObj = dishes.find((d) => d.id === selectedDish) ?? null;
|
||||
const query = dishSearch.trim().toLowerCase();
|
||||
@@ -114,6 +208,121 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
? dishes.filter((d) => localized(d, locale).toLowerCase().includes(query))
|
||||
: dishes;
|
||||
|
||||
// Kleine Anzeige/Umschaltung unter dem Tagesdatum: "Geschlossen" +
|
||||
// Wieder-öffnen-Button, sonst ein dezenter "Tag schließen"-Link.
|
||||
function renderClosureBadge(iso: string) {
|
||||
const closed = closedDayFor(iso);
|
||||
if (closed) {
|
||||
return (
|
||||
<div className="mt-1 flex flex-wrap items-center justify-center gap-1 text-xs">
|
||||
<span className="rounded-full bg-red-100 text-red-700 px-2 py-0.5 font-semibold">
|
||||
{dict.weekEditor.closedLabel}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
className="text-gray-400 hover:text-[var(--c-primary)] underline"
|
||||
onClick={() => reopenDay(iso)}
|
||||
disabled={closeBusy}
|
||||
>
|
||||
{dict.weekEditor.reopenDay}
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
className="mt-1 text-xs text-gray-400 hover:text-[var(--c-primary)] underline"
|
||||
onClick={() => startClosing(iso)}
|
||||
>
|
||||
{dict.weekEditor.closeDay}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
|
||||
// Formular zum Schließen/Bearbeiten eines Tages – ein gemeinsames
|
||||
// Formular für Desktop-Tabelle und mobile Tagesansicht, da immer nur
|
||||
// ein Tag gleichzeitig bearbeitet werden kann.
|
||||
function renderClosingForm() {
|
||||
if (!closingIso) return null;
|
||||
const day = days.find((d) => d.iso === closingIso);
|
||||
return (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
saveClosing();
|
||||
}}
|
||||
className="card mb-4 space-y-3"
|
||||
>
|
||||
<h2 className="font-semibold">
|
||||
{dict.weekEditor.closeDay}: {day?.weekday} ·{" "}
|
||||
{formatDateLong(closingIso, dict.dateLocale)}
|
||||
</h2>
|
||||
<div>
|
||||
<label className="label" htmlFor="closed-reason">
|
||||
{dict.weekEditor.closedReasonLabel}
|
||||
</label>
|
||||
<input
|
||||
id="closed-reason"
|
||||
className="input"
|
||||
placeholder={dict.weekEditor.closedReasonPlaceholder}
|
||||
value={closeReason}
|
||||
onChange={(e) => setCloseReason(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
{languages.length > 0 && (
|
||||
<div className="grid gap-3 sm:grid-cols-2">
|
||||
{languages.map((lang) => (
|
||||
<div key={lang.code}>
|
||||
<label className="label" htmlFor={`closed-reason-${lang.code}`}>
|
||||
{lang.label}
|
||||
</label>
|
||||
<input
|
||||
id={`closed-reason-${lang.code}`}
|
||||
className="input"
|
||||
value={closeTranslations[lang.code] ?? ""}
|
||||
onChange={(e) =>
|
||||
setCloseTranslations({
|
||||
...closeTranslations,
|
||||
[lang.code]: e.target.value,
|
||||
})
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{translateEnabled && languages.length > 0 && (
|
||||
<div>
|
||||
<button
|
||||
type="button"
|
||||
className="btn btn-neutral"
|
||||
onClick={autoTranslateClosing}
|
||||
disabled={closeTranslateState === "busy" || !closeReason.trim()}
|
||||
>
|
||||
{closeTranslateState === "busy"
|
||||
? dict.dishManager.translating
|
||||
: dict.dishManager.autoTranslate}
|
||||
</button>
|
||||
{closeTranslateState === "done" && (
|
||||
<span className="ml-2 text-sm text-green-700">
|
||||
{dict.dishManager.translated}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
<div className="flex gap-2">
|
||||
<button type="submit" className="btn btn-add" disabled={closeBusy}>
|
||||
{dict.common.save}
|
||||
</button>
|
||||
<button type="button" className="btn btn-neutral" onClick={cancelClosing}>
|
||||
{dict.common.cancel}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
// Inhalt einer Tag/Essenszeit-Zelle: Eintragsliste + "hinzufügen"-Flow.
|
||||
// Identisches Fragment für Desktop-Tabelle und Mobile-Karten, damit der
|
||||
// Bearbeitungs-Zustand (adding/selectedDish/dishSearch) nicht dupliziert
|
||||
@@ -121,6 +330,7 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
// unabhängig davon, welche Ansicht gerade rendert.
|
||||
function renderCell(iso: string, sectionKey: string) {
|
||||
const isAdding = adding?.iso === iso && adding?.section === sectionKey;
|
||||
const closed = Boolean(closedDayFor(iso));
|
||||
return (
|
||||
<>
|
||||
{entriesFor(entries, iso, sectionKey).map((e) => {
|
||||
@@ -143,7 +353,7 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
{isAdding ? (
|
||||
{closed ? null : isAdding ? (
|
||||
<div className="mt-2 space-y-1 text-left">
|
||||
{selectedDishObj ? (
|
||||
<div className="flex items-center gap-1 text-sm">
|
||||
@@ -250,6 +460,7 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
{renderClosingForm()}
|
||||
{loading ? (
|
||||
<p className="text-gray-500">{dict.weekEditor.loading}</p>
|
||||
) : (
|
||||
@@ -272,9 +483,10 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<h2 className="font-bold text-[var(--c-primary)] mb-2">
|
||||
<h2 className="font-bold text-[var(--c-primary)] mb-1">
|
||||
{selectedDay.weekday} · {formatDateLong(selectedDay.iso, dict.dateLocale)}
|
||||
</h2>
|
||||
<div className="mb-2">{renderClosureBadge(selectedDay.iso)}</div>
|
||||
<div className="space-y-3">
|
||||
{sections.map((s) => (
|
||||
<div key={s.key} className="rounded-md border border-gray-200 overflow-hidden">
|
||||
@@ -301,6 +513,7 @@ export default function WeekEditor({ monday }: { monday: string }) {
|
||||
<span className="font-bold">{d.weekday}</span>
|
||||
<br />
|
||||
{formatDateLong(d.iso, dict.dateLocale)}
|
||||
{renderClosureBadge(d.iso)}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
|
||||
Reference in New Issue
Block a user