-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
112 lines (101 loc) · 3.62 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
const books = JSON.parse(localStorage.getItem('books')) || [];
const booksContainer = document.getElementById('books');
const addBtn = document.getElementById('add-btn');
const title = document.getElementById('title');
const author = document.getElementById('author');
const bookNavBtn = document.getElementById('books-nav-btn');
const addNavBtn = document.getElementById('add-nav-btn');
const contactNavBtn = document.getElementById('contact-nav-btn');
const contactSection = document.getElementById('contact-section');
const formSection = document.getElementById('form-section');
const addBookAlert = document.getElementById('add-book-alert');
const dateSpan = document.getElementById('date-span');
setInterval(() => {
const currentDate = luxon.DateTime; /* eslint-disable-line no-undef, prefer-destructuring */
dateSpan.textContent = currentDate.local().toFormat('FF');
}, 100);
class Book {
constructor(title, author, uuid) {
this.title = title;
this.author = author;
this.uuid = uuid;
}
addBook() {
const book = new Book(this.title, this.author, this.uuid);
books.push(book);
localStorage.setItem('books', JSON.stringify(books));
}
removeBook() {
const index = books.findIndex((book) => book.uuid === this.uuid);
books.splice(index, 1);
}
}
bookNavBtn.addEventListener('click', () => {
contactSection.classList.add('d-none');
contactSection.classList.remove('active');
booksContainer.classList.remove('d-none');
booksContainer.classList.add('active');
formSection.classList.remove('active');
formSection.classList.add('d-none');
});
addNavBtn.addEventListener('click', () => {
contactSection.classList.add('d-none');
contactSection.classList.remove('active');
booksContainer.classList.add('d-none');
booksContainer.classList.remove('active');
formSection.classList.remove('d-none');
formSection.classList.add('active');
});
contactNavBtn.addEventListener('click', () => {
contactSection.classList.remove('d-none');
contactSection.classList.add('active');
booksContainer.classList.add('d-none');
formSection.classList.add('d-none');
formSection.classList.remove('active');
booksContainer.classList.remove('active');
});
const setItems = () => {
booksContainer.innerHTML = '';
if (books.length !== 0) {
books.forEach((book) => {
const li = document.createElement('li');
li.className = 'row';
const h2 = document.createElement('h2');
h2.textContent = `"${book.title}" by ${book.author}`;
h2.className = 'col';
const button = document.createElement('button');
button.className = 'btn btn-danger m-3 col-2';
button.textContent = 'Remove';
button.addEventListener('click', () => {
const bookToRemove = new Book(book.title, book.author);
bookToRemove.removeBook();
localStorage.setItem('books', JSON.stringify(books));
setItems();
});
li.appendChild(h2);
li.appendChild(button);
return booksContainer.appendChild(li);
});
} else {
const h5 = document.createElement('h5');
h5.textContent = 'No Books Found';
h5.className = 'text-center';
return booksContainer.appendChild(h5);
}
return null;
};
const getUuid = () => new Date().getTime().toString() + Math.floor(Math.random() * 1000000);
addBtn.addEventListener('click', () => {
if (title.value !== '' && author.value !== '') {
const book = new Book(title.value, author.value, getUuid());
book.addBook();
title.value = '';
author.value = '';
setItems();
addBookAlert.classList.remove('d-none');
setTimeout(() => {
addBookAlert.classList.add('d-none');
}, 3000);
}
});
setItems();