240 lines
9.0 KiB
TypeScript
240 lines
9.0 KiB
TypeScript
"use client";
|
|
|
|
import { uploadCSVAction } from "./actions";
|
|
import { useEffect, useState } from "react";
|
|
|
|
import Card from "@/components/Card"
|
|
import { Input } from "@/components/FormElements";
|
|
|
|
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 font-sans p-4 sm:p-6 lg:p-8">
|
|
<div className="max-w-6xl mx-auto space-y-6">
|
|
|
|
{/* HEADER */}
|
|
|
|
<h2 className="text-lg font-semibold mb-3 flex items-center gap-2">
|
|
Datenbank verwalten, neue Bücher erstellen und Lieder einpflegen
|
|
</h2>
|
|
|
|
{/* DREISPALTIRES GRID */}
|
|
<div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
|
|
|
|
<Card title="Neues Liederbuch">
|
|
<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.Text
|
|
type="text"
|
|
placeholder="z.B. Gesangbuch"
|
|
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.Text
|
|
type="text"
|
|
placeholder="z.B. Deutsch"
|
|
value={newBookLanguage}
|
|
onChange={(e) => setNewBookLanguage(e.target.value)}
|
|
/>
|
|
</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>
|
|
</div>
|
|
</Card>
|
|
|
|
<Card title="Lied hinzufügen">
|
|
<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.Text
|
|
type="text"
|
|
placeholder="12a"
|
|
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.Text
|
|
type="text"
|
|
placeholder="Großer Gott, wir..."
|
|
value={title}
|
|
onChange={(e) => setTitle(e.target.value)}
|
|
/>
|
|
</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>
|
|
</div>
|
|
</Card>
|
|
|
|
<Card title="CSV-Massenimport">
|
|
<form action={handleCSVUpload} className="h-full flex flex-col justify-between">
|
|
<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 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>
|
|
</div>
|
|
</form>
|
|
</Card>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
);
|
|
} |