|
1 | | -let myLibrary = []; |
2 | | - |
3 | | -window.addEventListener("load", function () { |
4 | | - populateStorage(); |
5 | | - render(); |
6 | | -}); |
7 | | - |
8 | | -function populateStorage() { |
9 | | - if (myLibrary.length == 0) { |
10 | | - let book1 = new Book("Robinson Crusoe", "Daniel Defoe", 252, true); |
11 | | - let book2 = new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true); |
12 | | - myLibrary.push(book1); |
13 | | - myLibrary.push(book2); |
| 1 | +class Book { |
| 2 | + constructor(title, author, pages, read) { |
| 3 | + this.title = title; |
| 4 | + this.author = author; |
| 5 | + this.pages = pages; |
| 6 | + this.read = read; |
14 | 7 | } |
15 | 8 | } |
16 | 9 |
|
17 | | -const title = document.getElementById("title"); |
18 | | -const author = document.getElementById("author"); |
19 | | -const pages = document.getElementById("pages"); |
20 | | -const check = document.getElementById("check"); |
21 | 10 |
|
22 | | -function submit() { |
23 | | - if (title.value === "" || author.value === "" || pages.value === "") { |
24 | | - alert("Please fill all fields!"); |
| 11 | +const library = [ |
| 12 | + new Book("Robinson Crusoe", "Daniel Defoe", 252, true), |
| 13 | + new Book("The Old Man and the Sea", "Ernest Hemingway", 127, true), |
| 14 | +]; |
| 15 | + |
| 16 | + |
| 17 | +const bookForm = document.getElementById("bookForm"); |
| 18 | +const titleInput = document.getElementById("titleInput"); |
| 19 | +const authorInput = document.getElementById("authorInput"); |
| 20 | +const pagesInput = document.getElementById("pagesInput"); |
| 21 | +const readCheckbox = document.getElementById("readCheckbox"); |
| 22 | +const tableBody = document.querySelector("#displayTable tbody"); |
| 23 | + |
| 24 | + |
| 25 | +bookForm.addEventListener("submit", (event) => { |
| 26 | + event.preventDefault(); |
| 27 | + |
| 28 | + const title = titleInput.value.trim(); |
| 29 | + const author = authorInput.value.trim(); |
| 30 | + const pages = Number(pagesInput.value); |
| 31 | + const read = readCheckbox.checked; |
| 32 | + |
| 33 | + |
| 34 | + if (!title || !author || !Number.isInteger(pages) || pages <= 0) { |
| 35 | + alert("Please enter valid book details."); |
25 | 36 | return; |
26 | 37 | } |
27 | 38 |
|
28 | | - let book = new Book(title.value, author.value, pages.value, check.checked); |
29 | | - myLibrary.push(book); |
30 | 39 |
|
31 | | - // clear form |
32 | | - title.value = ""; |
33 | | - author.value = ""; |
34 | | - pages.value = ""; |
35 | | - check.checked = false; |
| 40 | + library.push(new Book(title, author, pages, read)); |
| 41 | + |
| 42 | + |
| 43 | + bookForm.reset(); |
| 44 | + |
36 | 45 |
|
37 | 46 | render(); |
38 | | -} |
| 47 | +}); |
39 | 48 |
|
40 | | -function Book(title, author, pages, read) { |
41 | | - this.title = title; |
42 | | - this.author = author; |
43 | | - this.pages = pages; |
44 | | - this.read = read; // renamed from "check" (more readable) |
45 | | -} |
46 | 49 |
|
47 | 50 | function render() { |
48 | | - let table = document.getElementById("display"); |
49 | | - |
50 | | - // delete old rows |
51 | | - while (table.rows.length > 1) { |
52 | | - table.deleteRow(1); |
53 | | - } |
54 | 51 |
|
55 | | - // insert updated rows |
56 | | - for (let i = 0; i < myLibrary.length; i++) { |
57 | | - let row = table.insertRow(1); |
| 52 | + tableBody.innerHTML = ""; |
| 53 | + |
| 54 | + library.forEach((book, index) => { |
| 55 | + const row = document.createElement("tr"); |
| 56 | + |
| 57 | + row.innerHTML = ` |
| 58 | + <td>${book.title}</td> |
| 59 | + <td>${book.author}</td> |
| 60 | + <td>${book.pages}</td> |
| 61 | + <td> |
| 62 | + <button |
| 63 | + class="btn btn-sm ${book.read ? "btn-success" : "btn-secondary"}" |
| 64 | + data-index="${index}" |
| 65 | + data-action="toggle" |
| 66 | + > |
| 67 | + ${book.read ? "Yes" : "No"} |
| 68 | + </button> |
| 69 | + </td> |
| 70 | + <td> |
| 71 | + <button |
| 72 | + class="btn btn-sm btn-danger" |
| 73 | + data-index="${index}" |
| 74 | + data-action="delete" |
| 75 | + > |
| 76 | + Delete |
| 77 | + </button> |
| 78 | + </td> |
| 79 | + `; |
| 80 | + |
| 81 | + tableBody.appendChild(row); |
| 82 | + }); |
| 83 | +} |
58 | 84 |
|
59 | | - row.insertCell(0).innerHTML = myLibrary[i].title; |
60 | | - row.insertCell(1).innerHTML = myLibrary[i].author; |
61 | | - row.insertCell(2).innerHTML = myLibrary[i].pages; |
62 | 85 |
|
63 | | - // read status toggle button |
64 | | - let readCell = row.insertCell(3); |
65 | | - let readButton = document.createElement("button"); |
66 | | - readButton.className = "btn btn-success"; |
67 | | - readButton.innerText = myLibrary[i].read ? "Yes" : "No"; |
| 86 | +tableBody.addEventListener("click", (event) => { |
| 87 | + const button = event.target.closest("button"); |
| 88 | + if (!button) return; |
68 | 89 |
|
69 | | - readButton.addEventListener("click", function () { |
70 | | - myLibrary[i].read = !myLibrary[i].read; |
71 | | - render(); |
72 | | - }); |
| 90 | + const index = Number(button.dataset.index); |
| 91 | + const action = button.dataset.action; |
73 | 92 |
|
74 | | - readCell.appendChild(readButton); |
75 | 93 |
|
76 | | - // delete button |
77 | | - let delCell = row.insertCell(4); |
78 | | - let delButton = document.createElement("button"); |
79 | | - delButton.className = "btn btn-warning"; |
80 | | - delButton.innerText = "Delete"; |
| 94 | + if (action === "toggle") { |
| 95 | + library[index].read = !library[index].read; |
| 96 | + render(); |
| 97 | + } |
81 | 98 |
|
82 | | - delButton.addEventListener("click", function () { |
83 | | - alert(`You've deleted: ${myLibrary[i].title}`); |
84 | | - myLibrary.splice(i, 1); |
85 | | - render(); |
86 | | - }); |
87 | 99 |
|
88 | | - delCell.appendChild(delButton); |
| 100 | + if (action === "delete") { |
| 101 | + const removedTitle = library[index].title; |
| 102 | + library.splice(index, 1); |
| 103 | + render(); |
| 104 | + alert(`Deleted: ${removedTitle}`); |
89 | 105 | } |
90 | | -} |
| 106 | +}); |
| 107 | + |
| 108 | + |
| 109 | +render(); |
0 commit comments