- 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
224 lines
9.5 KiB
JavaScript
224 lines
9.5 KiB
JavaScript
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
import { Link } from 'react-router-dom';
|
|
import { api } from '../../api/client.js';
|
|
import ImageUploadField from '../../components/ImageUploadField.jsx';
|
|
import SortableList, { saveReorder } from '../../components/admin/SortableList.jsx';
|
|
import ViewOnSite from '../../components/admin/ViewOnSite.jsx';
|
|
import { useToast, useUnsavedChanges } from '../../context/AdminUiContext.jsx';
|
|
|
|
const emptyForm = { name: '', gallery_brand_id: '', cover_image_path: '', sort_order: 0 };
|
|
|
|
export default function GalleryProjects() {
|
|
const [projects, setProjects] = useState([]);
|
|
const [brands, setBrands] = useState([]);
|
|
const [form, setForm] = useState(emptyForm);
|
|
const [savedForm, setSavedForm] = useState(emptyForm);
|
|
const [editingId, setEditingId] = useState(null);
|
|
const [error, setError] = useState('');
|
|
const [brandFilter, setBrandFilter] = useState('all');
|
|
const formRef = useRef(null);
|
|
const toast = useToast();
|
|
|
|
useUnsavedChanges(JSON.stringify(form) !== JSON.stringify(savedForm), 'gallery-projects');
|
|
|
|
const filteredProjects = useMemo(
|
|
() => (brandFilter === 'all' ? projects : projects.filter((p) => String(p.gallery_brand_id) === brandFilter)),
|
|
[projects, brandFilter]
|
|
);
|
|
// Sortiert wird immer innerhalb einer Marke - in der Gesamtliste wuerde eine Verschiebung
|
|
// ueber Markengrenzen hinweg nichts bedeuten.
|
|
const canSort = brandFilter !== 'all';
|
|
|
|
const load = () => {
|
|
api.get('/gallery-projects').then(setProjects);
|
|
api.get('/gallery-brands').then(setBrands);
|
|
};
|
|
|
|
useEffect(load, []);
|
|
|
|
const startEdit = (project) => {
|
|
const next = {
|
|
name: project.name,
|
|
gallery_brand_id: project.gallery_brand_id,
|
|
cover_image_path: project.cover_image_path || '',
|
|
sort_order: project.sort_order,
|
|
};
|
|
setEditingId(project.id);
|
|
setForm(next);
|
|
setSavedForm(next);
|
|
formRef.current?.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
};
|
|
|
|
const resetForm = () => {
|
|
setEditingId(null);
|
|
setForm(emptyForm);
|
|
setSavedForm(emptyForm);
|
|
};
|
|
|
|
const handleSubmit = async (e) => {
|
|
e.preventDefault();
|
|
setError('');
|
|
const payload = { ...form, gallery_brand_id: Number(form.gallery_brand_id) };
|
|
try {
|
|
if (editingId) {
|
|
await api.put(`/gallery-projects/${editingId}`, payload);
|
|
} else {
|
|
const nextSort = projects
|
|
.filter((p) => Number(p.gallery_brand_id) === payload.gallery_brand_id)
|
|
.reduce((max, p) => Math.max(max, Number(p.sort_order) || 0), 0) + 10;
|
|
await api.post('/gallery-projects', { ...payload, sort_order: nextSort });
|
|
}
|
|
toast.success(editingId ? 'Projekt gespeichert.' : 'Projekt angelegt.');
|
|
resetForm();
|
|
load();
|
|
} catch (err) {
|
|
setError(err.message);
|
|
}
|
|
};
|
|
|
|
const handleDelete = async (id) => {
|
|
if (!confirm('Projekt inkl. aller Fotos wirklich löschen?')) return;
|
|
try {
|
|
await api.delete(`/gallery-projects/${id}`);
|
|
if (editingId === id) resetForm();
|
|
toast.success('Projekt gelöscht.');
|
|
load();
|
|
} catch (err) {
|
|
toast.error(`Löschen fehlgeschlagen: ${err.message}`);
|
|
}
|
|
};
|
|
|
|
const handleReorder = async (reordered) => {
|
|
const previous = projects;
|
|
const ids = new Set(reordered.map((p) => p.id));
|
|
const merge = (list) => [...projects.filter((p) => !ids.has(p.id)), ...list];
|
|
setProjects(merge(reordered));
|
|
try {
|
|
setProjects(merge(await saveReorder('gallery-projects', reordered)));
|
|
toast.success('Reihenfolge gespeichert.');
|
|
} catch (err) {
|
|
setProjects(previous);
|
|
toast.error(`Reihenfolge konnte nicht gespeichert werden: ${err.message}`);
|
|
}
|
|
};
|
|
|
|
const rowCls = 'bg-white px-4 py-2 dark:bg-neutral-950';
|
|
|
|
return (
|
|
<div className="grid gap-6 lg:grid-cols-3">
|
|
<div className="lg:col-span-2">
|
|
<div className="mb-4 flex items-center justify-between gap-3">
|
|
<h1 className="text-xl font-bold text-neutral-900 dark:text-white">Galerie-Projekte</h1>
|
|
<ViewOnSite to="/galerie" />
|
|
</div>
|
|
|
|
<div className="mb-3 flex flex-wrap items-center gap-2">
|
|
<label className="text-sm font-medium text-neutral-600 dark:text-neutral-300">Nach Marke filtern:</label>
|
|
<select
|
|
value={brandFilter}
|
|
onChange={(e) => setBrandFilter(e.target.value)}
|
|
className="rounded-md border border-neutral-300 bg-white px-3 py-1.5 text-sm dark:border-neutral-700 dark:bg-neutral-900"
|
|
>
|
|
<option value="all">Alle Marken</option>
|
|
{brands.map((b) => <option key={b.id} value={String(b.id)}>{b.name}</option>)}
|
|
</select>
|
|
{brandFilter !== 'all' && (
|
|
<button onClick={() => setBrandFilter('all')} className="text-sm text-brand-600 hover:underline">
|
|
Filter zurücksetzen
|
|
</button>
|
|
)}
|
|
<span className="text-sm text-neutral-400">{filteredProjects.length} von {projects.length}</span>
|
|
</div>
|
|
<p className="mb-3 text-xs text-neutral-500 dark:text-neutral-400">
|
|
{canSort ? 'Reihenfolge am Griff links per Ziehen ändern.' : 'Zum Sortieren erst eine Marke auswählen.'}
|
|
</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>
|
|
{canSort && <th className="w-10 px-2 py-2"><span className="sr-only">Reihenfolge</span></th>}
|
|
<th className="px-4 py-2">Marke</th>
|
|
<th className="px-4 py-2">Projekt</th>
|
|
<th className="px-4 py-2 text-right">Aktionen</th>
|
|
</tr>
|
|
</thead>
|
|
<SortableList
|
|
items={filteredProjects}
|
|
onReorder={handleReorder}
|
|
as="tbody"
|
|
disabled={!canSort}
|
|
className="divide-y divide-neutral-100 dark:divide-neutral-800"
|
|
>
|
|
{(project, handle) => (
|
|
<>
|
|
{canSort && <td className="bg-white px-2 py-2 dark:bg-neutral-950">{handle}</td>}
|
|
<td className={rowCls}>{project.brand_name}</td>
|
|
<td className={`${rowCls} font-medium text-neutral-800 dark:text-neutral-100`}>
|
|
{project.name}
|
|
<ViewOnSite to={`/galerie/${project.brand_slug}/${project.slug}`} className="ml-2" />
|
|
</td>
|
|
<td className={`${rowCls} whitespace-nowrap text-right`}>
|
|
<Link to={`/admin/gallery-projects/${project.id}/photos`} className="mr-3 text-brand-600 hover:underline">Fotos</Link>
|
|
<button onClick={() => startEdit(project)} className="mr-3 text-brand-600 hover:underline">Bearbeiten</button>
|
|
<button onClick={() => handleDelete(project.id)} className="text-red-600 hover:underline">Löschen</button>
|
|
</td>
|
|
</>
|
|
)}
|
|
</SortableList>
|
|
</table>
|
|
{filteredProjects.length === 0 && (
|
|
<p className="px-4 py-6 text-center text-sm text-neutral-400">
|
|
{projects.length === 0 ? 'Noch keine Projekte angelegt.' : 'Keine Projekte für diese Marke.'}
|
|
</p>
|
|
)}
|
|
</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 ? 'Projekt bearbeiten' : 'Neues Projekt'}</h2>
|
|
<div>
|
|
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Marke *</label>
|
|
<select
|
|
required
|
|
value={form.gallery_brand_id}
|
|
onChange={(e) => setForm({ ...form, gallery_brand_id: 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"
|
|
>
|
|
<option value="">Bitte wählen…</option>
|
|
{brands.map((b) => <option key={b.id} value={b.id}>{b.name}</option>)}
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label className="mb-1 block text-sm font-medium text-neutral-700 dark:text-neutral-300">Projektname *</label>
|
|
<input
|
|
required
|
|
placeholder="z. B. Audi TT"
|
|
value={form.name}
|
|
onChange={(e) => setForm({ ...form, name: 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>
|
|
<ImageUploadField
|
|
value={form.cover_image_path}
|
|
onChange={(path) => setForm({ ...form, cover_image_path: path })}
|
|
aspect={4 / 3}
|
|
label="Titelbild (optional)"
|
|
/>
|
|
|
|
{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>
|
|
);
|
|
}
|