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 { id: number; songbook_id: number; song_number: string; title: string; } interface Songbook { id: number; title: string; language: string; } function Songs({ actionName = "Auswählen", 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!", error); } }; 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 ( ); }) )}
Titel Liederbuch Nummer Aktion
Keine Lieder gefunden
{song.title} {songBook ? songBook.title : "Unbekannt"} {songBook ? songBook.title : "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, };