Neue interne Route Handler unter app/api/ (dishes inkl. Bulk-Delete, sections, allergens, additives, menu-entries, dish-suggestions, settings, admin/branding) ersetzen den direkten Browser→Postgrest- Zugriff der bisherigen Supabase-Anbindung. Jede Route prüft Autorisierung selbst (lib/auth/session.ts requireRole) statt sich auf Postgres-RLS zu verlassen – ersetzt 1:1 die bisherigen Policies (öffentlich lesbar / staff+admin schreiben / nur admin für Einstellungen & Benutzer). Rezeptvorschlag-Dublettencheck (SuggestionForm) läuft jetzt zwingend serverseitig im POST-Handler, da es keinen Browser→Postgres-Pfad mehr gibt. "Passkey annehmen" (SuggestionReview) läuft transaktional (Gericht anlegen + Vorschlag löschen in einem Schritt). Logo/Favicon: neuer lokaler Dateispeicher (lib/branding.ts, Docker- Volume) statt Supabase-Storage-Bucket, ausgeliefert über app/branding/[filename]. logoUrl()/faviconUrl() liefern jetzt einfache relative Pfade statt einer fest verdrahteten Supabase-Storage-URL. DeepL-Route (api/translate) und Benutzerverwaltung (api/admin/users) laufen jetzt gegen die users-Tabelle statt Supabase Auth. Alle "use client"-Manager-Komponenten (DishManager, WeekEditor, SectionManager, CodeListManager, SuggestionForm/Review, UserManager, DesignEditor) rufen ihre jeweiligen /api/-Routen per fetch() statt den Supabase-Browser-Client zu nutzen.
234 lines
8.1 KiB
TypeScript
234 lines
8.1 KiB
TypeScript
"use client";
|
||
|
||
import { useCallback, useEffect, useState } from "react";
|
||
import Link from "next/link";
|
||
import {
|
||
addDays,
|
||
dishCodes,
|
||
entriesFor,
|
||
formatDateLong,
|
||
formatPrice,
|
||
mondayOf,
|
||
todayISO,
|
||
} 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";
|
||
|
||
export default function WeekEditor({ monday }: { monday: string }) {
|
||
const dict = useDict();
|
||
const locale = useLocale();
|
||
const friday = addDays(monday, 4);
|
||
const days = dict.weekdays.long.map((weekday, i) => ({
|
||
weekday,
|
||
iso: addDays(monday, i),
|
||
}));
|
||
|
||
const [sections, setSections] = useState<Section[]>([]);
|
||
const [dishes, setDishes] = useState<Dish[]>([]);
|
||
const [entries, setEntries] = useState<MenuEntry[]>([]);
|
||
const [loading, setLoading] = useState(true);
|
||
const [error, setError] = useState<string | null>(null);
|
||
// Zelle, in der gerade ein Gericht hinzugefügt wird
|
||
const [adding, setAdding] = useState<{ iso: string; section: string } | null>(
|
||
null
|
||
);
|
||
const [selectedDish, setSelectedDish] = useState("");
|
||
|
||
const load = useCallback(async () => {
|
||
setError(null);
|
||
const [se, di, en] = await Promise.all([
|
||
fetch("/api/sections"),
|
||
fetch("/api/dishes"),
|
||
fetch(`/api/menu-entries?from=${monday}&to=${friday}`),
|
||
]);
|
||
if (!se.ok || !di.ok || !en.ok) {
|
||
setError(formatMessage(dict.weekEditor.loadError, { message: "load failed" }));
|
||
} else {
|
||
setSections(await se.json());
|
||
setDishes(await di.json());
|
||
setEntries(await en.json());
|
||
}
|
||
setLoading(false);
|
||
}, [monday, friday, dict]);
|
||
|
||
useEffect(() => {
|
||
setLoading(true);
|
||
load();
|
||
}, [load]);
|
||
|
||
async function addEntry() {
|
||
if (!adding || !selectedDish) return;
|
||
const res = await fetch("/api/menu-entries", {
|
||
method: "POST",
|
||
headers: { "Content-Type": "application/json" },
|
||
body: JSON.stringify({
|
||
menu_date: adding.iso,
|
||
section_key: adding.section,
|
||
dish_id: selectedDish,
|
||
}),
|
||
});
|
||
if (!res.ok) {
|
||
const data = await res.json().catch(() => ({}));
|
||
setError(
|
||
data.error === "duplicate_entry"
|
||
? dict.weekEditor.duplicateEntry
|
||
: formatMessage(dict.weekEditor.saveError, { message: data.error ?? String(res.status) })
|
||
);
|
||
}
|
||
setAdding(null);
|
||
setSelectedDish("");
|
||
await load();
|
||
}
|
||
|
||
async function removeEntry(id: string) {
|
||
const res = await fetch(`/api/menu-entries/${id}`, { method: "DELETE" });
|
||
if (!res.ok)
|
||
setError(formatMessage(dict.weekEditor.deleteError, { message: String(res.status) }));
|
||
await load();
|
||
}
|
||
|
||
const [hintBefore, hintAfter] = dict.weekEditor.newDishesHint.split("{link}");
|
||
|
||
return (
|
||
<div>
|
||
<div className="flex flex-wrap items-center gap-3 mb-4">
|
||
<h1 className="text-xl font-bold text-[var(--c-primary)] mr-auto">
|
||
{dict.weekEditor.title}
|
||
</h1>
|
||
<Link href={`/intern?woche=${addDays(monday, -7)}`} className="btn btn-add">
|
||
{dict.publicMenu.prevWeek}
|
||
</Link>
|
||
<Link
|
||
href={`/intern?woche=${mondayOf(todayISO())}`}
|
||
className="btn btn-primary"
|
||
>
|
||
{dict.publicMenu.today}
|
||
</Link>
|
||
<Link href={`/intern?woche=${addDays(monday, 7)}`} className="btn btn-add">
|
||
{dict.publicMenu.nextWeek}
|
||
</Link>
|
||
</div>
|
||
|
||
{error && (
|
||
<p className="mb-3 text-sm text-[var(--c-del)]" role="alert">
|
||
{error}
|
||
</p>
|
||
)}
|
||
{loading ? (
|
||
<p className="text-gray-500">{dict.weekEditor.loading}</p>
|
||
) : (
|
||
<table className="menu-table">
|
||
<thead>
|
||
<tr>
|
||
<th className="section-head w-52" aria-hidden />
|
||
{days.map((d) => (
|
||
<th key={d.iso} className="day-head">
|
||
<span className="font-bold">{d.weekday}</span>
|
||
<br />
|
||
{formatDateLong(d.iso, dict.dateLocale)}
|
||
</th>
|
||
))}
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{sections.map((s) => (
|
||
<tr key={s.key}>
|
||
<th className="section-head">
|
||
<div>{localized(s, locale)}</div>
|
||
<div className="time">{s.time_range}</div>
|
||
</th>
|
||
{days.map((d) => {
|
||
const isAdding =
|
||
adding?.iso === d.iso && adding?.section === s.key;
|
||
return (
|
||
<td key={d.iso} className="cell">
|
||
{entriesFor(entries, d.iso, s.key).map((e) => {
|
||
const codes = dishCodes(e.dish);
|
||
return (
|
||
<div key={e.id} className="dish">
|
||
<span className="font-bold">
|
||
{localized(e.dish, locale)}
|
||
</span>
|
||
{codes && <sup className="dish-codes">{codes}</sup>}
|
||
{" – "}
|
||
{formatPrice(e.dish.price)}
|
||
<div className="mt-1">
|
||
<button
|
||
type="button"
|
||
className="btn btn-del"
|
||
onClick={() => removeEntry(e.id)}
|
||
>
|
||
{dict.common.delete}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
);
|
||
})}
|
||
{isAdding ? (
|
||
<div className="mt-2 space-y-1 text-left">
|
||
<select
|
||
className="input"
|
||
value={selectedDish}
|
||
onChange={(e) => setSelectedDish(e.target.value)}
|
||
>
|
||
<option value="">{dict.weekEditor.selectDish}</option>
|
||
{dishes.map((dish) => (
|
||
<option key={dish.id} value={dish.id}>
|
||
{localized(dish, locale)} ({formatPrice(dish.price)})
|
||
</option>
|
||
))}
|
||
</select>
|
||
<div className="flex gap-1 justify-center">
|
||
<button
|
||
type="button"
|
||
className="btn btn-add"
|
||
onClick={addEntry}
|
||
disabled={!selectedDish}
|
||
>
|
||
{dict.common.add}
|
||
</button>
|
||
<button
|
||
type="button"
|
||
className="btn btn-neutral"
|
||
onClick={() => {
|
||
setAdding(null);
|
||
setSelectedDish("");
|
||
}}
|
||
>
|
||
{dict.common.cancel}
|
||
</button>
|
||
</div>
|
||
</div>
|
||
) : (
|
||
<button
|
||
type="button"
|
||
className="btn btn-add mt-1"
|
||
onClick={() => {
|
||
setAdding({ iso: d.iso, section: s.key });
|
||
setSelectedDish("");
|
||
}}
|
||
>
|
||
{dict.weekEditor.addMeal}
|
||
</button>
|
||
)}
|
||
</td>
|
||
);
|
||
})}
|
||
</tr>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
)}
|
||
<p className="mt-4 text-sm text-gray-500">
|
||
{hintBefore}
|
||
<Link href="/intern/gerichte" className="underline">
|
||
{dict.weekEditor.newDishesLinkLabel}
|
||
</Link>
|
||
{hintAfter}
|
||
</p>
|
||
</div>
|
||
);
|
||
}
|