-
-
Notifications
You must be signed in to change notification settings - Fork 263
London | 26-ITP-May | Damilola Odumosu | Sprint 2 | Book Library #533
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 all commits
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,103 +1,101 @@ | ||
| let myLibrary = []; | ||
|
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. Can we declare |
||
|
|
||
| window.addEventListener("load", function (e) { | ||
| window.addEventListener("load", function () { | ||
| populateStorage(); | ||
| render(); | ||
| }); | ||
|
|
||
| function Book(title, author, pages, check) { | ||
| this.title = title; | ||
| this.author = author; | ||
| this.pages = pages; | ||
| this.check = check; | ||
| } | ||
| function populateStorage() { | ||
| if (myLibrary.length == 0) { | ||
| let book1 = new Book("Robison Crusoe", "Daniel Defoe", "252", true); | ||
| 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", | ||
| 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"); | ||
| const titleInput = document.getElementById("title"); | ||
| const authorInput = document.getElementById("author"); | ||
| const pagesInput = document.getElementById("pages"); | ||
| const checkInput = document.getElementById("check"); | ||
|
Comment on lines
+28
to
+31
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. Common practice is to declare all shared variables/constants at the beginning of the file before function definitions. |
||
|
|
||
| //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 == "" | ||
| titleInput.value.trim() === "" || | ||
| pagesInput.value.trim() === "" || | ||
| authorInput.value.trim() === "" | ||
| ) { | ||
| alert("Please fill all fields!"); | ||
| return false; | ||
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| let book = new Book( | ||
| titleInput.value, | ||
| authorInput.value, | ||
| Number(pagesInput.value), | ||
| checkInput.checked | ||
| ); | ||
|
Comment on lines
+42
to
+47
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.
|
||
| myLibrary.push(book); | ||
| render(); | ||
| } | ||
| } | ||
|
|
||
| function Book(title, author, pages, check) { | ||
| this.title = title; | ||
| this.author = author; | ||
| this.pages = pages; | ||
| this.check = 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); | ||
| } | ||
| tableBody.innerHTML = ""; | ||
| //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; | ||
| myLibrary.forEach((book, i) => { | ||
| createBookRow(book, i); | ||
| }); | ||
| } | ||
|
|
||
| const tableBody = document.querySelector("tbody"); | ||
|
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. Why not keep this variable declaration with the other variable declarations at the beginning of the file? |
||
| let readStatus; | ||
|
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. Does |
||
|
|
||
| //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; | ||
| function createBookRow(book, index) { | ||
| const changeBtn = document.createElement("button"); | ||
| const deleteBtn = document.createElement("button"); | ||
|
|
||
| changeBut.addEventListener("click", function () { | ||
| myLibrary[i].check = !myLibrary[i].check; | ||
| render(); | ||
| }); | ||
| let row = tableBody.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); | ||
|
|
||
| //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(); | ||
| }); | ||
| titleCell.textContent = book.title; | ||
| authorCell.textContent = book.author; | ||
| pagesCell.textContent = book.pages; | ||
| wasReadCell.appendChild(changeBtn); | ||
| deleteCell.appendChild(deleteBtn); | ||
| deleteBtn.textContent = "Delete"; | ||
| changeBtn.className = "btn btn-success"; | ||
|
|
||
| deleteBtn.className = "btn btn-warning"; | ||
|
|
||
| if (!book.check) { | ||
| readStatus = "No"; | ||
| } else { | ||
| readStatus = "Yes"; | ||
| } | ||
| changeBtn.innerText = readStatus; | ||
|
Comment on lines
+86
to
+91
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. This could be a good opportunity to practice using the |
||
| changeBtn.addEventListener("click", function () { | ||
| book.check = !book.check; | ||
| render(); | ||
| }); | ||
| deleteBtn.addEventListener("click", function () { | ||
| alert(`You've deleted title: ${book.title}`); | ||
| myLibrary.splice(index, 1); | ||
| render(); | ||
|
Comment on lines
+97
to
+99
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. The alert message is shown before the book is actually deleted; the deletion only occurs after the alert dialog is dismissed. This introduces a risk that the operation may not complete (e.g., if the user closes the browser before dismissing the alert). In general, it’s better to display a confirmation message only after the associated operation has successfully completed. In addition, Suggestion: Research for approaches that allows the UI to update before displaying the |
||
| }); | ||
| } | ||
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.
Could you find out why the browser does not check if the value in this input field is a positive whole number (even if when the constraints
min="1"is specified)?