Changed the Sender Page
This commit is contained in:
1 parent
bdc864cf94
commit
1d047c06a7
4 files changed
+140
-194
No files matched your search
@@ -1,8 +1,11 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { error } from "console";
|
//import { error } from "console";
|
||||||
import Papa from "papaparse";
|
import Papa from "papaparse";
|
||||||
import { addSongToSongbook } from "@/lib/db/managing";
|
import { createSong } from "@/lib/db/song";
|
||||||
|
//import { createSongBook } from "@/lib/db/songbook.ts";
|
||||||
|
import { addSongToBook } from "@/lib/db/songbooksongs.ts";
|
||||||
|
|
||||||
|
|
||||||
interface CSVRow {
|
interface CSVRow {
|
||||||
nummer: string;
|
nummer: string;
|
||||||
|
|||||||
+6
-76
@@ -1,97 +1,27 @@
|
|||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useState, useEffect } from "react";
|
|
||||||
|
|
||||||
import { Input } from "@/components/FormElements";
|
|
||||||
import { Screens } from "@/components/DatabaseOverview";
|
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() {
|
export default function SenderPage() {
|
||||||
const [songbooks, setSongbooks] = useState<Songbook[]>([]);
|
const sendSong = async (title: string, bookTitle: string, number: string) => {
|
||||||
const [song_search_name, setSongSearchName] = useState("");
|
|
||||||
const [allSongs, setAllSongs] = useState<Song[]>([]);
|
|
||||||
const [searchResults, setSearchResults] = useState<Song[]>([]);
|
|
||||||
|
|
||||||
const getSongslist = async () => {
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/songs");
|
const res = await fetch("/api/stream", {
|
||||||
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",
|
method: "POST",
|
||||||
headers: { "Content-Type": "application/json" },
|
headers: { "Content-Type": "application/json" },
|
||||||
body: JSON.stringify({ songbookID: sbID, songID: sID }),
|
body: JSON.stringify({ song: title, songBook: bookTitle, number }),
|
||||||
});
|
});
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
alert("Fehler beim Senden.");
|
alert("Fehler beim Senden.");
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed sending current songID:", error);
|
console.error("Failed sending current song:", 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 (
|
return (
|
||||||
<Screens.Songs
|
<Screens.Songs
|
||||||
actionName="Anzeigen"
|
actionName="Anzeigen"
|
||||||
onSelectSong={(song) => sendID(String(song.song_id), String(song.songbook_id))}
|
onSelectSong={(song, book, number) => sendSong(song.title, book.title, number)}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
+119
-116
@@ -1,139 +1,142 @@
|
|||||||
import { useState, useEffect, ComponentPropsWithoutRef } from "react";
|
import { useState, useEffect, useMemo, ComponentPropsWithoutRef } from "react";
|
||||||
|
|
||||||
import Card from "./Card"
|
import Card from "./Card"
|
||||||
import { Input } from "./FormElements"
|
import { Input } from "./FormElements"
|
||||||
|
import { Song, SongBookDetailed } from "@/lib/db/response";
|
||||||
|
|
||||||
interface SongsProps extends Omit<ComponentPropsWithoutRef<typeof Card>, "title" | "children"> {
|
interface SongsProps extends Omit<ComponentPropsWithoutRef<typeof Card>, "title" | "children"> {
|
||||||
onSelectSong?: (song: Song) => void;
|
onSelectSong?: (song: Song, book: { id: number; title: string }, songNumber: string) => void;
|
||||||
actionName?: string;
|
actionName?: string;
|
||||||
|
songBooks?: SongBookDetailed[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Song {
|
function Songs({ actionName, onSelectSong, songBooks, className = "", ...props }: SongsProps) {
|
||||||
song_id: number;
|
const [query, setQuery] = useState("");
|
||||||
songbook_id: number;
|
const [fetchedBooks, setFetchedBooks] = useState<SongBookDetailed[]>([]);
|
||||||
song_number: string;
|
|
||||||
title: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface Songbook {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
language: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
function Songs({ actionName, onSelectSong, className = "", ...props }: SongsProps) {
|
|
||||||
const [song_search_name, setSongSearchName] = useState("");
|
|
||||||
const [searchResults, setSearchResults] = useState<Song[]>([]);
|
|
||||||
const [allSongs, setAllSongs] = useState<Song[]>([]);
|
|
||||||
const [songbooks, setSongbooks] = useState<Songbook[]>([]);
|
|
||||||
|
|
||||||
|
|
||||||
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(() => {
|
useEffect(() => {
|
||||||
const getSongslist = async () => {
|
if (songBooks) return;
|
||||||
try {
|
async function load() {
|
||||||
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 {
|
try {
|
||||||
const res = await fetch("/api/songbooks");
|
const res = await fetch("/api/songbooks");
|
||||||
if (res.ok) {
|
if (!res.ok) return;
|
||||||
const data = await res.json();
|
const list: { id: number }[] = await res.json();
|
||||||
setSongbooks(data);
|
|
||||||
}
|
const details = await Promise.all(
|
||||||
|
list.map(async (b) => {
|
||||||
|
const r = await fetch(`/api/songbooks/${b.id}`);
|
||||||
|
return r.ok ? await r.json() : null;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
setFetchedBooks(details.filter(Boolean));
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Loading songbooks failed: ", error);
|
console.error("Loading songbooks failed: ", error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
loadSongbooks();
|
load();
|
||||||
getSongslist();
|
}, [songBooks]);
|
||||||
}, []);
|
|
||||||
|
const books = songBooks ?? fetchedBooks;
|
||||||
|
|
||||||
|
// Pro Buch filtern; Bücher ohne Treffer werden ausgeblendet
|
||||||
|
const groups = useMemo(() => {
|
||||||
|
const q = query.trim().toLowerCase();
|
||||||
|
return books
|
||||||
|
.map((book) => {
|
||||||
|
const bookMatches = q !== "" && book.title.toLowerCase().includes(q);
|
||||||
|
const songs = (book.songs ?? []).filter(
|
||||||
|
(e) =>
|
||||||
|
!q ||
|
||||||
|
bookMatches ||
|
||||||
|
e.song.title.toLowerCase().includes(q) ||
|
||||||
|
String(e.songNumber).toLowerCase().includes(q)
|
||||||
|
);
|
||||||
|
return { book, songs };
|
||||||
|
})
|
||||||
|
.filter((g) => g.songs.length > 0);
|
||||||
|
}, [books, query]);
|
||||||
|
|
||||||
|
const th = "p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider";
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Card
|
<Card
|
||||||
{...props}
|
{...props}
|
||||||
title="Alle Lieder"
|
title="Alle Lieder"
|
||||||
className={`h-[500px] ${className}`}
|
className={`h-[500px] ${className}`}
|
||||||
>
|
>
|
||||||
<div className="mb-4">
|
<div className="mb-4">
|
||||||
<Input.Text
|
<Input.Text
|
||||||
type="text"
|
type="text"
|
||||||
placeholder="Nach Songtitel oder Nummer suchen..."
|
placeholder="Nach Songtitel, Nummer oder Liederbuch suchen..."
|
||||||
value={song_search_name}
|
value={query}
|
||||||
onChange={(e) => search(e.target.value)}
|
onChange={(e) => setQuery(e.target.value)}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
{/* TABELLE */}
|
<div className="flex-1 min-h-0 overflow-auto border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-black">
|
||||||
<div className="flex-1 min-h-0 overflow-auto border border-neutral-200 dark:border-neutral-800 rounded-lg bg-white dark:bg-black">
|
<table className="w-full text-left border-collapse text-sm">
|
||||||
<table className="w-full text-left border-collapse text-sm">
|
<thead className="bg-neutral-100 dark:bg-neutral-900 sticky top-0 border-b border-neutral-200 dark:border-neutral-800 z-10">
|
||||||
<thead className="bg-neutral-100 dark:bg-neutral-900 sticky top-0 border-b border-neutral-200 dark:border-neutral-800 z-10">
|
<tr>
|
||||||
<tr>
|
<th className={`${th} w-24 text-center`}>Nummer</th>
|
||||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider">Titel</th>
|
<th className={th}>Titel</th>
|
||||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider">Liederbuch</th>
|
<th className={`${th} text-right`}>Aktion</th>
|
||||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider text-center">Nummer</th>
|
</tr>
|
||||||
<th className="p-3 text-xs font-semibold text-neutral-600 dark:text-neutral-400 uppercase tracking-wider text-right">Aktion</th>
|
</thead>
|
||||||
|
{groups.length === 0 ? (
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td colSpan={3} className="p-8 text-center text-neutral-400 dark:text-neutral-500">
|
||||||
|
Keine Lieder gefunden
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
) : (
|
||||||
|
groups.map(({ book, songs }) => (
|
||||||
|
<tbody key={book.id} className="divide-y divide-neutral-200 dark:divide-neutral-800">
|
||||||
|
<tr className="bg-neutral-50 dark:bg-neutral-950">
|
||||||
|
<th
|
||||||
|
colSpan={3}
|
||||||
|
className="p-3 text-sm font-bold text-neutral-900 dark:text-white border-y border-neutral-200 dark:border-neutral-800"
|
||||||
|
>
|
||||||
|
{book.title}
|
||||||
|
{book.language && (
|
||||||
|
<span className="ml-2 text-xs font-normal text-neutral-500 dark:text-neutral-400">
|
||||||
|
({book.language})
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
</th>
|
||||||
|
</tr>
|
||||||
|
{songs.map((entry) => (
|
||||||
|
<tr
|
||||||
|
key={`${book.id}-${entry.song.id}`}
|
||||||
|
className="hover:bg-neutral-100/60 dark:hover:bg-neutral-900/40 transition"
|
||||||
|
>
|
||||||
|
<td className="p-3 text-center font-mono text-blue-600 dark:text-blue-400 font-bold">
|
||||||
|
{entry.songNumber}
|
||||||
|
</td>
|
||||||
|
<td className="p-3 font-medium text-neutral-900 dark:text-white">
|
||||||
|
{entry.song.title}
|
||||||
|
</td>
|
||||||
|
<td className="p-3 text-right">
|
||||||
|
<Input.Button
|
||||||
|
onClick={() =>
|
||||||
|
onSelectSong?.(
|
||||||
|
entry.song,
|
||||||
|
{ id: book.id, title: book.title },
|
||||||
|
String(entry.songNumber)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
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}
|
||||||
|
</Input.Button>
|
||||||
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
))}
|
||||||
<tbody className="divide-y divide-neutral-200 dark:divide-neutral-800">
|
</tbody>
|
||||||
{searchResults.length === 0 ? (
|
))
|
||||||
<tr>
|
)}
|
||||||
<td colSpan={4} className="p-8 text-center text-neutral-400 dark:text-neutral-500">Keine Lieder gefunden</td>
|
</table>
|
||||||
</tr>
|
</div>
|
||||||
) : (
|
|
||||||
searchResults.map((song) => {
|
|
||||||
const songBook = songbooks.find((book) => book.id === song.songbook_id);
|
|
||||||
return (
|
|
||||||
<tr key={`${song.songbook_id}-${song.song_id}`} className="hover:bg-neutral-100/60 dark:hover:bg-neutral-900/40 transition">
|
|
||||||
<td className="p-3 font-medium text-neutral-900 dark:text-white max-w-[180px] sm:max-w-none truncate">
|
|
||||||
{song.title}
|
|
||||||
<span className="block text-xs text-neutral-500 dark:text-neutral-400 sm:hidden mt-0.5">
|
|
||||||
{songBook ? songBook.name : "Unbekannt"}
|
|
||||||
</span>
|
|
||||||
</td>
|
|
||||||
<td className="p-3 text-neutral-700 dark:text-neutral-300 hidden sm:table-cell">
|
|
||||||
{songBook ? songBook.name : "Unbekannt"}
|
|
||||||
</td>
|
|
||||||
<td className="p-3 text-center font-mono text-blue-600 dark:text-blue-400 font-bold">
|
|
||||||
{song.song_number}
|
|
||||||
</td>
|
|
||||||
<td className="p-3 text-right">
|
|
||||||
<Input.Button
|
|
||||||
onClick={() => 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}
|
|
||||||
</Input.Button>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
);
|
|
||||||
})
|
|
||||||
)}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
</Card>
|
</Card>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,16 @@ export interface SongDetailed {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SongBookDetailed {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
language: string;
|
||||||
|
songs?: {
|
||||||
|
songNumber: string;
|
||||||
|
song: Song;
|
||||||
|
}[]
|
||||||
|
}
|
||||||
|
|
||||||
export type APIResponse<T> =
|
export type APIResponse<T> =
|
||||||
| { success: true; status: number; data: T }
|
| { success: true; status: number; data: T }
|
||||||
| { success: false; status: number; error: string };
|
| { success: false; status: number; error: string };
|
||||||
|
|||||||
Reference in new issue
Block a user