-
-
Notifications
You must be signed in to change notification settings - Fork 125
ZA Cape Town | ITP-May-2025 | Dawud Vermeulen | Sprint 2 | Book-Library #308
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 4 commits
3c360ff
afae57d
07d1bd8
5699d2c
6980aeb
3dd9e9a
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,85 @@ | ||
// array | ||
let myLibrary = []; | ||
|
||
window.addEventListener("load", function (e) { | ||
populateStorage(); | ||
render(); | ||
}); | ||
|
||
// form elements | ||
const titleInput = document.getElementById("title"); | ||
const authorInput = document.getElementById("author"); | ||
const pagesInput = document.getElementById("pages"); | ||
const checkInput = document.getElementById("check"); | ||
// populate the library with some default books if it's 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 | ||
); | ||
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 == "" | ||
titleInput.value.trim == "" || // cant be null so removed all that | ||
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. There may be ways to improve your form validation here. Things to think about:
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. @LonMcGregor ah very DRY indeed. cool HTML5 tricks. have updated both files to handle the validation and submission a bit differently. |
||
authorInput.value.trim == "" || | ||
pagesInput.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 newBook = new Book(titleInput.value, authorInput.value, parseInt(pagesInput.value), checkInput.checked); | ||
myLibrary.push(newBook); | ||
//clear form after adding newBook | ||
titleInput.value = ''; | ||
authorInput.value = ''; | ||
pagesInput.value = ''; | ||
checkInput.value = false; | ||
render(); | ||
} | ||
} | ||
|
||
// book constructor | ||
function Book(title, author, pages, check) { | ||
this.title = title; | ||
this.author = author; | ||
this.pages = pages; | ||
this.check = check; | ||
} | ||
|
||
// renders lib data into HTML | ||
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; | ||
let tableBody = document.querySelector("#display tbody"); | ||
tableBody.innerHTML = ''; // get rid of all instead of one by one row deletion | ||
|
||
myLibrary.forEach((book, index) => { | ||
let row = tableBody.insertRow(); | ||
// post debate lets use textContent instead of innerHTML | ||
row.insertCell(0).textContent = book.title; | ||
row.insertCell(1).textContent = book.author; | ||
row.insertCell(2).textContent = book.pages; | ||
// 'read' status | ||
const wasReadCell = row.insertCell(3); | ||
const readStatusBtn = document.createElement("button"); | ||
readStatusBtn.className = "btn btn-success"; | ||
readStatusBtn.textContent = book.check ? "Yes" : "No"; | ||
wasReadCell.appendChild(readStatusBtn); | ||
|
||
changeBut.addEventListener("click", function () { | ||
myLibrary[i].check = !myLibrary[i].check; | ||
readStatusBtn.addEventListener("click", function () { | ||
book.check = !book.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}`); | ||
myLibrary.splice(i, 1); | ||
const deleteCell = row.insertCell(4); | ||
const deleteBtn = document.createElement("button"); | ||
deleteBtn.className = "btn btn-warning"; | ||
deleteBtn.innerHTML = "Delete"; | ||
deleteCell.appendChild(deleteBtn); | ||
deleteBtn.addEventListener("click", function () { | ||
const deletedTitle = myLibrary[index].title; | ||
myLibrary.splice(index, 1); | ||
render(); | ||
alert(`You've deleted title: ${deletedTitle}`); | ||
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. Think about how deleting a file works on your desktop. Is being notified after the deletion normally how this works? Is there a better interaction you could use than an 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. @LonMcGregor okay so this is bit higher grade. i think what you mean is that the users flow is interrupted when the alert pops up (clarify if im wrong kanala). based on this assumption and the fact that the delete works i have built a new function to display the notification and done away with the disruptive alert. The new one disappears after 3 secs. 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 is quite interrupting, yes, making it less "in your face" is one solution. The other is, if you really want something interrupting, to use something like It sounds like you've made good progress here. I can't see the files yet on github though - have you comitted them all? |
||
}); | ||
} | ||
}); | ||
} | ||
// moved to end to make sure all is defined before load | ||
window.addEventListener("load", function () { | ||
populateStorage(); | ||
render(); | ||
}); |
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.
There are some things missing from your HTML file that should be added. Hint: how people are going to interact with your webpage in a web browser?
There is also some invalid HTML. Can you spot these? Hint: have you used an HTML validator before? https://validator.w3.org/
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.
@LonMcGregor this was actually a lot. Really useful site. So have update the file, please review and let me know if more is required.