Files
psalmtafel-webservice/app/admin/page.tsx
T
2026-07-03 16:23:04 +02:00

266 lines
11 KiB
TypeScript

"use client";
import { uploadCSVAction } from "./actions";
import { useEffect, useState } from "react";
interface Songbook {
id: number;
name: string;
language: string;
}
export default function AdminPage() {
const [songbooks, setSongbooks] = useState<Songbook[]>([]);
const [selectedSongbookId, setSelectedSongbookId] = useState("");
const [number, setNumber] = useState("");
const [title, setTitle] = useState("");
const [newBookName, setNewBookName] = useState("");
const [newBookLanguage, setNewBookLanguage] = useState("");
const [uploadStatus, setUploadStatus] = useState<string | null>(null);
async function handleCSVUpload(formData: FormData) {
setUploadStatus("Verarbeite...");
const result = await uploadCSVAction(formData);
if (result.success) {
setUploadStatus(`Erfolg: ${result.message}`);
} else {
setUploadStatus(`Fehler: ${result.error}`);
}
}
useEffect(() => {
async function loadSongbooks() {
try {
const res = await fetch("/api/songbooks");
if (res.ok) {
const data = await res.json();
setSongbooks(data);
}
} catch (error) {
console.error("Loading songbooks failed: ", error);
}
}
loadSongbooks();
}, []);
const addSong = async () => {
if (!selectedSongbookId || !number || !title) {
alert("Alle Felder ausfüllen!");
return;
}
try {
const res = await fetch("/api/songs", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
songbookId: Number(selectedSongbookId),
number: number,
title: title
})
});
if (res.ok) {
alert("Lied erfolgreich hinzugefügt!");
setNumber("");
setTitle("");
} else {
alert("Fehler beim Senden.");
}
} catch (error) {
console.error("Failed sending current songID:", error);
}
};
const addSongbook = async () => {
if (!newBookName) {
alert("Der Name des Liederbuches darf nicht leer sein!");
return;
}
try {
const res = await fetch("/api/songbooks", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: newBookName, language: newBookLanguage })
});
if (res.ok) {
const createdBook = await res.json();
setSongbooks([...songbooks, createdBook]);
alert("Liederbuch erfolgreich erstellt!");
setNewBookName("");
setNewBookLanguage("");
} else {
alert("Failed adding songbook!");
}
} catch (error) {
console.error("Error: ", error);
}
};
return (
<div className="min-h-screen bg-black text-white font-sans p-4 sm:p-6 lg:p-8">
<div className="max-w-6xl mx-auto space-y-6">
{/* HEADER */}
<header className="border-b border-neutral-800 pb-4">
<h1 className="text-2xl sm:text-3xl font-bold tracking-tight text-white">Chor-Anzeige Admin Panel</h1>
<p className="text-neutral-400 text-sm mt-1">Datenbank verwalten, neue Bücher erstellen und Lieder einpflegen.</p>
</header>
{/* DREISPALTIRES GRID */}
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
{/* KARTE 1: NEUES LIEDERBUCH */}
<section className="bg-neutral-900 border border-neutral-800 rounded-xl p-5 shadow-md flex flex-col justify-between">
<div>
<h2 className="text-lg font-semibold text-white mb-4 flex items-center gap-2">
<span className="h-2 w-2 rounded-full bg-blue-400"></span> Neues Liederbuch
</h2>
<div className="space-y-4">
<div>
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Name des Buchs</label>
<input
type="text"
placeholder="z.B. Gesangbuch"
className="w-full bg-black border border-neutral-700 rounded-lg px-3 py-2 text-white placeholder-neutral-500 focus:outline-none focus:border-blue-400 text-sm"
value={newBookName}
onChange={(e) => setNewBookName(e.target.value)}
/>
</div>
<div>
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Sprache (Optional)</label>
<input
type="text"
placeholder="z.B. Deutsch"
className="w-full bg-black border border-neutral-700 rounded-lg px-3 py-2 text-white placeholder-neutral-500 focus:outline-none focus:border-blue-400 text-sm"
value={newBookLanguage}
onChange={(e) => setNewBookLanguage(e.target.value)}
/>
</div>
</div>
</div>
<button
onClick={addSongbook}
className="w-full mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg px-4 py-2.5 transition text-sm shadow-sm"
>
Liederbuch erstellen
</button>
</section>
{/* KARTE 2: LIED HINZUFÜGEN */}
<section className="bg-neutral-900 border border-neutral-800 rounded-xl p-5 shadow-md flex flex-col justify-between">
<div>
<h2 className="text-lg font-semibold text-white mb-4 flex items-center gap-2">
<span className="h-2 w-2 rounded-full bg-blue-400"></span> Lied hinzufügen
</h2>
<div className="space-y-4">
<div>
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Liederbuch wählen</label>
<select
className="w-full bg-black border border-neutral-700 rounded-lg px-3 py-2 text-white focus:outline-none focus:border-blue-400 text-sm h-[38px]"
value={selectedSongbookId}
onChange={(e) => setSelectedSongbookId(e.target.value)}
>
<option value="">-- Bitte wählen --</option>
{songbooks.map((book) => (
<option key={book.id} value={String(book.id)}>
{book.name}{book.language ? ` (${book.language})` : ""}
</option>
))}
</select>
</div>
<div className="grid grid-cols-3 gap-2">
<div className="col-span-1">
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Nummer</label>
<input
type="text"
placeholder="12a"
className="w-full bg-black border border-neutral-700 rounded-lg px-3 py-2 text-white placeholder-neutral-500 focus:outline-none focus:border-blue-400 text-sm"
value={number}
onChange={(e) => setNumber(e.target.value)}
/>
</div>
<div className="col-span-2">
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Liedtitel</label>
<input
type="text"
placeholder="Großer Gott, wir..."
className="w-full bg-black border border-neutral-700 rounded-lg px-3 py-2 text-white placeholder-neutral-500 focus:outline-none focus:border-blue-400 text-sm"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
</div>
</div>
</div>
</div>
<button
onClick={addSong}
className="w-full mt-6 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg px-4 py-2.5 transition text-sm shadow-sm"
>
Lied speichern
</button>
</section>
{/* KARTE 3: CSV IMPORT */}
<section className="bg-neutral-900 border border-neutral-800 rounded-xl p-5 shadow-md flex flex-col justify-between">
<form action={handleCSVUpload} className="h-full flex flex-col justify-between">
<div>
<h2 className="text-lg font-semibold text-white mb-4 flex items-center gap-2">
<span className="h-2 w-2 rounded-full bg-blue-400"></span> CSV-Massenimport
</h2>
<input type="hidden" name="songbookId" value={selectedSongbookId} />
<div className="space-y-4">
<div>
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Ziel-Liederbuch</label>
<select
className="w-full bg-black border border-neutral-700 rounded-lg px-3 py-2 text-white focus:outline-none focus:border-blue-400 text-sm h-[38px]"
value={selectedSongbookId}
onChange={(e) => setSelectedSongbookId(e.target.value)}
>
<option value="">-- Bitte wählen --</option>
{songbooks.map((book) => (
<option key={book.id} value={String(book.id)}>
{book.name}{book.language ? ` (${book.language})` : ""}
</option>
))}
</select>
</div>
<div>
<label className="block text-xs font-semibold text-neutral-400 uppercase tracking-wider mb-1.5">Datei auswählen (.csv)</label>
<input
type="file"
name="csvFile"
accept=".csv"
required
className="w-full text-sm text-neutral-400 file:mr-4 file:py-2 file:px-4 file:rounded-md file:border-0 file:text-xs file:font-semibold file:bg-neutral-800 file:text-neutral-200 hover:file:bg-neutral-700 file:transition cursor-pointer"
/>
</div>
</div>
</div>
<div className="mt-6 space-y-3">
<button
type="submit"
className="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg px-4 py-2.5 transition text-sm shadow-sm"
>
Hochladen & Verarbeiten
</button>
{uploadStatus && (
<p className="text-xs text-center font-medium px-3 py-2 rounded-md bg-black border border-neutral-800 text-blue-400">
{uploadStatus}
</p>
)}
</div>
</form>
</section>
</div>
</div>
</div>
);
}