"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 () => { 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", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ songbookID: sbID, songID: sID }), }); 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); } }; return ( sendID(String(song.song_id), String(song.songbook_id))} /> ); }