diff --git a/components/DatabaseOverview.tsx b/components/DatabaseOverview.tsx new file mode 100644 index 0000000..245e95f --- /dev/null +++ b/components/DatabaseOverview.tsx @@ -0,0 +1,138 @@ +import { useState, useEffect } from "react"; + +import Card from "./Card" +import { Input } from "./FormElements" + +interface SongsProps { + onSelectSong?: (song: Song) => void; +} + +interface Song { + song_id: number; + songbook_id: number; + song_number: string; + title: string; +} + +interface Songbook { + id: number; + name: string; + language: string; +} + +function Songs({onSelectSong}: 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" + > + Anzeigen + +
+
+
+ ) +} + +export const Screens = { + Songs: Songs, +} \ No newline at end of file