- Kontaktformular mit Spam-Schutz (Honeypot, Mindestzeit, Rate-Limit), klarer Bestätigung, Paketpreis vom Server, WhatsApp mit vorbereitetem Text - Preis-Hinweis unter Paketpreisen, eigene 404-Seite, verständliche Fehler - Admin: Toasts, Speicherleiste, Warnung bei ungespeicherten Änderungen, Re-Login bei abgelaufener Sitzung, Sortieren per Drag & Drop - Neues Recht system.admin für DB, Mail, Google, Export/Import, Reset - Englische Texte für Leistungen/Pakete/Upgrades (content_translations) - Automatische Schema-Migration beim ersten Aufruf nach git pull - Build in hifi/ neu erzeugt Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015Dgc8etkq7JkjyZgD2DEbv
197 lines
7.9 KiB
JavaScript
197 lines
7.9 KiB
JavaScript
import { useEffect, useRef, useState } from 'react';
|
||
import { api } from '../../api/client.js';
|
||
import SortableList, { saveReorder } from '../../components/admin/SortableList.jsx';
|
||
import ViewOnSite from '../../components/admin/ViewOnSite.jsx';
|
||
import { useToast, useUnsavedChanges } from '../../context/AdminUiContext.jsx';
|
||
|
||
const emptyForm = {
|
||
question_de: '',
|
||
answer_de: '',
|
||
question_en: '',
|
||
answer_en: '',
|
||
sort_order: 0,
|
||
};
|
||
|
||
export default function Faqs() {
|
||
const toast = useToast();
|
||
const [faqs, setFaqs] = useState([]);
|
||
const [form, setForm] = useState(emptyForm);
|
||
const [initialForm, setInitialForm] = useState(emptyForm);
|
||
const [editingId, setEditingId] = useState(null);
|
||
const [error, setError] = useState('');
|
||
const formRef = useRef(null);
|
||
|
||
useUnsavedChanges(JSON.stringify(form) !== JSON.stringify(initialForm), 'faqs');
|
||
|
||
const load = () => api.get('/faqs').then(setFaqs);
|
||
|
||
useEffect(load, []);
|
||
|
||
const startEdit = (f) => {
|
||
const next = {
|
||
question_de: f.question_de,
|
||
answer_de: f.answer_de,
|
||
question_en: f.question_en,
|
||
answer_en: f.answer_en,
|
||
sort_order: f.sort_order,
|
||
};
|
||
setEditingId(f.id);
|
||
setForm(next);
|
||
setInitialForm(next);
|
||
setError('');
|
||
requestAnimationFrame(() => {
|
||
formRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||
formRef.current?.querySelector('input')?.focus({ preventScroll: true });
|
||
});
|
||
};
|
||
|
||
const resetForm = () => {
|
||
setEditingId(null);
|
||
setForm(emptyForm);
|
||
setInitialForm(emptyForm);
|
||
};
|
||
|
||
const handleSubmit = async (e) => {
|
||
e.preventDefault();
|
||
setError('');
|
||
const payload = { ...form, sort_order: Number(form.sort_order) };
|
||
try {
|
||
if (editingId) {
|
||
await api.put(`/faqs/${editingId}`, payload);
|
||
} else {
|
||
const sort = faqs.reduce((max, f) => Math.max(max, Number(f.sort_order) || 0), 0) + 10;
|
||
await api.post('/faqs', { ...payload, sort_order: sort });
|
||
}
|
||
toast.success(editingId ? 'Frage gespeichert.' : 'Frage angelegt.');
|
||
resetForm();
|
||
load();
|
||
} catch (err) {
|
||
setError(err.message);
|
||
}
|
||
};
|
||
|
||
const handleDelete = async (id) => {
|
||
if (!confirm('Diese Frage wirklich löschen?')) return;
|
||
try {
|
||
await api.delete(`/faqs/${id}`);
|
||
if (editingId === id) resetForm();
|
||
toast.success('Frage gelöscht.');
|
||
load();
|
||
} catch (err) {
|
||
toast.error(`Löschen fehlgeschlagen: ${err.message}`);
|
||
}
|
||
};
|
||
|
||
const handleReorder = async (reordered) => {
|
||
const previous = faqs;
|
||
setFaqs(reordered);
|
||
try {
|
||
setFaqs(await saveReorder('faqs', reordered));
|
||
toast.success('Reihenfolge gespeichert.');
|
||
} catch (err) {
|
||
setFaqs(previous);
|
||
toast.error(`Reihenfolge konnte nicht gespeichert werden: ${err.message}`);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="grid gap-6 lg:grid-cols-3">
|
||
<div className="lg:col-span-2">
|
||
<div className="mb-1 flex flex-wrap items-center justify-between gap-2">
|
||
<h1 className="text-xl font-bold text-neutral-900 dark:text-white">Häufig gestellte Fragen</h1>
|
||
<ViewOnSite to="/#faq" label="Auf der Startseite ansehen" />
|
||
</div>
|
||
<p className="mb-4 text-sm text-neutral-500 dark:text-neutral-400">
|
||
Diese Fragen erscheinen im FAQ-Bereich der Startseite, jeweils in der aktuell gewählten Sprache. Die 6
|
||
mitgelieferten Einträge sind die Standard-Einstellung – du kannst sie bearbeiten, löschen und beliebig
|
||
neue hinzufügen. Reihenfolge am Griff links per Ziehen ändern.
|
||
</p>
|
||
<div className="overflow-x-auto rounded-xl border border-neutral-200 dark:border-neutral-800">
|
||
<table className="w-full text-left text-sm">
|
||
<thead className="bg-neutral-50 text-neutral-500 dark:bg-neutral-900 dark:text-neutral-400">
|
||
<tr>
|
||
<th className="w-8 px-2 py-2" aria-label="Reihenfolge" />
|
||
<th className="px-4 py-2">Frage (DE)</th>
|
||
<th className="px-4 py-2 text-right">Aktionen</th>
|
||
</tr>
|
||
</thead>
|
||
<SortableList items={faqs} onReorder={handleReorder} as="tbody" className="divide-y divide-neutral-100 dark:divide-neutral-800">
|
||
{(f, handle) => (
|
||
<>
|
||
<td className="bg-white px-2 py-2 dark:bg-neutral-950">{handle}</td>
|
||
<td className="bg-white px-4 py-2 font-medium text-neutral-800 dark:bg-neutral-950 dark:text-neutral-100">{f.question_de}</td>
|
||
<td className="whitespace-nowrap bg-white px-4 py-2 text-right dark:bg-neutral-950">
|
||
<button onClick={() => startEdit(f)} className="mr-3 text-brand-600 hover:underline">Bearbeiten</button>
|
||
<button onClick={() => handleDelete(f.id)} className="text-red-600 hover:underline">Löschen</button>
|
||
</td>
|
||
</>
|
||
)}
|
||
</SortableList>
|
||
{faqs.length === 0 && (
|
||
<tbody>
|
||
<tr><td colSpan={3} className="px-4 py-6 text-center text-neutral-400">Noch keine Fragen angelegt.</td></tr>
|
||
</tbody>
|
||
)}
|
||
</table>
|
||
</div>
|
||
</div>
|
||
|
||
<form ref={formRef} onSubmit={handleSubmit} className="h-fit scroll-mt-20 space-y-4 rounded-xl border border-neutral-200 bg-white p-5 dark:border-neutral-800 dark:bg-neutral-900">
|
||
<h2 className="font-bold text-neutral-900 dark:text-white">{editingId ? 'Frage bearbeiten' : 'Neue Frage'}</h2>
|
||
|
||
<div>
|
||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Frage (Deutsch) *</label>
|
||
<input
|
||
required
|
||
value={form.question_de}
|
||
onChange={(e) => setForm({ ...form, question_de: e.target.value })}
|
||
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Antwort (Deutsch) *</label>
|
||
<textarea
|
||
required
|
||
rows={3}
|
||
value={form.answer_de}
|
||
onChange={(e) => setForm({ ...form, answer_de: e.target.value })}
|
||
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||
/>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Frage (Englisch) *</label>
|
||
<input
|
||
required
|
||
value={form.question_en}
|
||
onChange={(e) => setForm({ ...form, question_en: e.target.value })}
|
||
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||
/>
|
||
</div>
|
||
<div>
|
||
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Antwort (Englisch) *</label>
|
||
<textarea
|
||
required
|
||
rows={3}
|
||
value={form.answer_en}
|
||
onChange={(e) => setForm({ ...form, answer_en: e.target.value })}
|
||
className="w-full rounded-md border border-neutral-300 bg-white px-3 py-2 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
||
/>
|
||
</div>
|
||
|
||
{error && <p className="text-sm text-red-600">{error}</p>}
|
||
|
||
<div className="flex gap-2">
|
||
<button type="submit" className="rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600">
|
||
{editingId ? 'Speichern' : 'Anlegen'}
|
||
</button>
|
||
{editingId && (
|
||
<button type="button" onClick={resetForm} className="rounded-md border border-neutral-300 px-4 py-2 text-sm dark:border-neutral-700">
|
||
Abbrechen
|
||
</button>
|
||
)}
|
||
</div>
|
||
</form>
|
||
</div>
|
||
);
|
||
}
|