-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
115 lines (104 loc) · 3.55 KB
/
script.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
/* eslint-disable max-classes-per-file */
window.onload = () => {
const add = document.querySelector('.add');
const dateEl = document.querySelector('.date');
const displayDate = () => {
setInterval(() => {
const date = new Date().toUTCString();
dateEl.innerHTML = date;
}, 1000);
};
displayDate();
class Book {
constructor(title, author) {
this.title = title;
this.author = author;
}
}
class Methods {
constructor() {
this.bookList = [];
this.addBook = document.querySelector('.all-books');
this.parser = new DOMParser();
}
removeBook = (e, newBookElement) => {
const index = e.target.getAttribute('myIndex');
this.bookList = this.bookList.filter((element, i) => {
if (i === parseInt(index, 10)) {
return false;
}
return true;
});
newBookElement.remove();
localStorage.setItem('bookArray', JSON.stringify(this.bookList));
this.showBooks();
};
showBooks = () => {
this.addBook.innerHTML = '';
this.bookList.forEach((e, i) => {
const newBook = `
<div class="book-div">
<p>"${e.title}" by ${e.author}</p>
<button type="button" class="remove" myIndex ="${i}" >Remove</button>
</div>
`;
const newBookElement = this.parser.parseFromString(newBook, 'text/html').body.firstChild;
const remove = newBookElement.querySelector('.remove');
remove.addEventListener('click', (e) => {
this.removeBook(e, newBookElement);
});
this.addBook.append(newBookElement);
});
};
}
const method = new Methods();
const bookStorage = localStorage.getItem('bookArray');
const success = document.querySelector('.successMsg');
if (bookStorage) {
method.bookList = JSON.parse(bookStorage);
method.showBooks();
}
add.addEventListener('click', (e) => {
e.preventDefault();
const title = document.querySelector('.title').value;
const author = document.querySelector('.author').value;
const book = new Book(title, author);
method.bookList.push(book);
method.showBooks();
document.querySelector('.title').value = '';
document.querySelector('.author').value = '';
localStorage.setItem('bookArray', JSON.stringify(method.bookList));
success.classList.remove('transparent');
setTimeout(() => success.classList.add('transparent'), 3000);
});
};
const listEl = document.querySelector('.list');
const addNewEl = document.querySelector('.add-new');
const contactEl = document.querySelector('.contact');
const bookSection = document.querySelector('#books');
const formSection = document.querySelector('#form');
const contactSection = document.querySelector('#contact');
listEl.addEventListener('click', () => {
bookSection.classList.remove('hidden');
listEl.classList.add('active');
addNewEl.classList.remove('active');
contactEl.classList.remove('active');
formSection.classList.add('hidden');
contactSection.classList.add('hidden');
});
addNewEl.addEventListener('click', () => {
bookSection.classList.add('hidden');
formSection.classList.remove('hidden');
addNewEl.classList.add('active');
listEl.classList.remove('active');
contactEl.classList.remove('active');
contactSection.classList.add('hidden');
});
contactEl.addEventListener('click', () => {
bookSection.classList.add('hidden');
formSection.classList.add('hidden');
contactSection.classList.remove('hidden');
contactEl.classList.add('active');
addNewEl.classList.remove('active');
listEl.classList.remove('active');
});