-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
203 lines (170 loc) · 5.27 KB
/
script.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
class Book {
constructor(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
}
toggleRead() {
this.read = !this.read;
}
}
class Library {
constructor() {
this.books = [];
this.modal = document.querySelector("[data-modal]");
this.cancelButton = document.querySelector("[data-cancel]");
this.addButton = document.querySelector("[data-new-book]");
this.addBookBtn = document.getElementById("add-book-form");
this.formValidator = new FormValidation();
this.addButton.addEventListener("click", () => this.showModal());
this.cancelButton.addEventListener("click", () => this.closeModal());
this.addBookBtn.addEventListener("submit", (e) => this.handleAddBook(e));
window.onload = () => {
this.displayBooks();
};
}
addBook(title, author, pages, read) {
const newBook = new Book(title, author, pages, read);
this.books.push(newBook);
this.displayBooks();
}
displayBooks() {
const bookContainer = document.querySelector(".book-container");
bookContainer.innerHTML = "";
this.books.forEach((book, index) => {
const card = document.createElement("div");
card.classList.add("card");
card.setAttribute("data-book-num", index);
card.innerHTML = `
<h2>${book.title}</h2>
<p><strong>Author:</strong> ${book.author}</p>
<p><strong>Pages:</strong> ${book.pages}</p>
<p><strong>Read:</strong> <span class="read-state">${
book.read ? "Yes" : "No"
}</span></p>
<div class="card-btn">
<button type="button" class="remove-btn">Remove</button>
<button type="button" class="update-read-btn">Toggle Read</button>
</div>
`;
bookContainer.appendChild(card);
});
// Add remove button EventListener
document.querySelectorAll(".remove-btn").forEach((button) => {
button.addEventListener("click", (event) => this.removeBook(event));
});
// Add update EventListener
document.querySelectorAll(".update-read-btn").forEach((button) => {
button.addEventListener("click", (event) => this.toggleReadStatus(event));
});
}
removeBook(event) {
const selectedCard = event.target.closest(".card");
const bookNumber = selectedCard.getAttribute("data-book-num");
this.books.splice(bookNumber, 1);
this.displayBooks();
}
toggleReadStatus(event) {
const selectedCard = event.target.closest(".card");
const bookNumber = selectedCard.getAttribute("data-book-num");
const readText = selectedCard.querySelector(".read-state");
this.books[bookNumber].toggleRead();
readText.textContent = this.books[bookNumber].read ? "Yes" : "No";
}
showModal() {
this.formValidator.validateForm(); // Validate on modal open
this.modal.showModal();
}
closeModal() {
this.addBookBtn.reset();
this.modal.close();
}
handleAddBook(event) {
event.preventDefault();
// Manually validate all inputs
this.formValidator.validateForm();
// If the form is invalid, stop submission
if (!this.addBookBtn.checkValidity()) {
return;
}
const title = document.getElementById("title").value;
const author = document.getElementById("author").value;
const pages = document.getElementById("pages").value;
const read = document.getElementById("read").checked;
this.addBook(title, author, pages, read);
this.closeModal();
}
}
class FormValidation {
constructor() {
this.title = document.getElementById("title");
this.author = document.getElementById("author");
this.pages = document.getElementById("pages");
this.handleEventListeners();
}
handleEventListeners() {
this.title.addEventListener("input", () => {
if (!this.title.value.trim()) {
this.title.setCustomValidity("Enter the title of the book.");
} else {
this.title.setCustomValidity("");
}
this.title.reportValidity();
});
this.author.addEventListener("input", () => {
if (!this.author.value.trim()) {
this.author.setCustomValidity("Enter the author of the book.");
} else {
this.author.setCustomValidity("");
}
this.author.reportValidity();
});
this.pages.addEventListener("input", () => {
const value = Number(this.pages.value);
if (isNaN(value) || value < 1) {
this.pages.setCustomValidity(
"Enter a valid number of pages (1 or more)."
);
} else {
this.pages.setCustomValidity("");
}
this.pages.reportValidity();
});
}
validateForm() {
// Title validation
// trim prevents empty space causing false validation
if (!this.title.value.trim()) {
this.title.setCustomValidity("Enter the title of the book.");
} else {
this.title.setCustomValidity("");
}
this.title.reportValidity();
// Author validation
if (!this.author.value.trim()) {
this.author.setCustomValidity("Enter the author of the book.");
} else {
this.author.setCustomValidity("");
}
this.author.reportValidity();
// Pages validation
const value = Number(this.pages.value);
if (isNaN(value) || value < 1) {
this.pages.setCustomValidity(
"Enter a valid number of pages (1 or more)."
);
} else {
this.pages.setCustomValidity("");
}
this.pages.reportValidity();
}
}
const myLibrary = new Library();
myLibrary.addBook("Ulysses", "James Joyce", "736 pages", true);
myLibrary.addBook(
"Crime and Punishment",
"Fyodor Dostoevsky",
"720 pages",
true
);