HifiPlanet-Website/hifi-src/src/pages/admin/settings/LegalSettings.jsx
Maaxxs 5f1d4e1092 Add CMS v1: editable business identity and legal pages
Phase 5 of the dashboard roadmap. Business-facing data that was
previously hard-coded (and, for address/hours, duplicated three
independent ways across Footer.jsx, ContactPage.jsx, and the i18n
strings) is now stored in app_settings and editable under Website
settings: logo (light/dark variants, falling back to the bundled
defaults until set), street/zip/city, and a day-by-day business_hours
editor. Footer.jsx and ContactPage.jsx now share one open/closed and
hours-formatting implementation (new utils/businessHours.js) instead
of three separate hard-coded representations of the same thing.

New legal_pages table (impressum/datenschutz/agb) + PageContentController
replaces the three fully hard-coded React pages with fetched, admin-
editable HTML - built-in defaults matching the original page content
kick in until an admin saves anything, so a fresh install isn't blank.
Reuses the Phase 4 RichTextEditor (extended with a heading toggle and
inline-code toggle, since the legal text needs both) via a new
"Rechtstexte" tab in Settings. Legal content lives only in DB and is
authored exclusively by logged-in admins, so dangerouslySetInnerHTML
here doesn't take untrusted input.

IMAGE_COLUMNS in SettingsController (export/import) is now table =>
array of columns instead of table => one column, since app_settings
has three image columns (hero/logo/logo_dark) - this also retroactively
fixes hero_image_path never having been wired into export/import at
all. app_settings and legal_pages both got TABLE_COLUMNS/IMPORT_SECTIONS
entries so they round-trip through a hoster-migration export like
everything else.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-09 02:03:59 +02:00

114 lines
4 KiB
JavaScript

import { useEffect, useState } from 'react';
import { api } from '../../../api/client.js';
import RichTextEditor from '../../../components/RichTextEditor.jsx';
const PAGES = [
{ slug: 'impressum', label: 'Impressum' },
{ slug: 'datenschutz', label: 'Datenschutzerklärung' },
{ slug: 'agb', label: 'AGB' },
];
export default function LegalSettings() {
const [pages, setPages] = useState(null);
const [activeSlug, setActiveSlug] = useState('impressum');
const [saveBusy, setSaveBusy] = useState(false);
const [saveError, setSaveError] = useState('');
const [saveSuccess, setSaveSuccess] = useState(false);
useEffect(() => {
api.get('/legal-pages').then(setPages);
}, []);
if (!pages) {
return <p className="text-sm text-neutral-400">Lädt…</p>;
}
const active = pages[activeSlug];
const update = (field, value) => {
setPages((p) => ({ ...p, [activeSlug]: { ...p[activeSlug], [field]: value } }));
setSaveSuccess(false);
};
const handleSave = async () => {
setSaveBusy(true);
setSaveError('');
setSaveSuccess(false);
try {
await api.put(`/legal-pages/${activeSlug}`, {
title: active.title,
body_html: active.body_html,
});
setSaveSuccess(true);
} catch (err) {
setSaveError(err.message);
} finally {
setSaveBusy(false);
}
};
return (
<div className="max-w-3xl space-y-6">
<section className="rounded-xl border border-neutral-200 bg-white p-6 dark:border-neutral-800 dark:bg-neutral-900">
<h2 className="mb-1 font-semibold text-neutral-900 dark:text-white">Rechtstexte</h2>
<p className="mb-4 text-sm text-neutral-500 dark:text-neutral-400">
Impressum, Datenschutzerklärung und AGB direkt hier pflegen, statt einen Deploy für jede Textänderung zu
brauchen. Bitte bei Rechtstexten im Zweifel rechtlich beraten lassen, bevor du größere Änderungen
veröffentlichst.
</p>
<div className="mb-4 flex flex-wrap gap-2">
{PAGES.map((p) => (
<button
key={p.slug}
type="button"
onClick={() => {
setActiveSlug(p.slug);
setSaveSuccess(false);
setSaveError('');
}}
className={`rounded-md px-3 py-1.5 text-sm font-medium transition ${
activeSlug === p.slug
? 'bg-brand-500 text-white'
: 'border border-neutral-300 text-neutral-600 hover:bg-neutral-100 dark:border-neutral-700 dark:text-neutral-300 dark:hover:bg-neutral-800'
}`}
>
{p.label}
</button>
))}
</div>
<div className="space-y-3">
<div>
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">
Überschrift
</label>
<input
value={active.title}
onChange={(e) => update('title', 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>
<RichTextEditor label="Inhalt" value={active.body_html} onChange={(html) => update('body_html', html)} />
</div>
{active.updated_at && (
<p className="mt-3 text-xs text-neutral-400">
Zuletzt geändert: {new Date(active.updated_at.replace(' ', 'T')).toLocaleString('de-DE')}
</p>
)}
{saveError && <p className="mt-3 text-sm text-red-600">{saveError}</p>}
{saveSuccess && <p className="mt-3 text-sm text-green-600 dark:text-green-400">Gespeichert.</p>}
<button
type="button"
onClick={handleSave}
disabled={saveBusy}
className="mt-4 rounded-md bg-brand-500 px-4 py-2 text-sm font-semibold text-white hover:bg-brand-600 disabled:opacity-50"
>
{saveBusy ? 'Speichere…' : 'Speichern'}
</button>
</section>
</div>
);
}