-
-
Notifications
You must be signed in to change notification settings - Fork 161
London | 25-ITP-Sept | Samuel Tarawally | Sprint 2 | Book Library #364
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 5 commits
1582ca0
998034a
c83cbbb
c968435
8b302b9
c8e3a34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,53 @@ | ||
| 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); | ||
|
|
||
| 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 storedLibrary = localStorage.getItem("myLibrary"); | ||
| if (storedLibrary) { | ||
| myLibrary = JSON.parse(storedLibrary); | ||
| } | ||
| } | ||
|
|
||
| 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 == "" | ||
| ) { | ||
| 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 = pagesInput.value.trim(); | ||
|
|
||
| if (!title || !author || !pages) { | ||
| alert("Please fill all fields!"); | ||
| return false; | ||
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| render(); | ||
| return; | ||
| } | ||
| const book = new Book(title, author, pages, readCheckbox.checked); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On line 41, the value of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To address this I've made the following changes: 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);
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not all positive finite numbers are valid "number of pages" though. What other numbers should also be rejected?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. float would be a problem so i guess this would be better if (!title || !author || isNaN(pages) || pages <= 0 || !Number.isInteger(pages)) {}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this could also be shorten to if (!title || !author || !Number.isInteger(pages) || pages <= 0) {} |
||
| 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 +58,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.`); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't
myLibrarybe populated with some data for users who do not have any data in their browser's local storage?The objects in the restored array are not instances of Book. A a result,
myLibrarywill end up with two kinds of objects: Generic objects and Book objects (added on line 42). Even though this is not a problem for this simple app, it is a better practice to consistently keep the same type of data inmyLibrary.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also address the comment I left on the code on lines 18-21 in the previous review?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To populate data for users who do not have any, and to restore plain data objects back into book objects, the following code snippet has been applied:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well done.
Please note that if
pagesare consistently stored values of type number, we would not need the conversionNumber(data.pages).