-
-
Notifications
You must be signed in to change notification settings - Fork 162
WM | ITP-MAY-25 | NAHOM MESFIN | Sprint 2 | Book Library #315
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
Changes from 16 commits
6d1db61
74a65bb
87c782a
13940df
87b5187
6b150ac
5b00729
6fc08fe
7ec7ebf
ee53951
5045390
f45f0e4
76bf57c
4476c43
714badb
f29e2c0
6547b32
cff73c9
85d011f
43852c5
e3d4f0e
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,22 +1,15 @@ | ||
| 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) { | ||
| 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, book2); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -25,22 +18,31 @@ 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 == "" | ||
| ) { | ||
| const titleValue = title.value.trim(); | ||
| const authorValue = author.value.trim(); | ||
| const pagesValue = pages.value.trim(); | ||
|
|
||
| if (!titleValue || !authorValue || !pagesValue) { | ||
|
||
| alert("Please fill all fields!"); | ||
| return false; | ||
| } else { | ||
| let book = new Book(title.value, title.value, pages.value, check.checked); | ||
| library.push(book); | ||
| render(); | ||
| } | ||
|
|
||
| // Prevent numbers in Author field | ||
| if (/\d/.test(authorValue)) { | ||
| alert("Author name cannot contain numbers!"); | ||
| return false; | ||
| } | ||
|
|
||
| let book = new Book(titleValue, authorValue, Number(pagesValue), check.checked); | ||
| myLibrary.push(book); | ||
| render(); | ||
|
|
||
| // Reset form | ||
| title.value = ""; | ||
| author.value = ""; | ||
| pages.value = ""; | ||
| check.checked = false; | ||
|
Comment on lines
+54
to
+57
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. We could also use |
||
| } | ||
|
|
||
| function Book(title, author, pages, check) { | ||
|
|
@@ -51,53 +53,47 @@ 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 () { | ||
| const table = document.getElementById("display"); | ||
| const tbody = table.querySelector("tbody"); | ||
|
|
||
| // Clear all existing rows | ||
| tbody.innerHTML = ""; | ||
|
|
||
| myLibrary.forEach((book, i) => { | ||
| const row = tbody.insertRow(); | ||
|
|
||
| row.insertCell(0).textContent = book.title; | ||
| row.insertCell(1).textContent = book.author; | ||
| row.insertCell(2).textContent = book.pages; | ||
|
|
||
| // Read/unread button | ||
| const wasReadCell = row.insertCell(3); | ||
| const readToggleButton = document.createElement("button"); | ||
| readToggleButton.className = "btn btn-success"; | ||
| readToggleButton.textContent = book.check ? "Yes" : "No"; | ||
| wasReadCell.appendChild(readToggleButton); | ||
|
|
||
| readToggleButton.addEventListener("click", () => { | ||
| 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}`); | ||
| myLibrary.splice(i, 1); | ||
| render(); | ||
| // Delete button | ||
| const deleteCell = row.insertCell(4); | ||
|
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 you think of the pros and cons of these two approaches for creating cells within a row?
|
||
| const deleteButton = document.createElement("button"); | ||
| deleteButton.className = "btn btn-warning"; | ||
| deleteButton.textContent = "Delete"; | ||
| deleteCell.appendChild(deleteButton); | ||
|
|
||
| deleteButton.addEventListener("click", () => { | ||
| myLibrary.splice(i, 1); // delete immediately | ||
| render(); // update the table | ||
| alert(`Deleted "${book.title}" successfully.`); // optional confirmation | ||
|
||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| // Make header row toggle the form | ||
| document.querySelector(".thead-dark tr").addEventListener("click", function () { | ||
| $("#demo").collapse("toggle"); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,3 +17,7 @@ | |
| button.btn-info { | ||
| margin: 20px; | ||
| } | ||
|
|
||
| .clickable { | ||
| cursor: pointer; | ||
| } | ||
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.
Right now you need to manually validate the form. Do you know what change you need to make to the HTML and script to make the browser validate the form inputs automatically?
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 have removed the some of the manual validations i used earlier in the script and replaced it with an autotmatic error handler in my html for the inputs
pattern="[a-zA-Z\s.'-]+",required min="1"