From e8f332da43a6772453d885ccbef8bed1e6ae8664 Mon Sep 17 00:00:00 2001 From: Daniel S Date: Sun, 9 Aug 2026 22:30:13 +0200 Subject: [PATCH] feat(pages): add page opening, page deletion API, edit mode toggle and custom 404 page --- src/pages/404.astro | 25 +++++++++++++++ src/pages/admin/index.astro | 6 +++- src/pages/admin/pages.astro | 50 +++++++++++++++++++++++++++-- src/pages/api/admin/pages/delete.ts | 40 +++++++++++++++++++++++ 4 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 src/pages/404.astro create mode 100644 src/pages/api/admin/pages/delete.ts diff --git a/src/pages/404.astro b/src/pages/404.astro new file mode 100644 index 0000000..c889e8c --- /dev/null +++ b/src/pages/404.astro @@ -0,0 +1,25 @@ +--- +// src/pages/404.astro +import Layout from '../layouts/Layout.astro'; +--- + + +
+
+ 404 +

Seite nicht gefunden

+

+ Die von Ihnen angeforderte Seite existiert nicht oder wurde verschoben. +

+ + +
+
+
diff --git a/src/pages/admin/index.astro b/src/pages/admin/index.astro index f464ffd..208f278 100644 --- a/src/pages/admin/index.astro +++ b/src/pages/admin/index.astro @@ -19,13 +19,17 @@ const presets = Object.values(THEME_PRESETS);

System & Admin Dashboard

+ + 🛠️ Bearbeitungsmodus (Entwurf-Vorschau) + - 🌐 Live Seite Ansehen + 🌐 Live-Seite Ansehen
+ diff --git a/src/pages/admin/pages.astro b/src/pages/admin/pages.astro index 33942a9..93bd558 100644 --- a/src/pages/admin/pages.astro +++ b/src/pages/admin/pages.astro @@ -66,15 +66,35 @@ const currentHomepageId = (siteConfig as any).site_info?.homepage_id; - ✏️ Im Editor öffnen + 🔗 Öffnen + + ✏️ Editor + + {!isHomepage && ( + + )} ); })} + @@ -138,4 +158,28 @@ const currentHomepageId = (siteConfig as any).site_info?.homepage_id; window.location.reload(); } }); + + // Event: Seite löschen + document.querySelectorAll('.btn-delete-page').forEach(btn => { + btn.addEventListener('click', async (e: any) => { + const pageId = e.target.getAttribute('data-page-id'); + const pageTitle = e.target.getAttribute('data-page-title'); + + if (!confirm(`Möchten Sie die Seite "${pageTitle}" wirklich unwiderruflich löschen?`)) return; + + const res = await fetch('/api/admin/pages/delete', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ page_id: pageId }) + }); + + if (res.ok) { + window.location.reload(); + } else { + const data = await res.json(); + alert(data.error || 'Fehler beim Löschen der Seite.'); + } + }); + }); + diff --git a/src/pages/api/admin/pages/delete.ts b/src/pages/api/admin/pages/delete.ts new file mode 100644 index 0000000..36e86a7 --- /dev/null +++ b/src/pages/api/admin/pages/delete.ts @@ -0,0 +1,40 @@ +import type { APIRoute } from 'astro'; +import fs from 'node:fs/promises'; +import path from 'node:path'; + +export const prerender = false; + +const DATA_DIR = process.env.DATA_DIR || path.join(process.cwd(), 'app', 'data'); + +export const POST: APIRoute = async ({ request }) => { + try { + const { page_id } = await request.json(); + if (!page_id) { + return new Response(JSON.stringify({ error: 'page_id erforderlich' }), { status: 400 }); + } + + const configPath = path.join(DATA_DIR, 'site.config.json'); + let config: any = {}; + + try { + const rawData = await fs.readFile(configPath, 'utf-8'); + config = JSON.parse(rawData); + } catch { + return new Response(JSON.stringify({ error: 'Konfigurationsdatei nicht gefunden' }), { status: 404 }); + } + + // Verhindere Löschen der aktiven Homepage + if (config.site_info?.homepage_id === page_id) { + return new Response(JSON.stringify({ error: 'Die aktive Startseite kann nicht gelöscht werden.' }), { status: 400 }); + } + + config.pages = (config.pages || []).filter((p: any) => p.id !== page_id); + + await fs.mkdir(DATA_DIR, { recursive: true }); + await fs.writeFile(configPath, JSON.stringify(config, null, 2), 'utf-8'); + + return new Response(JSON.stringify({ success: true, message: 'Seite erfolgreich gelöscht' }), { status: 200 }); + } catch (error) { + return new Response(JSON.stringify({ error: 'Fehler beim Löschen der Seite' }), { status: 500 }); + } +};