-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
137 lines (125 loc) · 4.29 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
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
const $addButton = document.querySelector('#add');
const $cards = document.querySelector('.div-remove');
const $titleInput = document.querySelector('#title');
const $authorInput = document.querySelector('#author');
const $listLink = document.querySelector('#list');
const $contactLink = document.querySelector('#contact');
const $addLink = document.querySelector('#add-new');
const $h1 = document.querySelector('#h1');
const $date = document.getElementById('date');
class Books {
constructor() {
this.books = [];
this.$removeButtons = null;
this.initializeRemoveButtons();
}
initializeRemoveButtons() {
this.$removeButtons = document.querySelectorAll('.remove-button');
this.$removeButtons.forEach((removeButton) => {
removeButton.addEventListener('click', () => {
const bookTitle = removeButton.previousElementSibling.querySelector('.book-title').textContent;
const bookAuthor = removeButton.previousElementSibling.querySelector('.book-author').textContent;
for (let i = 0; i < this.books.length; i += 1) {
if (this.books[i].title === bookTitle && this.books[i].author === bookAuthor) {
this.books.splice(i, 1);
localStorage.setItem('books', JSON.stringify(this.books));
break;
}
}
removeButton.parentNode.remove();
this.displayBooks();
});
});
}
displayBooks() {
const booksSaved = JSON.parse(localStorage.getItem('books'));
if (booksSaved) {
this.books = booksSaved;
const booksHTML = this.books.map((book) => `
<div class="card-book">
<div class="text-container">
<h3 class="book-title">${book.title}</h3> <span> by </span>
<h3 class="book-author">${book.author}</h3>
</div>
<button class="remove-button">Remove</button>
<hr>
</div>
`).join('');
$cards.innerHTML = booksHTML;
const element = document.querySelectorAll('.card-book');
element.forEach((element, index) => {
if (index % 2 === 1) {
element.classList.add('alt');
}
});
this.initializeRemoveButtons();
}
}
addBook() {
if ($titleInput.value !== '' && $authorInput.value !== '') {
const book = {
title: $titleInput.value,
author: $authorInput.value,
};
this.books.push(book);
localStorage.setItem('books', JSON.stringify(this.books));
this.displayBooks();
}
}
showDate() {
const date = new Date();
const options = {
year: 'numeric',
month: 'long',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
hour12: true,
};
this.date = date.toLocaleString('en-US', options);
return this.date;
}
}
const myBooks = new Books();
$addButton.addEventListener('click', (e) => {
e.preventDefault();
if ($authorInput !== '' && $titleInput !== '') {
myBooks.addBook();
$authorInput.value = '';
$titleInput.value = '';
}
});
document.addEventListener('DOMContentLoaded', () => {
myBooks.displayBooks();
});
$addLink.addEventListener('click', () => {
document.querySelector('.div-remove').classList.add('hide');
document.querySelector('.form').classList.remove('hide');
document.querySelector('.section-contact-info').classList.add('hide');
$h1.textContent = 'Add a new book';
$addLink.style.color = 'blue';
$listLink.style.color = 'black';
$contactLink.style.color = 'black';
});
$listLink.addEventListener('click', () => {
document.querySelector('.div-remove').classList.remove('hide');
document.querySelector('.form').classList.add('hide');
document.querySelector('.section-contact-info').classList.add('hide');
$h1.textContent = 'All awesome books';
$listLink.style.color = 'blue';
$addLink.style.color = 'black';
$contactLink.style.color = 'black';
});
$contactLink.addEventListener('click', () => {
document.querySelector('.div-remove').classList.add('hide');
document.querySelector('.form').classList.add('hide');
document.querySelector('.section-contact-info').classList.remove('hide');
$h1.textContent = 'Contact information';
$contactLink.style.color = 'blue';
$addLink.style.color = 'black';
$listLink.style.color = 'black';
});
setInterval(() => {
$date.textContent = myBooks.showDate();
}, 1000);