-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
106 lines (96 loc) · 3.26 KB
/
app.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
const bookContainer = document.querySelector('.bookDisplay');
function displayBook(book) {
bookContainer.insertAdjacentHTML(
'beforeend',
`<div class="book-details" id="book-${book.id}">
<p>"${book.title}" by ${book.author}</p>
<button class="remove" type="button" id="remove-${book.id}">Remove</button>
</div>`,
);
}
class BookCollection {
constructor(bookArray) {
this.bookArray = bookArray;
}
removeBook(book) {
this.bookArray.splice(book, 1);
localStorage.setItem('bookCollectionArray', JSON.stringify(this.bookArray));
bookContainer.innerHTML = '';
this.bookArray.forEach((book, i) => {
book.id = i;
displayBook(book);
if (document.querySelector(`#remove-${book.id}`)) {
document
.querySelector(`#remove-${book.id}`)
.addEventListener('click', this.removeBook.bind(this, book));
}
});
if (this.bookArray[0]) bookContainer.style.border = 'solid 2.5px #635e5e';
else bookContainer.style.border = 'none';
}
addBook(title, author) {
this.bookArray.push({
title,
author,
id: this.bookArray.length,
});
localStorage.setItem('bookCollectionArray', JSON.stringify(this.bookArray));
displayBook(this.bookArray[this.bookArray.length - 1]);
if (
document.querySelector(
`#remove-${this.bookArray[this.bookArray.length - 1].id}`,
)
) {
document
.querySelector(
`#remove-${this.bookArray[this.bookArray.length - 1].id}`,
)
.addEventListener('click', () => {
this.removeBook(this.bookArray[this.bookArray.length - 1]);
});
}
if (this.bookArray[0]) bookContainer.style.border = 'solid 3px #000000';
else bookContainer.style.border = 'none';
}
}
const bookCollection = localStorage.getItem('bookCollectionArray')
? new BookCollection(JSON.parse(localStorage.getItem('bookCollectionArray')))
: new BookCollection([]);
bookCollection.bookArray.forEach((book) => {
displayBook(book);
if (document.querySelector(`#remove-${book.id}`)) {
document
.querySelector(`#remove-${book.id}`)
.addEventListener(
'click',
bookCollection.removeBook.bind(bookCollection, book),
);
}
});
if (bookCollection.bookArray[0]) {
bookContainer.style.border = 'solid 3px #000000';
} else bookContainer.style.border = 'none';
const addButton = document.querySelector('.addBtn');
const titleInput = document.querySelector('#titleInput');
const authorInput = document.querySelector('#authorInput');
addButton.addEventListener('click', () => {
if (titleInput.value && authorInput.value) {
bookCollection.addBook(titleInput.value, authorInput.value);
titleInput.value = '';
authorInput.value = '';
}
});
document.querySelectorAll('.section').forEach((section) => {
if (!(section.id === 'bookList')) section.style.display = 'none';
});
const navBar = document.querySelector('.navBar');
navBar.addEventListener('click', (e) => {
e.preventDefault();
if (e.target.classList.contains('navLink')) {
const anchorHref = e.target.href.split('#')[1];
document.querySelectorAll('.section').forEach((section) => {
if (section.id === anchorHref) section.style.display = 'flex';
else section.style.display = 'none';
});
}
});