Nicht angemeldete Besucher können unter /vorschlag ein fehlendes Gericht vorschlagen (Name + optionale Beschreibung); Dublettenprüfung gegen bestehende Gerichte, einfacher Honeypot gegen Bot-Spam. Staff/ Admin sichten eingereichte Vorschläge unter /intern/vorschlaege und übernehmen sie als Gericht oder lehnen sie ab.
138 lines
4.4 KiB
TypeScript
138 lines
4.4 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { createClient } from "@/lib/supabase/client";
|
|
import { formatMessage } from "@/lib/i18n/shared";
|
|
import { useDict } from "@/components/i18n/LocaleProvider";
|
|
import type { DishSuggestion } from "@/lib/types";
|
|
|
|
export default function SuggestionReview() {
|
|
const supabase = useMemo(() => createClient(), []);
|
|
const dict = useDict();
|
|
const [items, setItems] = useState<DishSuggestion[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [info, setInfo] = useState<string | null>(null);
|
|
const [busyId, setBusyId] = useState<string | null>(null);
|
|
|
|
const load = useCallback(async () => {
|
|
const { data, error } = await supabase
|
|
.from("dish_suggestions")
|
|
.select("*")
|
|
.order("created_at", { ascending: false });
|
|
if (error)
|
|
setError(formatMessage(dict.suggestions.loadError, { message: error.message }));
|
|
else setItems(data as DishSuggestion[]);
|
|
setLoading(false);
|
|
}, [supabase, dict]);
|
|
|
|
useEffect(() => {
|
|
load();
|
|
}, [load]);
|
|
|
|
async function accept(item: DishSuggestion) {
|
|
setBusyId(item.id);
|
|
setError(null);
|
|
setInfo(null);
|
|
const { error: insertError } = await supabase.from("dishes").insert({
|
|
name: item.name,
|
|
price: 0,
|
|
});
|
|
if (insertError) {
|
|
setError(
|
|
formatMessage(dict.suggestions.actionError, { message: insertError.message })
|
|
);
|
|
setBusyId(null);
|
|
return;
|
|
}
|
|
const { error: deleteError } = await supabase
|
|
.from("dish_suggestions")
|
|
.delete()
|
|
.eq("id", item.id);
|
|
if (deleteError) {
|
|
setError(
|
|
formatMessage(dict.suggestions.actionError, { message: deleteError.message })
|
|
);
|
|
setBusyId(null);
|
|
return;
|
|
}
|
|
setInfo(dict.suggestions.accepted);
|
|
setBusyId(null);
|
|
await load();
|
|
}
|
|
|
|
async function reject(item: DishSuggestion) {
|
|
if (!window.confirm(formatMessage(dict.suggestions.confirmReject, { name: item.name })))
|
|
return;
|
|
setBusyId(item.id);
|
|
setError(null);
|
|
setInfo(null);
|
|
const { error } = await supabase.from("dish_suggestions").delete().eq("id", item.id);
|
|
if (error) {
|
|
setError(formatMessage(dict.suggestions.actionError, { message: error.message }));
|
|
} else {
|
|
setInfo(dict.suggestions.rejected);
|
|
}
|
|
setBusyId(null);
|
|
await load();
|
|
}
|
|
|
|
return (
|
|
<div className="max-w-3xl">
|
|
<h1 className="text-xl font-bold text-[var(--c-primary)] mb-4">
|
|
{dict.suggestions.reviewTitle}
|
|
</h1>
|
|
|
|
{error && (
|
|
<p className="mb-3 text-sm text-[var(--c-del)]" role="alert">
|
|
{error}
|
|
</p>
|
|
)}
|
|
{info && <p className="mb-3 text-sm text-green-700">{info}</p>}
|
|
|
|
{loading ? (
|
|
<p className="text-gray-500">{dict.common.loading}</p>
|
|
) : (
|
|
<ul className="divide-y divide-gray-200 border border-gray-200 rounded-md bg-white">
|
|
{items.map((item) => (
|
|
<li key={item.id} className="flex flex-wrap items-center gap-3 px-4 py-3">
|
|
<span className="mr-auto">
|
|
<span className="font-semibold">{item.name}</span>
|
|
{item.description && (
|
|
<span className="block text-sm text-gray-600 mt-0.5">
|
|
{item.description}
|
|
</span>
|
|
)}
|
|
<span className="block text-xs text-gray-400 mt-0.5">
|
|
{formatMessage(dict.suggestions.submittedOn, {
|
|
date: new Date(item.created_at).toLocaleDateString(dict.dateLocale),
|
|
})}
|
|
</span>
|
|
</span>
|
|
<button
|
|
type="button"
|
|
className="btn btn-add"
|
|
onClick={() => accept(item)}
|
|
disabled={busyId === item.id}
|
|
>
|
|
{dict.suggestions.accept}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
className="btn btn-del"
|
|
onClick={() => reject(item)}
|
|
disabled={busyId === item.id}
|
|
>
|
|
{dict.suggestions.reject}
|
|
</button>
|
|
</li>
|
|
))}
|
|
{items.length === 0 && (
|
|
<li className="px-4 py-3 text-gray-500">{dict.suggestions.reviewEmpty}</li>
|
|
)}
|
|
</ul>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|