"use client"; // Verwaltung der Essenszeiten (Brotzeit, Mittagessen, …): // anlegen, umbenennen, Uhrzeit ändern, löschen, Reihenfolge anpassen. import { useCallback, useEffect, useState } from "react"; import { formatMessage } from "@/lib/i18n/shared"; import { localized } from "@/lib/types"; import type { Section } from "@/lib/types"; import { useDict, useLanguages, useLocale } from "@/components/i18n/LocaleProvider"; interface SectionForm { key: string | null; // null = neue Essenszeit label: string; translations: Record; timeFrom: string; timeTo: string; } const EMPTY_FORM: SectionForm = { key: null, label: "", translations: {}, timeFrom: "", timeTo: "", }; function splitTimeRange(range: string): { from: string; to: string } { const [from, to] = range.split("-").map((s) => s.trim()); return { from: from ?? "", to: to ?? "" }; } export default function SectionManager() { const dict = useDict(); const locale = useLocale(); const languages = useLanguages().filter((l) => l.code !== "de"); const [sections, setSections] = useState([]); const [form, setForm] = useState(null); const [error, setError] = useState(null); const [loading, setLoading] = useState(true); const [busy, setBusy] = useState(false); const load = useCallback(async () => { const res = await fetch("/api/sections"); if (!res.ok) setError(formatMessage(dict.sectionManager.loadError, { message: String(res.status) })); else setSections(await res.json()); setLoading(false); }, [dict]); useEffect(() => { load(); }, [load]); async function save(e: React.FormEvent) { e.preventDefault(); if (!form) return; setError(null); const label = form.label.trim(); if (!label || !form.timeFrom || !form.timeTo) return; const payload = { label, time_range: `${form.timeFrom} - ${form.timeTo}`, translations: form.translations, }; const res = form.key ? await fetch(`/api/sections/${form.key}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) : await fetch("/api/sections", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }); if (!res.ok) { setError(formatMessage(dict.sectionManager.saveError, { message: String(res.status) })); return; } setForm(null); await load(); } async function remove(section: Section) { if ( !window.confirm( formatMessage(dict.sectionManager.confirmDelete, { name: section.label }) ) ) return; const res = await fetch(`/api/sections/${section.key}`, { method: "DELETE" }); if (!res.ok) setError(formatMessage(dict.sectionManager.deleteError, { message: String(res.status) })); await load(); } async function move(section: Section, direction: -1 | 1) { const idx = sections.findIndex((s) => s.key === section.key); const neighbor = sections[idx + direction]; if (!neighbor) return; setBusy(true); setError(null); const [r1, r2] = await Promise.all([ fetch(`/api/sections/${section.key}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sort: neighbor.sort }), }), fetch(`/api/sections/${neighbor.key}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ sort: section.sort }), }), ]); if (!r1.ok || !r2.ok) setError(formatMessage(dict.sectionManager.saveError, { message: "move failed" })); setBusy(false); await load(); } return (

{dict.sectionManager.title}

{error && (

{error}

)} {form && (

{form.key ? dict.sectionManager.editSection : dict.sectionManager.newSection}

setForm({ ...form, label: e.target.value })} required />
{languages.length > 0 && (
{languages.map((lang) => (
setForm({ ...form, translations: { ...form.translations, [lang.code]: e.target.value }, }) } />
))}
)}
setForm({ ...form, timeFrom: e.target.value })} required />
setForm({ ...form, timeTo: e.target.value })} required />
)} {loading ? (

{dict.common.loading}

) : (
    {sections.map((section, idx) => (
  • {localized(section, locale)}{" "} – {section.time_range}
  • ))} {sections.length === 0 && (
  • {dict.sectionManager.empty}
  • )}
)}
); }