-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
296 lines (256 loc) · 10.6 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// @ts-nocheck
let myLibrary = [];
function Book(title, author, pages, hasRead) {
this.title = title;
this.author = author;
this.pages = pages;
this.hasRead = hasRead;
this.info = () => {
return this.title + " by " + this.author + ", " + this.pages + " pages, " + (this.hasRead ? "already read" : "not read yet");
}
}
function addBookToLibrary(book) {
myLibrary.push(book);
}
function delay(ms) {
// @ts-ignore
return new Promise(resolve => setTimeout(resolve, ms));
}
// Input dropdown color styles
const inputDropdown = document.getElementById("input-dropdown");
inputDropdown.onchange = (e) => {
if (e.target.value == "")
inputDropdown.style.color = "#757575";
else
inputDropdown.style.color = "#000";
}
const submitButton = document.getElementById("submit-button");
const inputBookTitle = document.getElementById("input-bookTitle");
const inputBookAuthor = document.getElementById("input-bookAuthor");
const inputBookPages = document.getElementById("input-bookPages");
const bookTitleError = document.getElementById("book-title-error");
const bookAuthorError = document.getElementById("book-author-error");
const bookPagesError = document.getElementById("book-pages-error");
const readingProgressError = document.getElementById("reading-progress-error");
const loader = document.getElementById("loader");
const deleteAllButton = document.getElementById("stat-delete-button");
const BOOK_TITLE_ERROR_MESSAGE = "*Book title must not be empty!";
const BOOK_AUTHOR_ERROR_MESSAGE = "*Book author must not be empty!";
const BOOK_PAGES_ERROR_MESSAGE = "*Book pages must be larger than 0!";
const READING_PROGRESS_ERROR_MESSAGE = "*Reading progress must not be empty!";
const inputErrorMap = new Map([
[inputBookTitle, bookTitleError],
[inputBookAuthor, bookAuthorError],
[inputBookPages, bookPagesError],
[inputDropdown, readingProgressError]
]);
inputBookTitle.oninput = function() {checkOnChange(inputBookTitle);}
inputBookAuthor.oninput = function() {checkOnChange(inputBookAuthor);}
inputBookPages.oninput = function() {checkOnChange(inputBookPages);}
inputDropdown.oninput = function() {checkOnChange(inputDropdown);}
//Form interaction when onchange
function checkOnChange(htmlElement) {
if (htmlElement["value"] == "" || (htmlElement["type"] == "number" && htmlElement["value"] < 1)) {
htmlElement.style.borderColor = "red";
switch(inputErrorMap.get(htmlElement)) {
case bookTitleError:
bookTitleError.innerText = BOOK_TITLE_ERROR_MESSAGE;
break;
case bookAuthorError:
bookAuthorError.innerText = BOOK_AUTHOR_ERROR_MESSAGE;
break;
case bookPagesError:
bookPagesError.innerText = BOOK_PAGES_ERROR_MESSAGE;
break;
case readingProgressError:
readingProgressError.innerText = READING_PROGRESS_ERROR_MESSAGE;
break;
}
} else {
htmlElement.style.borderColor = "#04AA6D";
inputErrorMap.get(htmlElement).innerText = "";
}
}
// Form Validation when submit
submitButton.onclick = (e) => {
for (const [key, value] of inputErrorMap) {
if (key["value"] == "" || (key == inputBookPages && key["value"] < 1)) {
switch(inputErrorMap.get(key)) {
case bookTitleError:
value.innerText = BOOK_TITLE_ERROR_MESSAGE;
break;
case bookAuthorError:
value.innerText = BOOK_AUTHOR_ERROR_MESSAGE;
break;
case bookPagesError:
value.innerText = BOOK_PAGES_ERROR_MESSAGE;
break;
case readingProgressError:
value.innerText = READING_PROGRESS_ERROR_MESSAGE;
break;
}
key.style.borderColor = "red";
key.style.animation = "shake 0.82s";
setTimeout(function(){
key.style.animation = "none";
}, 900);
}
}
if(inputBookTitle["value"] && inputBookPages["value"] && inputDropdown["value"] && inputBookAuthor["value"]) {
loader.style.display = "block";
delay(1500).then(() => {
loader.style.opacity="0";
delay(1500).then(() => {
loader.style.display="none";
loader.style.opacity="100";
});
const newBook = new Book(inputBookTitle["value"], inputBookAuthor["value"], inputBookPages["value"], inputDropdown["value"])
addBookToLibrary(newBook);
localStorage.setItem('library-data', JSON.stringify(myLibrary));
resetForm();
displayAllBooks();
showToast("form-success");
});
} else {
showToast("form-warning");
}
}
//Delete all button onclick event
deleteAllButton.onclick = (e) => {
if (myLibrary.length > 0) {
const modalContainer = document.querySelector(".modal-container");
modalContainer.classList.add('show');
modalContainer.onclick = (e) => {
const { target } = e;
if (target.classList.contains('close-modal')) {
modalContainer.classList.remove("show");
} else if (target.classList.contains('yes')) {
console.log('Hello');
myLibrary.splice(0, myLibrary.length);
localStorage.setItem('library-data', JSON.stringify(myLibrary));
modalContainer.classList.remove("show");
displayAllBooks();
showToast("reset-success");
}
}
} else {
showToast("reset-error");
}
};
function resetForm() {
const form = document.querySelector(".add-book-form");
const input = document.querySelectorAll("input");
for(let i = 0; i < input.length; i++) {
input[i].style.borderColor = "#5F9AEC";
}
const select = document.querySelector("select");
select.style.borderColor = "#5F9AEC";
select.style.color = "#757575";
form.reset();
}
function changeReadingProgress(e) {
const index = e.parentNode.parentNode.rowIndex - 1;
if (e.innerText == "Completed") {
myLibrary[index].hasRead = "Not yet";
} else {
myLibrary[index].hasRead = "Completed";
}
localStorage.setItem('library-data', JSON.stringify(myLibrary));
displayAllBooks();
}
function deleteBook(e) {
const tr = e.parentNode.parentNode.rowIndex - 1;
myLibrary.splice(tr, 1);
//console.log(myLibrary);
showToast("delete-item-success");
localStorage.setItem('library-data', JSON.stringify(myLibrary));
displayAllBooks();
}
function updateLibraryStats() {
const totalBooksCount = document.querySelector("#total-books");
const booksCompletedCount = document.querySelector("#books-completed");
const booksIncompleteCount = document.querySelector("#books-incomplete");
let completedCount = 0;
let incompleteCount = 0;
for(let i = 0; i < myLibrary.length; i++) {
if (myLibrary[i].hasRead == "Completed") {
completedCount++;
} else {
incompleteCount++;
}
}
totalBooksCount.innerText = myLibrary.length;
booksCompletedCount.innerText = completedCount;
booksIncompleteCount.innerText = incompleteCount;
}
function showToast(toastType) {
let toast;
let toastClose;
let toastProgress;
let toastMessage;
if (toastType == "form-success" || toastType == "delete-item-success" || toastType == "reset-success") {
toast = document.querySelector("#success-toast");
toastClose = document.querySelector("#success-toast .close");
toastProgress = document.querySelector("#success-toast .progress");
toastMessage = document.querySelector("#success-toast .text-2");
if (toastType == "form-success") {
toastMessage.innerText = "New record has been added !";
} else if (toastType == "delete-item-success") {
toastMessage.innerText = "Record has been deleted!";
} else if (toastType == "reset-success") {
toastMessage.innerText = "Library has been reset successfully!";
}
} else if (toastType == "reset-error") {
toast = document.querySelector("#error-toast");
toastClose = document.querySelector("#error-toast .close");
toastProgress = document.querySelector("#error-toast .progress");
} else if (toastType == "form-warning") {
toast = document.querySelector("#warning-toast");
toastClose = document.querySelector("#warning-toast .close");
toastProgress = document.querySelector("#warning-toast .progress");
}
toast.classList.add("toast-active");
toastProgress.classList.add("toast-active");
setTimeout(() => {
toast.classList.remove("toast-active");
}, 2000);
setTimeout(() => {
toastProgress.classList.remove("toast-active");
}, 2300);
toastClose.addEventListener("click", () => {
toast.classList.remove("toast-active");
setTimeout(() => {
toastProgress.classList.remove("toast-active");
}, 300);
});
}
function displayAllBooks() {
const bookRowInTable = document.getElementById("table-row");
bookRowInTable.textContent = ""
for(let i = 0; i < myLibrary.length; i++) {
const row = bookRowInTable.insertRow(-1);
const numberCell = row.insertCell(0)
const titleCell = row.insertCell(1);
const authorCell = row.insertCell(2);
const pagesCell = row.insertCell(3);
const readingProgressCell = row.insertCell(4);
const actionCell = row.insertCell(5);
numberCell.innerText = (i + 1) < 10 ? "0" + parseInt(i + 1) : parseInt(i + 1);
titleCell.innerText = myLibrary[i].title;
authorCell.innerText = myLibrary[i].author;
pagesCell.innerText = myLibrary[i].pages;
readingProgressCell.innerHTML = `<div class="reading-progress-div" style="color:${myLibrary[i].hasRead == "Completed" ? 'green' : 'red'};" onclick=changeReadingProgress(this)>${myLibrary[i].hasRead}</div>`;
actionCell.innerHTML = `<a class="delete-btn" onclick=deleteBook(this)><span style="color: #003E77"><i class="fa-solid fa-trash-can"></span></i>Delete</a>`;
}
updateLibraryStats();
}
function getLocalStorageBooks() {
if (localStorage.getItem("library-data") == null) {
localStorage.setItem("library-data", '[]');
}
var existing_data = JSON.parse(localStorage.getItem('library-data'));
myLibrary = existing_data;
localStorage.setItem('library-data', JSON.stringify(myLibrary));
}
getLocalStorageBooks();
displayAllBooks();