Changed the Sender Page

This commit is contained in:
Rudi committed 2026-10-13 21:22:54 +02:00
1 parent bdc864cf94
commit 1d047c06a7
4 files changed
+145 -199

No files matched your search

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