diff --git a/app/admin/page.tsx b/app/admin/page.tsx index e9eb936..72af9c2 100644 --- a/app/admin/page.tsx +++ b/app/admin/page.tsx @@ -1,10 +1,11 @@ "use client"; import { uploadCSVAction } from "./actions"; -import { useEffect, useState } from "react"; +import { JSXElementConstructor, Key, ReactElement, ReactNode, ReactPortal, useEffect, useState } from "react"; import Card from "@/components/Card" import { Input } from "@/components/FormElements"; +import { Screens } from "@/components/DatabaseOverview"; interface Songbook { id: number; @@ -12,6 +13,20 @@ interface Songbook { 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([]); const [selectedSongbookId, setSelectedSongbookId] = useState(""); @@ -24,6 +39,9 @@ export default function AdminPage() { const [uploadStatus, setUploadStatus] = useState(null); + const [selectedSong, setSelectedSong] = useState(null); + const [songInfos, setSongInfos] = useState([]) + async function handleCSVUpload(formData: FormData) { setUploadStatus("Verarbeite..."); const result = await uploadCSVAction(formData); @@ -35,6 +53,27 @@ export default function AdminPage() { } } + 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 { @@ -78,6 +117,57 @@ export default function AdminPage() { } }; + const attachSong = async () => { + if (!selectedSongbookId || !number || !selectedSong) { + alert("Alle Felder ausfüllen!"); + return; + } + + try { + const res = await fetch("/api/songs/attach", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + songbookId: selectedSongbookId, + songId: selectedSong.song_id, + number: number, + }) + }); + if (res.ok) { + alert("Lied erfolgreich zum Liederbuch hinzugefügt!"); + } else { + alert("Fehler beim Senden."); + } + } catch (error) { + console.error("Failed sending current songID:", error); + } + } + + const removeSong = async () => { + if (!selectedSongbookId || !selectedSong) { + alert("Alle Felder ausfüllen!"); + return; + } + + try { + const res = await fetch("/api/songs/remove", { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify({ + songbookId: selectedSongbookId, + songId: selectedSong.song_id + }) + }); + if (res.ok) { + alert("Lied erfolgreich aus dem Liederbuch gelöscht!"); + } 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!"); @@ -121,7 +211,7 @@ export default function AdminPage() {
- + Name des Buchs
- + Sprache (Optional)
- + Liederbuch wählen setSelectedSongbookId(e.target.value)} @@ -165,7 +255,7 @@ export default function AdminPage() {
- + Nummer
- + Titel -
+
- + Ziel-Liederbuch setSelectedSongbookId(e.target.value)} @@ -206,7 +296,7 @@ export default function AdminPage() {
- + Datei auswählen (.csv)
+
+ setSelectedSong(song)} + className="" + /> + + + +
+

Lied-ID: {selectedSong?.song_id ?? "-"}

+

In Liederbüchern:

+ {songInfos.length > 0 ? ( +
    + {songInfos.map((info) => ( +
  • + {info.songbook_name} (Nr. {info.song_number}) +
  • + ))} +
+ ) : ( +

Keine Daten geladen

+ )} +
+
+ + +
+
+ Ziel-Liederbuch + setSelectedSongbookId(e.target.value)} + > + + {songbooks.map((book) => ( + + ))} + +
+
+ Nummer + setNumber(e.target.value)} + /> +
+ + Lied hinzufügen + +
+
+ +
+
+ Ziel-Liederbuch + setSelectedSongbookId(e.target.value)} + > + + {songInfos.length > 0 ? ( + songInfos.map((info) => ( + + )) + ): ""} + +
+ + Lied aus Liederbuch löschen + +
+
+
+
+
); diff --git a/app/api/songs/[id]/route.ts b/app/api/songs/[id]/route.ts new file mode 100644 index 0000000..7b43176 --- /dev/null +++ b/app/api/songs/[id]/route.ts @@ -0,0 +1,20 @@ +import { NextResponse } from "next/server"; +import { getSongInfos } from "@/lib/db/managing"; + +export async function GET( + request: Request, + { params }: { params: Promise<{ id: string }> } +) { + try { + const { id } = await params; + const songInfos = await getSongInfos(id); + + return NextResponse.json(songInfos || []); + } catch (error) { + console.error("API Error:", error); + return NextResponse.json( + { error: "Fehler beim Laden der Songinfos" }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/api/songs/attach/route.ts b/app/api/songs/attach/route.ts new file mode 100644 index 0000000..cceaddb --- /dev/null +++ b/app/api/songs/attach/route.ts @@ -0,0 +1,40 @@ +import { NextResponse } from "next/server"; +import { attachSongToSongbook } from "@/lib/db/managing"; + +export async function POST(request: Request) { + try { + const body = await request.json(); + const { songId, songbookId, number } = body; + + if (isNaN(songId) || isNaN(songbookId) || !number) { + return NextResponse.json( + { error: "Ungültige IDs oder fehlende Nummer." }, + { status: 400 } + ); + } + + const result = await attachSongToSongbook( + Number(songId), + Number(songbookId), + String(number) + ); + + if (result.success) { + return NextResponse.json( + { message: "Lied erfolgreich verknüpft!" }, + { status: 200 } + ); + } else { + return NextResponse.json( + { error: "Fehler beim Verknüpfen in der Datenbank." }, + { status: 500 } + ); + } + } catch (error) { + console.error("API Error in /api/songs/attach:", error); + return NextResponse.json( + { error: "Ungültiges JSON oder Serverfehler." }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/api/songs/remove/route.ts b/app/api/songs/remove/route.ts new file mode 100644 index 0000000..42745b6 --- /dev/null +++ b/app/api/songs/remove/route.ts @@ -0,0 +1,39 @@ +import { NextResponse } from "next/server"; +import { removeSongfromSongbook } from "@/lib/db/managing"; + +export async function POST(request: Request) { + try { + const body = await request.json(); + const { songId, songbookId } = body; + + if (isNaN(songId) || isNaN(songbookId)) { + return NextResponse.json( + { error: "Ungültige IDs." }, + { status: 400 } + ); + } + + const result = await removeSongfromSongbook( + Number(songId), + Number(songbookId) + ); + + if (result.success) { + return NextResponse.json( + { message: "Lied erfolgreich aus dem Liederbuch gelöscht!" }, + { status: 200 } + ); + } else { + return NextResponse.json( + { error: "Fehler beim Löschen aus dem Liederbuch." }, + { status: 500 } + ); + } + } catch (error) { + console.error("API Error in /api/songs/remove:", error); + return NextResponse.json( + { error: "Ungültiges JSON oder Serverfehler." }, + { status: 500 } + ); + } +} \ No newline at end of file diff --git a/app/sender/page.tsx b/app/sender/page.tsx index a5a3486..71e4465 100644 --- a/app/sender/page.tsx +++ b/app/sender/page.tsx @@ -3,6 +3,7 @@ import { useState, useEffect } from "react"; import { Input } from "@/components/FormElements"; +import { Screens } from "@/components/DatabaseOverview"; interface Songbook { id: number; @@ -88,71 +89,9 @@ export default function SenderPage() { }; return ( -
- {/* LIEDERSUCHE */} -
-

- Liedersuche & Datenbank -

- -
- search(e.target.value)} - /> -
- - {/* TABELLE */} -
- - - - - - - - - - - {searchResults.length === 0 ? ( - - - - ) : ( - searchResults.map((song) => { - const songBook = songbooks.find((book) => book.id === song.songbook_id); - return ( - - - - - - - ); - }) - )} - -
TitelLiederbuchNummerAktion
Keine Lieder gefunden
- {song.title} - - {songBook ? songBook.name : "Unbekannt"} - - - {songBook ? songBook.name : "Unbekannt"} - - {song.song_number} - - sendID(String(song.song_id), String(song.songbook_id))} - className="bg-white dark:bg-black hover:bg-blue-600 dark:hover:bg-blue-600 border border-neutral-300 dark:border-neutral-700 hover:border-blue-600 dark:hover:border-blue-500 hover:text-white font-medium rounded-md px-3 py-1.5 transition text-xs" - > - Anzeigen - -
-
-
-
+ sendID(String(song.song_id), String(song.songbook_id))} + /> ); } \ No newline at end of file diff --git a/components/Card.tsx b/components/Card.tsx index 0a8450d..495b328 100644 --- a/components/Card.tsx +++ b/components/Card.tsx @@ -1,14 +1,25 @@ -import { ReactNode } from "react"; +import { ReactNode, ComponentPropsWithoutRef } from "react"; -export default function Card({title, children}: {title: string, children: ReactNode}) { - return ( -
-

- {title} -

-
- {children} -
-
- ); +interface CardProps extends ComponentPropsWithoutRef<"section"> { + title: string; + children: ReactNode; + hasLeftBorder?: boolean; +} + +export default function Card({ title, hasLeftBorder = false, children, className = "", ...props }: CardProps) { + return ( +
+

+ {title} +

+ + {/* border-l-2 fügt den Strich links ein, pl-4 macht Platz zum Inhalt */} +
+ {children} +
+
+ ); } \ No newline at end of file diff --git a/components/DatabaseOverview.tsx b/components/DatabaseOverview.tsx new file mode 100644 index 0000000..1e327f7 --- /dev/null +++ b/components/DatabaseOverview.tsx @@ -0,0 +1,143 @@ +import { useState, useEffect, ComponentPropsWithoutRef } from "react"; + +import Card from "./Card" +import { Input } from "./FormElements" + +interface SongsProps extends Omit, "title" | "children"> { + onSelectSong?: (song: Song) => void; + actionName?: string; +} + +interface Song { + song_id: number; + songbook_id: number; + song_number: string; + title: string; +} + +interface Songbook { + id: number; + name: string; + language: string; +} + +function Songs({ actionName, onSelectSong, className = "", ...props }: SongsProps) { + const [song_search_name, setSongSearchName] = useState(""); + const [searchResults, setSearchResults] = useState([]); + const [allSongs, setAllSongs] = useState([]); + const [songbooks, setSongbooks] = useState([]); + + + const search = (query: string) => { + setSongSearchName(query); + if (query.trim() !== "") { + setSearchResults( + allSongs.filter((song) => + song.title.toLowerCase().includes(query.toLowerCase()) || + song.song_number.includes(query) + ) + ); + } else { + setSearchResults(allSongs); + } + }; + + useEffect(() => { + const getSongslist = async () => { + try { + const res = await fetch("/api/songs"); + if (res.ok) { + const data = await res.json(); + setAllSongs(data); + setSearchResults(data); + } + } catch (error) { + setAllSongs([]); + console.error("Failed fetching songslist!"); + } + }; + + 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(); + getSongslist(); + }, []); + + return ( + +
+ search(e.target.value)} + /> +
+ {/* TABELLE */} +
+ + + + + + + + + + + {searchResults.length === 0 ? ( + + + + ) : ( + searchResults.map((song) => { + const songBook = songbooks.find((book) => book.id === song.songbook_id); + return ( + + + + + + + ); + }) + )} + +
TitelLiederbuchNummerAktion
Keine Lieder gefunden
+ {song.title} + + {songBook ? songBook.name : "Unbekannt"} + + + {songBook ? songBook.name : "Unbekannt"} + + {song.song_number} + + onSelectSong?.(song)} + className="bg-white dark:bg-black hover:bg-blue-600 dark:hover:bg-blue-600 border border-neutral-300 dark:border-neutral-700 hover:border-blue-600 dark:hover:border-blue-500 hover:text-white font-medium rounded-md px-3 py-1.5 transition text-xs" + > + {actionName} + +
+
+
+ ) +} + +export const Screens = { + Songs: Songs, +} \ No newline at end of file diff --git a/components/FormElements.tsx b/components/FormElements.tsx index d2f722c..fd78cad 100644 --- a/components/FormElements.tsx +++ b/components/FormElements.tsx @@ -3,13 +3,14 @@ import { ComponentPropsWithoutRef } from "react"; interface TextfieldProps extends ComponentPropsWithoutRef<"input"> {} -function Textfield({value, onChange, placeholder}: TextfieldProps) { +function Textfield({value, onChange, placeholder, ...props}: TextfieldProps) { return ( ) } @@ -46,9 +47,18 @@ function Button({ children, ...props }: ComponentPropsWithoutRef<"button">) { ) } +function Label( {children}: ComponentPropsWithoutRef<"label">) { + return ( + + ) +} + export const Input = { Text: Textfield, Select: Dropdown, File: File, Button: Button, + Label: Label, }; \ No newline at end of file diff --git a/components/Header.tsx b/components/Header.tsx index c9b3882..d0711fc 100644 --- a/components/Header.tsx +++ b/components/Header.tsx @@ -2,7 +2,7 @@ import NavItem from "./NavItem"; export default function Header() { return ( -
+
PsalmTafel