-
-
Notifications
You must be signed in to change notification settings - Fork 272
Glasgow | 26-ITP-May | Francesco Romano Monda | Sprint 2 | Book Library #553
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,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"); | ||
|
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. When I clicked the Submit button without any input, I couldn't see any error message.
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. I found the issue: I had not included an element with id="message" in my HTML. I added the missing element, and the validation error message now displays correctly when the Submit button is clicked without any input.
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 was the bug I wanted you to find. You fixed it! |
||
| 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); | ||
| } | ||
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.
Even with these constraints, currently the browser would still allow a user to submit value like
12.345or-100. Could you find out why and then fix the issue?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.
I tested this again and found that decimal and negative page values are now rejected and the error message is displayed. It is possible that this issue occurred before I added the error message.
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.
The error message you mentioned is generated by your JS code, and not by the browser.
The browser will only check the input against those constraints when a user submits a form. Currently those input elements are not inside a form, and that's why the browser does not enforce the constraints.