-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
418 lines (327 loc) · 12.1 KB
/
index.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
const books = [];
const RENDER_EVENT = 'render-book';
const SAVED_EVENT = 'saved-book';
const FINISHED_EVENT = 'finished-book';
const UNFINISHED_EVENT = 'unfinished-book';
const DELETED_EVENT = 'deleted-book';
const UPDATED_EVENT = 'updated-book';
const STORAGE_KEY = 'BOOKSHELF_APPS';
document.addEventListener('DOMContentLoaded', function () {
const submitForm = document.getElementById('inputBook');
submitForm.addEventListener('submit', function (event) {
event.preventDefault();
addBook();
const inputBookTitle = document.getElementById('inputBookTitle');
const inputBookAuthor = document.getElementById('inputBookAuthor');
const inputBookYear = document.getElementById('inputBookYear');
const inputBookIsComplete = document.getElementById('inputBookIsComplete');
inputBookTitle.value = '';
inputBookAuthor.value = '';
inputBookYear.value = '';
inputBookIsComplete.checked = false;
});
const submitEditForm = document.getElementById('editBook');
submitEditForm.addEventListener('submit', function (event) {
event.preventDefault();
handleEditSubmit();
selectedEditBook.splice(0, selectedEditBook.length);
});
const searchForm = document.getElementById('searchBook');
searchForm.addEventListener('submit', function (event) {
event.preventDefault();
searchBooks();
});
const searchResultsContainer = document.getElementById('searchResults');
searchResultsContainer.style.display = 'none';
if (isStorageExist()) {
loadDataFromStorage();
}
});
function addBook() {
const bookTitle = document.getElementById('inputBookTitle').value;
const bookAuthor = document.getElementById('inputBookAuthor').value;
const bookYearString = document.getElementById('inputBookYear').value;
const bookYear = parseInt(bookYearString);
const bookIsComplete = document.getElementById('inputBookIsComplete').checked;
const generatedID = generateId();
const bookObject = generateBookObject(generatedID, bookTitle, bookAuthor, bookYear, bookIsComplete);
books.push(bookObject);
document.dispatchEvent(new Event(RENDER_EVENT));
saveBookData();
}
function generateId() {
return +new Date();
}
function generateBookObject(id, title, author, year, isComplete) {
return {
id,
title,
author,
year,
isComplete
}
}
document.addEventListener(RENDER_EVENT, function () {
const unfinishedReading = document.getElementById('incompleteBookshelfList');
unfinishedReading.innerHTML = '';
const finishedReading = document.getElementById('completeBookshelfList');
finishedReading.innerHTML = '';
for (const bookItem of books) {
const bookElement = makeBook(bookItem);
if (!bookItem.isComplete)
unfinishedReading.append(bookElement);
else
finishedReading.append(bookElement);
}
});
function makeBook(bookObject) {
const textTitle = document.createElement('h3');
textTitle.innerText = '📘' + bookObject.title;
const textAuthor = document.createElement('p');
textAuthor.innerText = bookObject.author;
const textYear = document.createElement('p');
textYear.innerText = bookObject.year;
const buttonContainer = document.createElement('div');
buttonContainer.classList.add('action');
const container = document.createElement('article');
container.classList.add('book_item');
container.append(textTitle, textAuthor, textYear, buttonContainer);
container.setAttribute('id', `book-${bookObject.id}`);
buttonContainer.innerHTML = '';
if (bookObject.isComplete) {
const unfinishedButton = createButton('Belum Selesai Dibaca', 'green', function () {
unfinishedBookFromFinished(bookObject.id);
});
const trashButton = createButton('Hapus Buku', 'red', function () {
removeBookFromFinished(bookObject.id);
});
const editButton = createButton('Edit', 'white', function () {
editBook(bookObject.id);
});
buttonContainer.append(unfinishedButton, trashButton, editButton);
} else {
const finishedButton = createButton('Selesai Dibaca', 'green', function () {
addBookToFinished(bookObject.id);
});
const trashButton = createButton('Hapus Buku', 'red', function () {
removeBookFromFinished(bookObject.id);
});
const editButton = createButton('Edit', 'white', function () {
editBook(bookObject.id);
});
buttonContainer.append(finishedButton, trashButton, editButton);
}
return container;
}
function createButton(text, className, clickHandler) {
const button = document.createElement('button');
button.innerText = text;
button.classList.add(className);
button.addEventListener('click', clickHandler);
return button;
}
function addBookToFinished(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isComplete = true;
document.dispatchEvent(new Event(RENDER_EVENT));
finishedBookData();
}
function findBook(bookId) {
for (const bookItem of books) {
if (bookItem.id === bookId) {
return bookItem;
}
}
return null;
}
function removeBookFromFinished(bookId) {
const bookTarget = findBookIndex(bookId);
if (bookTarget === -1) return;
books.splice(bookTarget, 1);
document.dispatchEvent(new Event(RENDER_EVENT));
deleteBookData();
}
function unfinishedBookFromFinished(bookId) {
const bookTarget = findBook(bookId);
if (bookTarget == null) return;
bookTarget.isComplete = false;
document.dispatchEvent(new Event(RENDER_EVENT));
unfinishedBookData();
}
function findBookIndex(bookId) {
for (const index in books) {
if (books[index].id === bookId) {
return index;
}
}
return -1;
}
function isStorageExist() {
if (typeof (Storage) === undefined) {
alert('Browser kamu tidak mendukung local storage');
return false;
}
return true;
}
function saveBookData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(SAVED_EVENT));
}
}
document.addEventListener(SAVED_EVENT, function () {
let messageModal = "Buku dimasukkan ke rak!";
showModal(messageModal);
});
function finishedBookData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(FINISHED_EVENT));
}
}
document.addEventListener(FINISHED_EVENT, function () {
let messageModal = "Buku sudah dibaca!";
showModal(messageModal);
});
function deleteBookData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(DELETED_EVENT));
}
}
document.addEventListener(DELETED_EVENT, function () {
let messageModal = "Buku telah dihapus!";
showModal(messageModal);
});
function unfinishedBookData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(UNFINISHED_EVENT));
}
}
document.addEventListener(UNFINISHED_EVENT, function () {
let messageModal = "Buku belum dibaca!";
showModal(messageModal);
});
function loadDataFromStorage() {
const serializedData = localStorage.getItem(STORAGE_KEY);
let data = JSON.parse(serializedData);
if (data !== null) {
for (const todo of data) {
books.push(todo);
}
}
document.dispatchEvent(new Event(RENDER_EVENT));
}
function showModal(message) {
const modal = document.getElementById("myModal");
const modalContent = document.getElementById("modalContent");
const backdrop = document.getElementById("backdrop");
modalContent.textContent = message;
modal.style.display = "block";
backdrop.style.display = "block";
}
function closeModal() {
const modal = document.getElementById("myModal");
const backdrop = document.getElementById("backdrop");
modal.style.display = "none";
backdrop.style.display = "none";
}
function closeModalEdit() {
const editModal = document.getElementById("editModal");
const backdrop2 = document.getElementById("backdrop2");
backdrop2.style.display = "none";
editModal.style.display = "none";
}
function searchBooks() {
const searchTerm = document.getElementById('searchBookTitle').value.toLowerCase();
if (searchTerm === '') {
const searchResultsContainer = document.getElementById('searchResults');
searchResultsContainer.style.display = 'none';
const unfnsBook = document.getElementById('unfnsBook');
const fnsBook = document.getElementById('fnsBook');
unfnsBook.style.display = 'block';
fnsBook.style.display = 'block';
return;
}
const searchResults = books.filter(book => book.title.toLowerCase().includes(searchTerm));
displaySearchResults(searchResults);
}
function displaySearchResults(results) {
const searchResultsContainer = document.getElementById('searchResults');
const resultBookshelf = document.getElementById('resultBookshelf');
resultBookshelf.innerHTML = '';
const searchResultCount = document.getElementById('searchResultCount');
searchResultCount.innerText = results.length;
if (results.length === 0) {
const searchBookTitle = document.getElementById('searchBookTitle');
searchBookTitle.value = '';
searchResultsContainer.style.display = 'none';
const unfnsBook = document.getElementById('unfnsBook');
const fnsBook = document.getElementById('fnsBook');
unfnsBook.style.display = 'block';
fnsBook.style.display = 'block';
showModal('Buku tidak ditemukan! 😢');
return
} else {
results.forEach(book => {
const bookElement = makeBook(book);
resultBookshelf.appendChild(bookElement);
});
const unfnsBook = document.getElementById('unfnsBook');
const fnsBook = document.getElementById('fnsBook');
unfnsBook.style.display = 'none';
fnsBook.style.display = 'none';
}
searchResultsContainer.style.display = 'block';
}
const selectedEditBook = [];
function editBook(bookId) {
const bookToEdit = findBook(bookId);
console.log(bookToEdit);
if (bookToEdit == null) return;
selectedEditBook.push(bookToEdit);
const titleInput = document.getElementById("editBookTitle");
const authorInput = document.getElementById("editBookAuthor");
const yearInput = document.getElementById("editBookYear");
titleInput.value = bookToEdit.title;
authorInput.value = bookToEdit.author;
yearInput.value = bookToEdit.year;
const modal = document.getElementById("editModal");
const backdrop2 = document.getElementById("backdrop2");
modal.style.display = "block";
backdrop2.style.display = "block";
}
function handleEditSubmit() {
const getBookId = selectedEditBook[0].id;
console.log(getBookId);
const titleInput = document.getElementById("editBookTitle").value;
const authorInput = document.getElementById("editBookAuthor").value;
const yearInput = document.getElementById("editBookYear").value;
const bookTarget = findBook(getBookId);
bookTarget.id = getBookId;
bookTarget.title = titleInput;
bookTarget.author = authorInput;
bookTarget.year = parseInt(yearInput);
if (bookTarget == null) return;
selectedEditBook.splice(0, selectedEditBook.length);
document.dispatchEvent(new Event(RENDER_EVENT));
updateBookData();
closeModalEdit();
}
function updateBookData() {
if (isStorageExist()) {
const parsed = JSON.stringify(books);
localStorage.setItem(STORAGE_KEY, parsed);
document.dispatchEvent(new Event(UPDATED_EVENT));
}
}
document.addEventListener(UPDATED_EVENT, function () {
let messageModal = "Buku berhasil diupdate!";
showModal(messageModal);
});