-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
171 lines (146 loc) · 4.41 KB
/
app.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
// Declarations.
class Book {
constructor(title, author, pages, isRead) {
this.title = title;
this.author = author;
this.pages = pages;
this.isRead = isRead;
}
}
let myLibrary = [];
// Modal selectors.
const container = document.querySelector("#container");
const addBookBtn = document.querySelector("#add-book");
const modal = document.querySelector("#modal");
const closeBtn = document.querySelector("#close");
// Form selectors.
const titleField = document.querySelector("#title");
const authorField = document.querySelector("#author");
const pagesField = document.querySelector("#pages");
const readField = document.querySelector("#read");
const submitBtn = document.querySelector("#submit");
displayBooks();
// Event Listeners.
addBookBtn.addEventListener("click", () => {
modal.style.display = "block";
});
submitBtn.addEventListener("click", () => {
if (formValidation()) {
myLibrary.push(addBookToLibrary());
displayBooks();
saveBooks();
modal.style.display = "none";
clearForm();
}
});
closeBtn.addEventListener("click", () => (modal.style.display = "none"));
window.addEventListener("click", (e) => {
if (e.target == modal) modal.style.display = "none";
});
// Functions.
function addBookToLibrary() {
let title = titleField.value;
let author = authorField.value;
let pages = Number(pagesField.value);
let isRead = readField.checked;
return new Book(title, author, pages, isRead);
}
function displayBooks() {
container.innerHTML = "";
for (let i = 0; i < myLibrary.length; i++) {
createBookCard(myLibrary[i]);
}
}
function createBookCard(book) {
const bookContainer = document.createElement("div");
bookContainer.classList.add("book-container");
bookContainer.setAttribute("data-i", myLibrary.indexOf(book));
const bookTitle = document.createElement("h2");
bookTitle.classList.add("book-title");
bookTitle.textContent = book.title;
const bookAuthor = document.createElement("p");
bookAuthor.classList.add("book-author");
bookAuthor.textContent = book.author;
const bookPages = document.createElement("p");
bookPages.classList.add("book-pages");
bookPages.textContent = `${book.pages} pages`;
const bookRead = document.createElement("p");
bookRead.classList.add("book-read");
bookRead.textContent = checkIsRead(book.isRead);
const delBtn = document.createElement("button");
delBtn.classList.add("delete-item-btn");
delBtn.innerHTML = "×";
addDeleteButtonListener(delBtn, bookContainer);
const toggleReadBtn = document.createElement("button");
toggleReadBtn.classList.add("toggle-read-btn");
toggleReadBtn.textContent = "Toggle read status";
addToggleReadButtonListener(toggleReadBtn, book, bookRead);
bookContainer.appendChild(bookTitle);
bookContainer.appendChild(bookAuthor);
bookContainer.appendChild(bookPages);
bookContainer.appendChild(bookRead);
bookContainer.appendChild(toggleReadBtn);
bookContainer.appendChild(delBtn);
container.appendChild(bookContainer);
}
function checkIsRead(isRead) {
if (isRead) {
return "Already read";
} else {
return "Not read yet";
}
}
function addToggleReadButtonListener(button, book, bookRead) {
button.addEventListener("click", () => {
if (book.isRead) {
book.isRead = false;
bookRead.textContent = checkIsRead(book.isRead);
saveBooks();
} else {
book.isRead = true;
bookRead.textContent = checkIsRead(book.isRead);
saveBooks();
}
});
}
function addDeleteButtonListener(button, card) {
button.addEventListener("click", () => {
const bookIndex = card.dataset.i;
myLibrary.splice(bookIndex, 1);
displayBooks();
saveBooks();
});
}
function formValidation() {
if (titleField.value == "") {
alert("Please enter the book's title.");
titleField.focus();
return false;
}
if (authorField.value == "") {
alert("Please enter the author's name.");
authorField.focus();
return false;
}
if (pagesField.value == "") {
alert("Please enter the pages number.");
pagesField.focus();
return false;
}
return true;
}
function clearForm() {
titleField.value = "";
authorField.value = "";
pagesField.value = "";
readField.checked = false;
}
function saveBooks() {
localStorage.setItem("myLibrary", JSON.stringify(myLibrary));
}
function getBooks() {
myLibrary = JSON.parse(localStorage.getItem("myLibrary"));
if (myLibrary == null) myLibrary = [];
displayBooks();
}
getBooks();