diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html
index 23acfa71..991c408f 100644
--- a/debugging/book-library/index.html
+++ b/debugging/book-library/index.html
@@ -1,12 +1,9 @@
-
-
+
+
-
-
+ Book Library
+
+
@@ -28,31 +25,48 @@ Library
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js
index 75ce6c1d..c97c8fa7 100644
--- a/debugging/book-library/script.js
+++ b/debugging/book-library/script.js
@@ -1,48 +1,108 @@
-let myLibrary = [];
+const myLibrary = [];
+const bookForm = document.getElementById("book-form");
+const titleInput = document.getElementById("title");
+const authorInput = document.getElementById("author");
+const pagesInput = document.getElementById("pages");
+const checkInput = document.getElementById("check");
-window.addEventListener("load", function (e) {
+window.addEventListener("load", function () {
populateStorage();
render();
+
+ if (bookForm) {
+ bookForm.addEventListener("submit", function (event) {
+ event.preventDefault();
+ submit();
+ });
+ }
+
+ if (titleInput) {
+ titleInput.addEventListener("input", function () {
+ titleInput.classList.remove("is-invalid");
+ });
+ }
+
+ if (authorInput) {
+ authorInput.addEventListener("input", function () {
+ authorInput.classList.remove("is-invalid");
+ });
+ }
+
+ if (pagesInput) {
+ pagesInput.addEventListener("input", function () {
+ pagesInput.classList.remove("is-invalid");
+ });
+ }
});
function populateStorage() {
- if (myLibrary.length == 0) {
- let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true);
- let book2 = new Book(
+ if (myLibrary.length === 0) {
+ const book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true);
+ const book2 = new Book(
"The Old Man and the Sea",
"Ernest Hemingway",
- "127",
+ 127,
true
);
- myLibrary.push(book1);
- myLibrary.push(book2);
- render();
+ myLibrary.push(book1, book2);
}
}
-const title = document.getElementById("title");
-const author = document.getElementById("author");
-const pages = document.getElementById("pages");
-const check = document.getElementById("check");
-
-//check the right input from forms and if its ok -> add the new book (object in array)
-//via Book function and start render function
function submit() {
- if (
- title.value == null ||
- title.value == "" ||
- pages.value == null ||
- pages.value == ""
- ) {
- alert("Please fill all fields!");
- return false;
+ const titleValue = titleInput.value.trim();
+ const authorValue = authorInput.value.trim();
+ const pagesValue = pagesInput.value.trim();
+ const pagesNumber = Number(pagesValue);
+
+ const isTitleInvalid = !titleValue;
+ const isAuthorInvalid = !authorValue;
+ const isPagesInvalid =
+ !pagesValue ||
+ isNaN(pagesNumber) ||
+ pagesNumber <= 0 ||
+ !Number.isInteger(pagesNumber);
+
+ if (isTitleInvalid) {
+ titleInput.classList.add("is-invalid");
} else {
- let book = new Book(title.value, title.value, pages.value, check.checked);
- library.push(book);
- render();
+ titleInput.classList.remove("is-invalid");
}
+
+ if (isAuthorInvalid) {
+ authorInput.classList.add("is-invalid");
+ } else {
+ authorInput.classList.remove("is-invalid");
+ }
+
+ if (isPagesInvalid) {
+ pagesInput.classList.add("is-invalid");
+ } else {
+ pagesInput.classList.remove("is-invalid");
+ }
+
+ if (isTitleInvalid || isAuthorInvalid || isPagesInvalid) {
+ return false;
+ }
+
+ const book = new Book(
+ titleValue,
+ authorValue,
+ pagesNumber,
+ checkInput.checked
+ );
+ myLibrary.push(book);
+
+ // Reset form values
+ if (bookForm) {
+ bookForm.reset();
+ }
+
+ render();
}
+// Attach submit function to global window for inline onclick handler compatibility
+window.submit = submit;
+
function Book(title, author, pages, check) {
this.title = title;
this.author = author;
@@ -51,53 +111,59 @@ function Book(title, author, pages, check) {
}
function render() {
- let table = document.getElementById("display");
- let rowsNumber = table.rows.length;
- //delete old table
- for (let n = rowsNumber - 1; n > 0; n-- {
- table.deleteRow(n);
- }
- //insert updated row and cells
- let length = myLibrary.length;
+ const tableBody = document.querySelector("#display tbody");
+ tableBody.innerHTML = "";
+
+ const length = myLibrary.length;
for (let i = 0; i < length; i++) {
- let row = table.insertRow(1);
- let titleCell = row.insertCell(0);
- let authorCell = row.insertCell(1);
- let pagesCell = row.insertCell(2);
- let wasReadCell = row.insertCell(3);
- let deleteCell = row.insertCell(4);
- titleCell.innerHTML = myLibrary[i].title;
- authorCell.innerHTML = myLibrary[i].author;
- pagesCell.innerHTML = myLibrary[i].pages;
-
- //add and wait for action for read/unread button
- let changeBut = document.createElement("button");
- changeBut.id = i;
- changeBut.className = "btn btn-success";
- wasReadCell.appendChild(changeBut);
- let readStatus = "";
- if (myLibrary[i].check == false) {
- readStatus = "Yes";
- } else {
- readStatus = "No";
- }
- changeBut.innerText = readStatus;
-
- changeBut.addEventListener("click", function () {
+ const row = tableBody.insertRow();
+ const titleCell = row.insertCell(0);
+ const authorCell = row.insertCell(1);
+ const pagesCell = row.insertCell(2);
+ const wasReadCell = row.insertCell(3);
+ const deleteCell = row.insertCell(4);
+
+ titleCell.textContent = myLibrary[i].title;
+ authorCell.textContent = myLibrary[i].author;
+ pagesCell.textContent = myLibrary[i].pages;
+
+ // Read status toggle button
+ const toggleReadBtn = document.createElement("button");
+ toggleReadBtn.className = "btn btn-success";
+ toggleReadBtn.textContent = myLibrary[i].check ? "Yes" : "No";
+
+ toggleReadBtn.addEventListener("click", function () {
myLibrary[i].check = !myLibrary[i].check;
render();
});
+ wasReadCell.appendChild(toggleReadBtn);
- //add delete button to every row and render again
- let delButton = document.createElement("button");
- delBut.id = i + 5;
- deleteCell.appendChild(delBut);
- delBut.className = "btn btn-warning";
- delBut.innerHTML = "Delete";
- delBut.addEventListener("clicks", function () {
- alert(`You've deleted title: ${myLibrary[i].title}`);
- myLibrary.splice(i, 1);
+ // Delete button
+ const deleteBtn = document.createElement("button");
+ deleteBtn.className = "btn btn-warning";
+ deleteBtn.textContent = "Delete";
+
+ deleteBtn.addEventListener("click", function () {
+ const deletedBook = myLibrary.splice(i, 1)[0];
render();
+
+ // Non-blocking banner / status message update after state change
+ showNotification(`You've deleted title: ${deletedBook.title}`);
});
+ deleteCell.appendChild(deleteBtn);
+ }
+}
+
+function showNotification(message) {
+ let noteEl = document.getElementById("notification");
+ if (!noteEl) {
+ noteEl = document.createElement("div");
+ noteEl.id = "notification";
+ noteEl.className = "alert alert-info mt-3";
+ document.querySelector(".container, body").prepend(noteEl);
}
+ noteEl.textContent = message;
+ setTimeout(() => {
+ noteEl.remove();
+ }, 3000);
}
diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css
index 302950cb..9f3e1dad 100644
--- a/debugging/book-library/style.css
+++ b/debugging/book-library/style.css
@@ -1,6 +1,5 @@
-.form-group {
+#book-form {
width: 400px;
- height: 300px;
align-self: left;
padding-left: 20px;
}