-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
104 lines (84 loc) · 2.87 KB
/
Copy pathapp.js
File metadata and controls
104 lines (84 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
"use strict";
const grid = document.querySelector(".grid");
const modal = document.querySelector(".modal");
let myLibrary = [];
function elementFromHTML(html) {
const template = document.createElement("template");
template.innerHTML = html.trim();
return template.content.firstElementChild;
}
function delegateEvent(type, selector, callback, parent = document) {
parent.addEventListener(type, (e) => {
if (e.target.closest(selector)) {
callback(e);
}
});
}
class Book {
constructor(title, author, pageQuantity, readStatus) {
this.title = title;
this.author = author;
this.pageQuantity = pageQuantity;
this.readStatus = readStatus;
this.uuid = crypto.randomUUID();
}
}
function addBookToLibrary(title, author, pageQuantity, readStatus) {
const book = new Book(title, author, pageQuantity, readStatus);
myLibrary.push(book);
}
function renderLibrary(book) {
const bookCard = elementFromHTML(`
<article class="book" id="${book.uuid}">
<h3 class="book__title">${book.title}</h3>
<p class="book__body">${book.author}</p>
<p class="book__body">${book.pageQuantity} pages</p>
<button type="button" id="toggle-read">${book.readStatus}</button>
<button type="button" id="remove">Remove</button>
</article>
`);
grid.appendChild(bookCard);
}
function clearForm() {
const inputs = document.querySelectorAll("input");
inputs.forEach((input) => {
input.value = "";
if (input.checked) input.checked = false;
});
}
function submitBook() {
const title = document.querySelector("#title").value;
const author = document.querySelector("#author").value;
const pageQuantity = document.querySelector("#page-quantity").value;
const readStatus = document.querySelector("#read-status");
if (readStatus.checked) readStatus.value = "Read";
else readStatus.value = "Not read";
addBookToLibrary(title, author, pageQuantity, readStatus.value);
grid.innerHTML = "";
myLibrary.forEach(renderLibrary);
clearForm();
}
function removeBook(btn) {
const bookUUID = btn.target.closest(".book").id;
myLibrary = myLibrary.filter((book) => book.uuid !== bookUUID);
grid.innerHTML = "";
myLibrary.forEach(renderLibrary);
}
function toggleReadStatus(btn) {
const bookUUID = btn.target.closest(".book").id;
myLibrary.find((book) => {
if (book.uuid === bookUUID) {
if (book.readStatus === "Not read") {
return (book.readStatus = "Read");
} else {
return (book.readStatus = "Not read");
}
}
});
grid.innerHTML = "";
myLibrary.forEach(renderLibrary);
}
delegateEvent("click", "#add-book", () => modal.showModal());
delegateEvent("submit", "form", submitBook, modal);
delegateEvent("click", "#toggle-read", toggleReadStatus);
delegateEvent("click", "#remove", removeBook);