diff --git a/frontend/files/index.html b/frontend/files/index.html index 2aaa99b..4496a2a 100644 --- a/frontend/files/index.html +++ b/frontend/files/index.html @@ -23,33 +23,29 @@ function init() { fetch("/api") .then((response) => response.json()) - .then(showInfo); - } - - function showInfo(books) { - books.forEach((book) => { - book.sortTitle = titleCleaner(book.title); - if (!book["isbn-10"] && book["isbn-13"]) { - book["isbn-10"] = ISBNfromEAN(book["isbn-13"]); - } - if (!book.coverurl && book["isbn-10"]) { - book.coverurl = amazonCoverUrl(book["isbn-10"]); - } - }); - - $("#search") - .unbind("input") - .on("input", (e) => { - search(books, e.target.value); - }); - - $("#reloadLink") - .unbind("click") - .on("click", () => { - init(); - }); - - renderTable(books); + .then((books) => { + // prepare response + books.forEach((book) => { + book.sortTitle = titleCleaner(book.title); + if (!book["isbn-10"] && book["isbn-13"]) { + book["isbn-10"] = ISBNfromEAN(book["isbn-13"]); + } + if (!book.coverurl && book["isbn-10"]) { + book.coverurl = + `https://images-na.ssl-images-amazon.com/images/P/` + + book["isbn-10"] + + `.01.LZZ.jpg`; + } + }); + return books; + }) + .then((books) => { + document.getElementById("search").addEventListener("input", (e) => { + search(books, e.target.value); + }); + return books; + }) + .then(renderTable); } function search(books, searchBy) { @@ -88,15 +84,19 @@ if (sortState.sortOrder === "desc") { books.reverse(); } - books.forEach((e, i) => (e.rowNumber = i)); // re-key for new sort + books.forEach((e, i) => (e.rowNumber = i)); // re-key - $("#books").html(TableTemplate(books)); + // rendering + document.getElementById("books").innerHTML = TableTemplate(books); $("#books tbody tr") .not(":first") // ignore the headers - .on("click", function () { - updateCurrentBook(books[$(this)[0].id]); + .on("click", (e) => { + document.getElementById("current").innerHTML = BookTemplate( + books[e.currentTarget.id] + ); }); - $("#books tbody tr th[data-sort-by]").on("click", function () { + $("#books tbody tr th[data-sort-by]").on("click", function (e) { + console.log(e); renderTable(books, $(this).data("sortBy")); // only add callback when there's a sortBy attribute }); $("#books tbody tr th[data-sort-by=" + sortState.sortBy + "]").addClass( @@ -104,10 +104,6 @@ ); } - function updateCurrentBook(book) { - $("#current").html(BookTemplate(book)); - } - function titleCleaner(title) { return title .replace('"', "") @@ -124,10 +120,6 @@ .replace(" ", ""); } - function amazonCoverUrl(ISBN) { - return `https://images-na.ssl-images-amazon.com/images/P/${ISBN}.01.LZZ.jpg`; - } - function ISBNfromEAN(EAN) { ISBN = EAN.slice(3, 12); var checkdigit = @@ -209,7 +201,9 @@ }, "")} `; } - window.addEventListener("DOMContentLoaded", init); + window.addEventListener("DOMContentLoaded", () => { + init(); + }); @@ -219,7 +213,6 @@ git - reload