diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..d705c4d1 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,11 +1,9 @@ - + My Library @@ -19,6 +17,7 @@
+

Library

Add books to your virtual library

@@ -28,10 +27,11 @@

Library

+
Library /> Library class="form-control" id="pages" name="pages" - required + required min="1" />
+
- + @@ -81,13 +82,7 @@

Library

- - - - - - - +
Title Author Number of Pages
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..17066062 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,22 +1,15 @@ let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", function () { populateStorage(); render(); }); function populateStorage() { - if (myLibrary.length == 0) { - let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); - let book2 = new Book( - "The Old Man and the Sea", - "Ernest Hemingway", - "127", - true - ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + if (myLibrary.length === 0) { + let book1 = new Book("Robison Crusoe", "Daniel Defoe", 252, true); + let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); + myLibrary.push(book1, book2); } } @@ -25,22 +18,43 @@ 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 +// Notification function +function showNotification(message) { + let notification = document.getElementById("notification"); + notification.textContent = message; + notification.style.display = "block"; + setTimeout(() => { + notification.style.display = "none"; + }, 3000); // 3 seconds +} + +// Listen for form submission +document.getElementById("bookForm").addEventListener("submit", function(event) { + event.preventDefault(); + submit(); +}); + function submit() { - if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" - ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + const titleValue = title.value.trim(); + const authorValue = author.value.trim(); + const pagesValue = pages.value.trim(); + + // Additional validation for pages + const pagesNumber = Number(pagesValue); + if (!Number.isInteger(pagesNumber) || pagesNumber <= 0) { + showNotification("Number of pages must be a positive whole number."); + return; } + + let book = new Book(titleValue, authorValue, pagesNumber, check.checked); + myLibrary.push(book); + render(); + + // Reset form + title.value = ""; + author.value = ""; + pages.value = ""; + check.checked = false; } function Book(title, author, pages, check) { @@ -51,53 +65,47 @@ 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; - 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 table = document.getElementById("display"); + const tbody = table.querySelector("tbody"); + + // Clear all existing rows + tbody.innerHTML = ""; + + myLibrary.forEach((book, i) => { + const row = tbody.insertRow(); + + row.insertCell(0).textContent = book.title; + row.insertCell(1).textContent = book.author; + row.insertCell(2).textContent = book.pages; + + // Read/unread button + const wasReadCell = row.insertCell(3); + const readToggleButton = document.createElement("button"); + readToggleButton.className = "btn btn-success"; + readToggleButton.textContent = book.check ? "Yes" : "No"; + wasReadCell.appendChild(readToggleButton); + + readToggleButton.addEventListener("click", () => { myLibrary[i].check = !myLibrary[i].check; render(); }); - //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); - render(); + // Delete button + const deleteCell = row.insertCell(4); + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + deleteCell.appendChild(deleteButton); + + deleteButton.addEventListener("click", () => { + myLibrary.splice(i, 1); // delete immediately + render(); // update the table + showNotification(`Deleted "${book.title}" successfully.`); }); - } + }); } + +// Make header row toggle the form +document.querySelector(".thead-dark tr").addEventListener("click", function () { + $("#demo").collapse("toggle"); +}); diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb..d26eb05b 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -17,3 +17,24 @@ button.btn-info { margin: 20px; } + +.clickable { + cursor: pointer; +} +.notification { + display: none; + position: fixed; + top: 230px; + left: 50%; + transform: translateX(-50%); + padding: 15px 20px; + background-color: #f8d7da; + color: #721c24; + border: 1px solid #f5c6cb; + border-radius: 5px; + text-align: center; + font-weight: bold; + font-size: 1.2rem; + z-index: 1000; +} +