From 1d047c06a79881527569135576d6a4ba50ea1656 Mon Sep 17 00:00:00 2001 From: Rudi Date: Tue, 13 Oct 2026 21:22:54 +0200 Subject: [PATCH] Changed the Sender Page --- app/admin/actions.ts | 9 +- app/sender/page.tsx | 84 +---------- components/DatabaseOverview.tsx | 241 ++++++++++++++++---------------- lib/db/response.ts | 10 ++ 4 files changed, 145 insertions(+), 199 deletions(-) diff --git a/app/admin/actions.ts b/app/admin/actions.ts index 27fc923..0e3a440 100644 --- a/app/admin/actions.ts +++ b/app/admin/actions.ts @@ -1,8 +1,11 @@ "use server"; -import { error } from "console"; +//import { error } from "console"; import Papa from "papaparse"; -import { addSongToSongbook } from "@/lib/db/managing"; +import { createSong } from "@/lib/db/song"; +//import { createSongBook } from "@/lib/db/songbook.ts"; +import { addSongToBook } from "@/lib/db/songbooksongs.ts"; + interface CSVRow { nummer: string; @@ -40,4 +43,4 @@ export async function uploadCSVAction(formData:FormData) { console.error(error); return { success: false, error: "Error"}; } -} \ No newline at end of file +} diff --git a/app/sender/page.tsx b/app/sender/page.tsx index 71e4465..c8962f2 100644 --- a/app/sender/page.tsx +++ b/app/sender/page.tsx @@ -1,97 +1,27 @@ "use client"; -import { useState, useEffect } from "react"; - -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; -} - export default function SenderPage() { - const [songbooks, setSongbooks] = useState([]); - const [song_search_name, setSongSearchName] = useState(""); - const [allSongs, setAllSongs] = useState([]); - const [searchResults, setSearchResults] = useState([]); - - const getSongslist = async () => { + const sendSong = async (title: string, bookTitle: string, number: string) => { 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!"); - } - }; - - 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(); - getSongslist(); - }, []); - - const sendID = async (sID: string, sbID: string) => { - if (!sID || !sbID) { - alert("Bitte Liederbuch UND SongID auswählen/eingeben!"); - return; - } - - try { - const res = await fetch("/api/stream/currentID", { + const res = await fetch("/api/stream", { method: "POST", headers: { "Content-Type": "application/json" }, - body: JSON.stringify({ songbookID: sbID, songID: sID }), + body: JSON.stringify({ song: title, songBook: bookTitle, number }), }); if (!res.ok) { alert("Fehler beim Senden."); } } catch (error) { - console.error("Failed sending current songID:", error); - } - }; - - 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); + console.error("Failed sending current song:", error); } }; return ( sendID(String(song.song_id), String(song.songbook_id))} + actionName="Anzeigen" + onSelectSong={(song, book, number) => sendSong(song.title, book.title, number)} /> ); -} \ No newline at end of file +} diff --git a/components/DatabaseOverview.tsx b/components/DatabaseOverview.tsx index 1e327f7..069f3f4 100644 --- a/components/DatabaseOverview.tsx +++ b/components/DatabaseOverview.tsx @@ -1,143 +1,146 @@ -import { useState, useEffect, ComponentPropsWithoutRef } from "react"; +import { useState, useEffect, useMemo, ComponentPropsWithoutRef } from "react"; import Card from "./Card" import { Input } from "./FormElements" +import { Song, SongBookDetailed } from "@/lib/db/response"; interface SongsProps extends Omit, "title" | "children"> { - onSelectSong?: (song: Song) => void; - actionName?: string; + onSelectSong?: (song: Song, book: { id: number; title: string }, songNumber: string) => void; + actionName?: string; + songBooks?: SongBookDetailed[]; } -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); - } - }; +function Songs({ actionName, onSelectSong, songBooks, className = "", ...props }: SongsProps) { + const [query, setQuery] = useState(""); + const [fetchedBooks, setFetchedBooks] = useState([]); 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() { + if (songBooks) return; + async function load() { try { const res = await fetch("/api/songbooks"); - if (res.ok) { - const data = await res.json(); - setSongbooks(data); - } + if (!res.ok) return; + const list: { id: number }[] = await res.json(); + + const details = await Promise.all( + list.map(async (b) => { + const r = await fetch(`/api/songbooks/${b.id}`); + return r.ok ? await r.json() : null; + }) + ); + setFetchedBooks(details.filter(Boolean)); } catch (error) { console.error("Loading songbooks failed: ", error); } } - loadSongbooks(); - getSongslist(); - }, []); - + load(); + }, [songBooks]); + + const books = songBooks ?? fetchedBooks; + + // Pro Buch filtern; Bücher ohne Treffer werden ausgeblendet + const groups = useMemo(() => { + const q = query.trim().toLowerCase(); + return books + .map((book) => { + const bookMatches = q !== "" && book.title.toLowerCase().includes(q); + const songs = (book.songs ?? []).filter( + (e) => + !q || + bookMatches || + e.song.title.toLowerCase().includes(q) || + String(e.songNumber).toLowerCase().includes(q) + ); + return { book, songs }; + }) + .filter((g) => g.songs.length > 0); + }, [books, query]); + + const th = "p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider"; + return ( - -
- search(e.target.value)} - /> -
- {/* TABELLE */} -
- - - - - - - +
+ setQuery(e.target.value)} + /> +
+
+
TitelLiederbuchNummerAktion
+ + + + + + + + {groups.length === 0 ? ( + + + + + + ) : ( + groups.map(({ book, songs }) => ( + + + + + {songs.map((entry) => ( + + + + - - - {searchResults.length === 0 ? ( - - - - ) : ( - searchResults.map((song) => { - const songBook = songbooks.find((book) => book.id === song.songbook_id); - return ( - - - - - - - ); - }) - )} - -
NummerTitelAktion
+ Keine Lieder gefunden +
+ {book.title} + {book.language && ( + + ({book.language}) + + )} +
+ {entry.songNumber} + + {entry.song.title} + + + onSelectSong?.( + entry.song, + { id: book.id, title: book.title }, + String(entry.songNumber) + ) + } + 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} + +
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/lib/db/response.ts b/lib/db/response.ts index 55af0e9..67aa8d3 100644 --- a/lib/db/response.ts +++ b/lib/db/response.ts @@ -18,6 +18,16 @@ export interface SongDetailed { }; } +export interface SongBookDetailed { + id: number; + title: string; + language: string; + songs?: { + songNumber: string; + song: Song; + }[] +} + export type APIResponse = | { success: true; status: number; data: T } | { success: false; status: number; error: string };