diff --git a/debugging/book-library/index.html b/debugging/book-library/index.html index 23acfa71..ebf1e4f8 100644 --- a/debugging/book-library/index.html +++ b/debugging/book-library/index.html @@ -1,12 +1,9 @@ - + - - + Book Library + + @@ -19,7 +16,7 @@
-

Library

+

My Book Library

Add books to your virtual library

@@ -31,7 +28,7 @@

Library

Library /> Library
- - - - - - - - - - - - - - - - - - - -
TitleAuthorNumber of PagesRead
+
+ + + + + + + + + + + +
TitleAuthorNumber of PagesRead
+
diff --git a/debugging/book-library/script.js b/debugging/book-library/script.js index 75ce6c1d..91c67c8b 100644 --- a/debugging/book-library/script.js +++ b/debugging/book-library/script.js @@ -1,46 +1,70 @@ +const titleInput = document.getElementById("title"); +const authorInput = document.getElementById("author"); +const pagesInput = document.getElementById("pages"); +const readCheckbox = document.getElementById("check"); +const table = document.getElementById("display"); +const submitBtn = document.getElementById("submit-book-btn"); + let myLibrary = []; -window.addEventListener("load", function (e) { +window.addEventListener("load", () => { populateStorage(); render(); }); +submitBtn.addEventListener("click", addBook); + +// Initialises data and seeds default books if storage is empty 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 + const storedLibrary = localStorage.getItem("myLibrary"); + + if (storedLibrary) { + const rawData = JSON.parse(storedLibrary); + // Rehydrates plain data back into Book objects + myLibrary = rawData.map( + (data) => + new Book(data.title, data.author, Number(data.pages), data.check) ); - myLibrary.push(book1); - myLibrary.push(book2); - render(); + } else { + // Seeds data for new users + myLibrary = [ + new Book("The Hobbit", "J.R.R. Tolkien", 295, false), + new Book("1984", "George Orwell", 328, true), + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, false), + ]; + saveStorage(); } } -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(); +function saveStorage() { + localStorage.setItem("myLibrary", JSON.stringify(myLibrary)); +} + +// Trims input values and prevents empty submissions +function addBook(e) { + if (e) e.preventDefault(); + + // Trims input whitespace to sanitise entries + const title = titleInput.value.trim(); + const author = authorInput.value.trim(); + const pages = Number(pagesInput.value); + + // Validates input: checks for empty strings, non-numbers, or negative values + if (!title || !author || isNaN(pages) || pages <= 0) { + alert("Please enter valid book details. Pages must be a positive number."); + return; } + const book = new Book(title, author, pages, readCheckbox.checked); + myLibrary.push(book); + saveStorage(); + render(); + + // Resets the entry form after adding a book + titleInput.value = ""; + authorInput.value = ""; + pagesInput.value = ""; + readCheckbox.checked = false; } function Book(title, author, pages, check) { @@ -51,53 +75,50 @@ 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 () { - myLibrary[i].check = !myLibrary[i].check; + const tbody = table.querySelector("tbody"); + tbody.innerHTML = ""; + + myLibrary.forEach((book, i) => { + const row = tbody.insertRow(-1); + + // Inserts new cells into the table row + 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); + + // Prevents XSS by inserting data as textContent + titleCell.textContent = book.title; + authorCell.textContent = book.author; + pagesCell.textContent = book.pages; + + // Toggles the read status with a ternary operator + const readBtn = document.createElement("button"); + readBtn.className = book.check + ? "btn btn-success" + : "btn btn-outline-secondary"; + readBtn.textContent = book.check ? "Yes" : "No"; + wasReadCell.appendChild(readBtn); + + readBtn.addEventListener("click", () => { + book.check = !book.check; + saveStorage(); 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}`); + // Deletes the book and notifies the user + const deleteBtn = document.createElement("button"); + deleteBtn.className = "btn btn-danger btn-sm"; + deleteBtn.textContent = "Delete"; + deleteCell.appendChild(deleteBtn); + + deleteBtn.addEventListener("click", () => { + const deletedTitle = book.title; myLibrary.splice(i, 1); + saveStorage(); render(); + alert(`Success: "${deletedTitle}" has been removed.`); }); - } + }); }