-
-
Notifications
You must be signed in to change notification settings - Fork 159
London | 25-ITP-Sep | Adnaan Abo | Sprint 2 | book library project #344
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 1 commit
450911b
ac6bc70
ea76889
eea5314
aadf229
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,89 @@ | ||
| let myLibrary = []; | ||
|
|
||
| window.addEventListener("load", function (e) { | ||
| window.addEventListener("load", function () { | ||
| 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(); | ||
| if (myLibrary.length === 0) { | ||
| myLibrary.push(new Book("Robinson Crusoe", "Daniel Defoe", 252, true)); | ||
| myLibrary.push(new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true)); | ||
| } | ||
| } | ||
|
|
||
| const title = document.getElementById("title"); | ||
| const bookTitle = document.getElementById("book-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() { | ||
| // renamed so we don’t conflict with form.submit() | ||
| function addBook() { | ||
| if ( | ||
| title.value == null || | ||
| title.value == "" || | ||
| pages.value == null || | ||
| pages.value == "" | ||
| !bookTitle.value.trim() || | ||
| !author.value.trim() || | ||
| !pages.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); | ||
| render(); | ||
| return; | ||
| } | ||
|
|
||
| const book = new Book( | ||
| bookTitle.value.trim(), | ||
| author.value.trim(), | ||
| pages.value.trim(), | ||
|
||
| check.checked | ||
| ); | ||
|
|
||
| myLibrary.push(book); | ||
| render(); | ||
| } | ||
|
|
||
| function Book(title, author, pages, check) { | ||
| this.title = title; | ||
| function Book(bookTitle, author, pages, check) { | ||
| this.bookTitle = bookTitle; | ||
| 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); | ||
| const table = document.getElementById("display"); | ||
|
|
||
| // remove all rows except the header | ||
| while (table.rows.length > 1) { | ||
| table.deleteRow(1); | ||
| } | ||
|
||
| //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; | ||
| myLibrary.forEach((book, i) => { | ||
| const row = table.insertRow(1); | ||
|
|
||
| row.insertCell(0).textContent = book.bookTitle; | ||
| row.insertCell(1).textContent = book.author; | ||
| row.insertCell(2).textContent = book.pages; | ||
|
|
||
| // read/unread toggle | ||
| const wasReadCell = row.insertCell(3); | ||
| const changeBut = document.createElement("button"); | ||
| changeBut.className = "btn btn-success"; | ||
| changeBut.textContent = book.check ? "Yes" : "No"; | ||
| 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; | ||
| changeBut.addEventListener("click", () => { | ||
| 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); | ||
| // delete button | ||
| const deleteCell = row.insertCell(4); | ||
|
||
| const delBut = document.createElement("button"); | ||
| delBut.className = "btn btn-warning"; | ||
| delBut.innerHTML = "Delete"; | ||
| delBut.addEventListener("clicks", function () { | ||
| alert(`You've deleted title: ${myLibrary[i].title}`); | ||
| delBut.textContent = "Delete"; | ||
| deleteCell.appendChild(delBut); | ||
|
|
||
| delBut.addEventListener("click", () => { | ||
| alert(`You've deleted: ${book.bookTitle}`); | ||
| myLibrary.splice(i, 1); | ||
| render(); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,9 @@ | |
| button.btn-info { | ||
| margin: 20px; | ||
| } | ||
|
|
||
| @media (min-width: 576px) { | ||
| .jumbotron { | ||
| padding: 2rem 32px 2rem; | ||
| } | ||
| } | ||
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.
It's a better practice to "attach event listener via JS" than "mixing JS code in HTML".
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.
added event listener via JS.