diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71e..9e6b553b5 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - + - - + My Library + + @@ -27,23 +24,27 @@

Library

Add new book + +
Library id="pages" name="pages" required + min="1" + step="1" /> - - +
+ + +
+ +
- @@ -80,17 +76,9 @@

Library

- - - - - - - - - +
- + diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d3..d1c6bb2ce 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,48 +1,62 @@ -let myLibrary = []; +const myLibrary = []; + +let titleInput; +let authorInput; +let pagesInput; +let checkInput; +let submitBtn; +let bookList; +let messageEl; + +function init() { + // Get elements from HTML + titleInput = document.getElementById("title"); + authorInput = document.getElementById("author"); + pagesInput = document.getElementById("pages"); + checkInput = document.getElementById("check"); + submitBtn = document.getElementById("submitBtn"); + bookList = document.getElementById("bookList"); + messageEl = document.getElementById("message"); + + // Listen for Submit button click + submitBtn.addEventListener("click", submitBook); + + // Add initial books + const book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); + const book2 = new Book( + "The Old Man and the Sea", + "Ernest Hemingway", + 127, + true + ); + + myLibrary.push(book1); + myLibrary.push(book2); -window.addEventListener("load", function (e) { - 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(); - } } -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; - } else { - let book = new Book(title.value, title.value, pages.value, check.checked); - library.push(book); - render(); +init(); + +// Check the right input from forms and if it's okay, +// add the new book object to the array and render +function submitBook() { + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + + if (!title || !author || !Number.isInteger(pages) || pages < 1) { + showMessage("Please enter valid book information.", "danger"); + return; } + + const book = new Book(title, author, pages, checkInput.checked); + + myLibrary.push(book); + render(); } +// Book constructor function Book(title, author, pages, check) { this.title = title; this.author = author; @@ -51,53 +65,64 @@ 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; + // Delete old table rows + bookList.replaceChildren(); + + // Insert updated rows and cells + 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 = bookList.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; + + // Add and wait for action for read/unread button + const changeButton = document.createElement("button"); + changeButton.className = "btn btn-success"; + + const readStatus = myLibrary[i].check ? "Yes" : "No"; + changeButton.textContent = readStatus; + + wasReadCell.appendChild(changeButton); + + changeButton.addEventListener("click", function () { 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}`); + // Add delete button to every row + const deleteButton = document.createElement("button"); + deleteButton.className = "btn btn-warning"; + deleteButton.textContent = "Delete"; + + deleteCell.appendChild(deleteButton); + + deleteButton.addEventListener("click", function () { + const deletedTitle = myLibrary[i].title; + myLibrary.splice(i, 1); render(); + + showMessage(`You've deleted title: ${deletedTitle}`, "success"); }); } } + +// Display a message without using alert() +function showMessage(message, type) { + messageEl.textContent = message; + messageEl.className = `alert alert-${type}`; + messageEl.hidden = false; + + setTimeout(function () { + messageEl.hidden = true; + }, 3000); +} diff --git a/debugging/book-library/style.css b/debugging/book-library/style.css index 302950cb8..44b114fa8 100644 --- a/debugging/book-library/style.css +++ b/debugging/book-library/style.css @@ -1,7 +1,7 @@ .form-group { width: 400px; height: 300px; - align-self: left; + align-self: flex-start; padding-left: 20px; } @@ -10,8 +10,8 @@ } .form-check-label { - padding-left: 20px; - margin: 5px 0px 5px 0px; + padding-left: 5px; + margin: 0px 0px 10px 0px; } button.btn-info {