"use client"; // Verwaltung der Essenszeiten (Brotzeit, Mittagessen, …): // anlegen, umbenennen, Uhrzeit ändern, löschen, Reihenfolge anpassen. import { useCallback, useEffect, useMemo, useState } from "react"; import { createClient } from "@/lib/supabase/client"; import { formatMessage } from "@/lib/i18n/shared"; import { localized } from "@/lib/types"; import type { Section } from "@/lib/types"; import { useDict, useLocale } from "@/components/i18n/LocaleProvider"; interface SectionForm { key: string | null; // null = neue Essenszeit label: string; labelEn: string; labelPl: string; timeFrom: string; timeTo: string; } const EMPTY_FORM: SectionForm = { key: null, label: "", labelEn: "", labelPl: "", 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 supabase = useMemo(() => createClient(), []); const dict = useDict(); const locale = useLocale(); 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 { data, error } = await supabase.from("sections").select("*").order("sort"); if (error) setError(formatMessage(dict.sectionManager.loadError, { message: error.message })); else setSections(data as Section[]); setLoading(false); }, [supabase, 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, label_en: form.labelEn.trim() || null, label_pl: form.labelPl.trim() || null, time_range: `${form.timeFrom} - ${form.timeTo}`, }; let dbError; if (form.key) { ({ error: dbError } = await supabase .from("sections") .update(payload) .eq("key", form.key)); } else { // Frisch aus der DB lesen statt den lokalen State zu nutzen, damit // ein veralteter Snapshot nicht zu doppelten sort-Werten führt. const { data: maxRow } = await supabase .from("sections") .select("sort") .order("sort", { ascending: false }) .limit(1) .maybeSingle(); ({ error: dbError } = await supabase .from("sections") .insert({ ...payload, sort: (maxRow?.sort ?? 0) + 1 })); } if (dbError) { setError(formatMessage(dict.sectionManager.saveError, { message: dbError.message })); return; } setForm(null); await load(); } async function remove(section: Section) { if ( !window.confirm( formatMessage(dict.sectionManager.confirmDelete, { name: section.label }) ) ) return; const { error } = await supabase.from("sections").delete().eq("key", section.key); if (error) setError(formatMessage(dict.sectionManager.deleteError, { message: error.message })); 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([ supabase.from("sections").update({ sort: neighbor.sort }).eq("key", section.key), supabase.from("sections").update({ sort: section.sort }).eq("key", neighbor.key), ]); const dbError = r1.error ?? r2.error; if (dbError) setError(formatMessage(dict.sectionManager.saveError, { message: dbError.message })); 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 />
setForm({ ...form, labelEn: e.target.value })} />
setForm({ ...form, labelPl: 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}
  • )}
)}
); }