-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.js
113 lines (97 loc) · 3.08 KB
/
book.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
/* eslint-disable no-use-before-define */
const newBk = document.querySelector('#new-bk');
const currentDate = document.querySelector('.current-date');
// CURRENT DATE
const date = new Date();
currentDate.append(document.createElement('p').innerHTML = `${date}`);
class BooksClass {
constructor(bookTitle, bookAuthor) {
this.title = bookTitle;
this.author = bookAuthor;
this.bookCollection = JSON.parse(localStorage.getItem('Books')) || [];
}
AddBook() {
const { title } = this;
const { author } = this;
this.bookCollection.push({ title, author });
localStorage.setItem('Books', JSON.stringify(this.bookCollection));
}
ShowAllBooks() {
const showBks = document.querySelector('#show-bks');
showBks.innerHTML = '';
for (let i = 0; i < this.bookCollection.length; i += 1) {
const addedBk = document.createElement('div');
addedBk.className = 'added-bks';
addedBk.style.backgroundColor = '#d3d3d3';
addedBk.id = `${i}`;
addedBk.innerHTML = `
<p class="new-book-title">${this.bookCollection[i].title}</p>
<p class="new-book-author">${this.bookCollection[i].author}</p>
<button id="del-bk">Remove</button>`;
showBks.appendChild(addedBk);
}
const removeBtns = document.querySelectorAll('.added-bks');
this.removeBook(removeBtns);
}// ShowAllBooks
removeBook(removeBtns) {
removeBtns.forEach((btn) => {
btn.addEventListener('click', () => {
this.bookCollection.splice(btn.getAttribute('id'), 1);
localStorage.setItem('Books', JSON.stringify(this.bookCollection));
this.ShowAllBooks();
});
});
}// removeBook
getLocalStorage() {
return this.bookCollection;
}
}
const awesomeBooksIn = new BooksClass();
if (awesomeBooksIn.getLocalStorage().length > 0) {
awesomeBooksIn.ShowAllBooks();
}
newBk.addEventListener('submit', (e) => {
e.preventDefault();
const bookTitle = document.querySelector('#bk-title').value;
const bookAuthor = document.querySelector('#bk-author').value;
const awesomeBooks = new BooksClass(bookTitle, bookAuthor);
awesomeBooks.AddBook();
awesomeBooks.ShowAllBooks();
newBk.reset();
});
// NAVIGATION
const menuItems = document.querySelectorAll('.menu-item');
const collection = document.querySelector('.collection');
const add = document.querySelector('.add-books-section');
const info = document.querySelector('.contact-section');
hideAll();
collection.style.display = 'block';
function hideAll() {
collection.style.display = 'none';
add.style.display = 'none';
info.style.display = 'none';
}
menuItems.forEach((item) => {
item.addEventListener('click', () => {
const key = item.getAttribute('value');
switch (key) {
case 'list':
{
hideAll();
collection.style.display = 'block'; break;
}
case 'add':
{
hideAll();
add.style.display = 'block'; break;
}
case 'info':
{
hideAll();
info.style.display = 'block'; break;
}
default: collection.style.display = 'block';
break;
}
});
});