Files
psalmtafel-webservice/app/admin/page.tsx
T
robin d871bddc99 feat: add api for getSongInfos
feat: show song informations of selected song
2026-07-28 15:00:48 +02:00

316 lines
10 KiB
TypeScript

"use client";
import { uploadCSVAction } from "./actions";
import { useEffect, useState } from "react";
import Card from "@/components/Card"
import { Input } from "@/components/FormElements";
import { Screens } from "@/components/DatabaseOverview";
interface Songbook {
id: number;
name: string;
language: string;
}
interface Song {
song_id: number;
songbook_id: number;
song_number: string;
title: string;
}
interface SongInfo {
songbook_id: number;
songbook_name: string;
song_number: string;
title: 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);
const [selectedSong, setSelectedSong] = useState<Song | null>(null);
const [songInfos, setSongInfos] = useState<SongInfo[]>([])
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(() => {
if (!selectedSong) {
setSongInfos([]);
return;
}
async function fetchSongInfos() {
try {
const res = await fetch(`/api/songs/${selectedSong?.song_id}`);
if (res.ok) {
const data = await res.json();
setSongInfos(data);
}
} catch (error) {
console.error("Fehler beim Laden der Songinfos:", error);
}
}
fetchSongInfos();
}, [selectedSong]);
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="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>
<Input.Label>Name des Buchs</Input.Label>
<Input.Text
type="text"
placeholder="z.B. Gesangbuch"
value={newBookName}
onChange={(e) => setNewBookName(e.target.value)}
/>
</div>
<div>
<Input.Label>Sprache (Optional)</Input.Label>
<Input.Text
type="text"
placeholder="z.B. Deutsch"
value={newBookLanguage}
onChange={(e) => setNewBookLanguage(e.target.value)}
/>
</div>
<Input.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
</Input.Button>
</div>
</Card>
<Card title="Lied hinzufügen">
<div className="space-y-4">
<div>
<Input.Label>Liederbuch wählen</Input.Label>
<Input.Select
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>
))}
</Input.Select>
</div>
<div className="grid grid-cols-3 gap-2">
<div className="col-span-1">
<Input.Label>Nummer</Input.Label>
<Input.Text
type="text"
placeholder="12a"
value={number}
onChange={(e) => setNumber(e.target.value)}
/>
</div>
<div className="col-span-2">
<Input.Label>Titel</Input.Label>
<Input.Text
type="text"
placeholder="Großer Gott, wir..."
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
</div>
</div>
<Input.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</Input.Button>
</div>
</Card>
<Card title="CSV-Massenimport">
<form action={handleCSVUpload} className="flex flex-col justify-between">
<input type="hidden" name="songbookId" value={selectedSongbookId} />
<div className="space-y-4">
<div>
<Input.Label>Ziel-Liederbuch</Input.Label>
<Input.Select
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>
))}
</Input.Select>
</div>
<div>
<Input.Label>Datei auswählen (.csv)</Input.Label>
<Input.File
name="csvFile"
accept=".csv"
required
/>
</div>
<div className="mt-6 space-y-3">
<Input.Button
type="submit"
>
Hochladen & Verarbeiten
</Input.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 className="grid grid-cols-1 lg:grid-cols-2 gap-6">
<Screens.Songs
actionName="Auswählen"
onSelectSong={(song) => setSelectedSong(song)}
className=""
/>
<Card title="Ausgewähltes Lied">
<Input.Text value={selectedSong ? selectedSong.title : ""} disabled></Input.Text>
<Card title="Informationen" className="mt-4">
<div className="text-sm space-y-1">
<p><span className="font-semibold">Lied-ID:</span> {selectedSong?.song_id ?? "-"}</p>
<p><span className="font-semibold">In Liederbüchern:</span></p>
{songInfos.length > 0 ? (
<ul className="list-disc list-inside pl-2 text-xs text-neutral-400">
{songInfos.map((info) => (
<li key={info.songbook_id}>
{info.songbook_name} (Nr. {info.song_number})
</li>
))}
</ul>
) : (
<p className="text-xs text-neutral-500">Keine Daten geladen</p>
)}
</div>
</Card>
<Card title="Aktionen">
<Card title="Zu Liederbuch hinzufügen">
<Input.Select
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>
))}
</Input.Select>
</Card>
</Card>
</Card>
</div>
</div>
</div>
);
}