-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
111 lines (94 loc) · 2.76 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
const bookList = document.querySelector('.books');
const form = document.getElementById('newBook');
const titleInput = document.getElementById('title');
const authorInput = document.getElementById('author');
const list = document.querySelector('.container');
const addNewBook = document.querySelector('.new-book');
const contact = document.querySelector('.contact');
const navItem1 = document.querySelector('.nav-item-1');
const navItem2 = document.querySelector('.nav-item-2');
const navItem3 = document.querySelector('.nav-item-3');
navItem1.addEventListener('click', () => {
addNewBook.style.display = 'none';
contact.style.display = 'none';
list.style.display = 'block';
});
navItem2.addEventListener('click', () => {
list.style.display = 'none';
contact.style.display = 'none';
addNewBook.style.display = 'block';
});
navItem3.addEventListener('click', () => {
addNewBook.style.display = 'none';
list.style.display = 'none';
contact.style.display = 'flex';
});
let bookss = [];
class BookClass {
constructor(title, author, id) {
this.title = title;
this.author = author;
this.id = id;
}
bookCode() {
return `<li><div class="list-items">
<p>"${this.title}" by ${this.author}</p>
<button type="button" data-id='${this.id}' class="remove">remove</button>
</div>
</li>`;
}
static addBook(book) {
let id = 1;
if (bookss.length > 0) {
id = bookss[bookss.length - 1].id + 1;
}
book.id = id;
bookss.push(book);
localStorage.setItem('bookss', JSON.stringify(bookss));
}
static remove(id) {
bookss = bookss.filter((b) => b.id !== Number(id));
localStorage.setItem('bookss', JSON.stringify(bookss));
}
}
const storedBooks = JSON.parse(localStorage.getItem('bookss'));
function showBooks() {
const booksCode = bookss.map((book) => new BookClass(book.title, book.author, book.id)
.bookCode());
bookList.innerHTML = booksCode.join('');
const btn = document.querySelectorAll('.remove');
btn.forEach((el) => {
el.addEventListener('click', (e) => {
const id = e.target.getAttribute('data-id');
BookClass.remove(id);
showBooks();
});
});
}
if (storedBooks) {
bookss = storedBooks;
showBooks();
}
form.addEventListener('submit', (e) => {
e.preventDefault();
const title = titleInput.value.trim();
const author = authorInput.value.trim();
if (!title || !author) {
return;
}
const newBook = new BookClass(title, author);
BookClass.addBook(newBook);
showBooks();
titleInput.value = '';
authorInput.value = '';
});
const displayTime = () => {
const date = new Date();
document.querySelector('.time').innerHTML = date;
};
document.addEventListener('DOMContentLoaded', () => {
displayTime();
setInterval(() => {
displayTime();
}, 1000);
});