diff --git a/web/src/bookshelf.ts b/web/src/bookshelf.ts index de5f416..7f1c1a1 100644 --- a/web/src/bookshelf.ts +++ b/web/src/bookshelf.ts @@ -1,113 +1,125 @@ function selectLibrary(libraryId: string): void { - localStorage.setItem('selectedLibrary', libraryId); + localStorage.setItem("selectedLibrary", libraryId); - document.querySelectorAll('.library-item').forEach(el => { - el.classList.remove('ring-2'); - el.classList.remove('ring-accent'); - }); + document.querySelectorAll(".library-item").forEach((el) => { + el.classList.remove("ring-2"); + el.classList.remove("ring-accent"); + }); - const selected = document.querySelector(`[data-library-id="${libraryId}"]`); - if (selected) { - selected.classList.add('ring-2'); - selected.classList.add('ring-accent'); - } + const selected = document.querySelector(`[data-library-id="${libraryId}"]`); + if (selected) { + selected.classList.add("ring-2"); + selected.classList.add("ring-accent"); + } - loadBookshelf(libraryId); + loadBookshelf(libraryId); } async function loadBookshelf(libraryId: string): Promise { - const token = localStorage.getItem('token'); - if (!token) return; + const token = localStorage.getItem("token"); + if (!token) return; - try { - const response = await fetch(`/api/libraries/${libraryId}/books`, { - headers: { 'Authorization': `Bearer ${token}` } - }); + try { + const response = await fetch(`/api/libraries/${libraryId}/books`, { + headers: { Authorization: `Bearer ${token}` }, + }); - if (response.ok) { - const data = await response.json(); - renderBooks(data.books || []); - } - } catch (error) { - console.error('Failed to load bookshelf:', error); + if (response.ok) { + const data = await response.json(); + renderBooks(data.books || []); } + } catch (error) { + console.error("Failed to load bookshelf:", error); + } } function renderBooks(books: unknown[]): void { - const container = document.getElementById('books-grid'); - if (!container) return; + const container = document.getElementById("books-grid"); + if (!container) return; - if (books.length === 0) { - container.innerHTML = '

No books in this library

'; - return; - } + if (books.length === 0) { + container.innerHTML = + '

No books in this library

'; + return; + } - container.innerHTML = books.map((book: any) => ` + container.innerHTML = books + .map( + (book: any) => `
- ${book.cover_image_path ? - `${book.title}` : - `
+ ${ + book.cover_image_path + ? `${book.title}` + : `
📖
` }

${book.title}

-

${book.author || 'Unknown Author'}

+

${book.author || "Unknown Author"}

- `).join(''); + `, + ) + .join(""); } function selectBook(bookId: string): void { - localStorage.setItem('selectedBook', bookId); - window.location.href = `/books/${bookId}`; + localStorage.setItem("selectedBook", bookId); + window.location.href = `/books/${bookId}`; } function changePage(page: number): void { - const libraryId = localStorage.getItem('selectedLibrary'); - if (!libraryId) return; + const libraryId = localStorage.getItem("selectedLibrary"); + if (!libraryId) return; - const offset = (page - 1) * 50; - loadBookshelfPaginated(libraryId, offset); + const offset = (page - 1) * 50; + loadBookshelfPaginated(libraryId, offset); } -async function loadBookshelfPaginated(libraryId: string, offset: number): Promise { - const token = localStorage.getItem('token'); - if (!token) return; +async function loadBookshelfPaginated( + libraryId: string, + offset: number, +): Promise { + const token = localStorage.getItem("token"); + if (!token) return; - try { - const response = await fetch(`/api/libraries/${libraryId}/books?offset=${offset}&limit=50`, { - headers: { 'Authorization': `Bearer ${token}` } - }); + try { + const response = await fetch( + `/api/libraries/${libraryId}/books?offset=${offset}&limit=50`, + { + headers: { Authorization: `Bearer ${token}` }, + }, + ); - if (response.ok) { - const data = await response.json(); - renderBooks(data.books || []); - updatePagination(data.total, offset); - } - } catch (error) { - console.error('Failed to load bookshelf:', error); + if (response.ok) { + const data = await response.json(); + renderBooks(data.books || []); + updatePagination(data.total, offset); } + } catch (error) { + console.error("Failed to load bookshelf:", error); + } } function updatePagination(total: number, offset: number): void { - const container = document.getElementById('pagination'); - if (!container) return; + const container = document.getElementById("pagination"); + if (!container) return; - const limit = 50; - const currentPage = Math.floor(offset / limit) + 1; - const totalPages = Math.ceil(total / limit); + const limit = 50; + const currentPage = Math.floor(offset / limit) + 1; + const totalPages = Math.ceil(total / limit); - if (totalPages <= 1) { - container.innerHTML = ''; - return; - } + if (totalPages <= 1) { + container.innerHTML = ""; + return; + } - container.innerHTML = ` + container.innerHTML = `
- ${currentPage > 1 ? `` : ''} + ${currentPage > 1 ? `` : ""} Page ${currentPage} of ${totalPages} - ${currentPage < totalPages ? `` : ''} + ${currentPage < totalPages ? `` : ""}
`; }