Files
psalmtafel-webservice/app/sender/page.tsx
T
2026-07-27 16:06:43 +02:00

97 lines
2.3 KiB
TypeScript

"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<Songbook[]>([]);
const [song_search_name, setSongSearchName] = useState("");
const [allSongs, setAllSongs] = useState<Song[]>([]);
const [searchResults, setSearchResults] = useState<Song[]>([]);
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 (
<Screens.Songs
actionName="Anzeigen"
onSelectSong={(song) => sendID(String(song.song_id), String(song.songbook_id))}
/>
);
}