diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..eb6a4411c 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,11 +1,9 @@ - - Virtual Library + + @@ -28,10 +26,11 @@

Library

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

Library

- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..784f7e181 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,97 @@ -let myLibrary = []; +const myLibrary = []; window.addEventListener("load", function (e) { populateStorage(); render(); + + // Connect the form submit event listener + const formElement = document.getElementById("book-form"); + if (formElement) { + formElement.addEventListener("submit", submit); + } }); 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(); + 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); } } -const title = document.getElementById("title"); -const author = document.getElementById("author"); -const pages = document.getElementById("pages"); -const check = document.getElementById("check"); +const formElement = document.getElementById("book-form"); +const titleElement = document.getElementById("title"); +const authorElement = document.getElementById("author"); +const pagesElement = document.getElementById("pages"); +const checkElement = document.getElementById("check"); + + + + + +// Helper function to display messages to the user in the UI +function displayStatusMessage(message, isError = false) { + const statusContainer = document.getElementById("status-message") || createStatusContainer(); + statusContainer.textContent = message; + statusContainer.style.color = isError ? "red" : "black"; +} + +function createStatusContainer() { + const container = document.createElement("div"); + container.id = "status-message"; + container.style.padding = "1rem"; + container.style.fontSize = "1.2rem"; + container.style.textAlign = "center"; + + // Prepend to body or main container + document.body.prepend(container); + return container; +} + //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() { +function submit(event) { + // Prevent form submission from reloading the page + if (event) { + event.preventDefault(); + } + + const titleValue = titleElement.value.trim(); + const authorValue = authorElement.value.trim(); + const pagesValue = pagesElement.value.trim(); + + displayStatusMessage(""); + if ( - title.value == null || - title.value == "" || - pages.value == null || - pages.value == "" + titleValue === "" || + authorValue === "" || + pagesValue === "" ) { - alert("Please fill all fields!"); - return false; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); + displayStatusMessage("Please fill all fields!", true); + return; + } + + +// Convert to Number + const pagesNum = Number(pagesValue); + + // Check if it's a valid positive integer + if (!Number.isInteger(pagesNum) || pagesNum <= 0) { + displayStatusMessage("Pages must be a positive whole number!", true); + return; } + + let book = new Book(titleValue, authorValue, pagesNum, checkElement.checked); + + myLibrary.push(book); + // Clear inputs on successful submit + formElement.reset(); + + displayStatusMessage(`Added "${titleValue}" to your library!`, false); + render(); + } function Book(title, author, pages, check) { @@ -52,52 +103,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 tbodyElement = document.getElementById("tableBody"); + + // Clear existing rows inside tbody only + tbodyElement.innerHTML = ""; + + + for (let i = 0; i < myLibrary.length; i++) { + let row = tbodyElement.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; + + titleCell.textContent = myLibrary[i].title; + authorCell.textContent = myLibrary[i].author; + pagesCell.textContent = 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 () { + + + changeBut.innerText = myLibrary[i].check ? "Yes" : "No"; + changeBut.addEventListener("click", () => { myLibrary[i].check = !myLibrary[i].check; render(); }); + wasReadCell.appendChild(changeBut); //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}`); + delButton.className = "btn btn-warning"; + delButton.innerText = "Delete"; + delButton.addEventListener("click", () => { + displayStatusMessage(`You've deleted title: ${myLibrary[i].title}`,false); myLibrary.splice(i, 1); render(); }); + deleteCell.appendChild(delButton); } }