-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
133 lines (110 loc) · 3.82 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* eslint-disable no-undef */
const homeLink = document.querySelector('.home');
const listLink = document.querySelector('#list-link');
const addNewLink = document.querySelector('#add-new-link');
const contactLink = document.querySelector('#contact-link');
const booksListElem = document.querySelector('.list');
const addNewBookElem = document.querySelector('.add-new');
const contactElem = document.querySelector('.contact');
listLink.classList.add('active');
const displayHome = () => {
booksListElem.classList.remove('display-none');
addNewBookElem.classList.add('display-none');
contactElem.classList.add('display-none');
listLink.classList.add('active');
addNewLink.classList.remove('active');
contactLink.classList.remove('active');
};
listLink.addEventListener('click', displayHome);
homeLink.addEventListener('click', displayHome);
addNewLink.addEventListener('click', () => {
booksListElem.classList.add('display-none');
addNewBookElem.classList.remove('display-none');
contactElem.classList.add('display-none');
listLink.classList.remove('active');
addNewLink.classList.add('active');
contactLink.classList.remove('active');
});
contactLink.addEventListener('click', () => {
booksListElem.classList.add('display-none');
addNewBookElem.classList.add('display-none');
contactElem.classList.remove('display-none');
listLink.classList.remove('active');
addNewLink.classList.remove('active');
contactLink.classList.add('active');
});
class Library {
books;
constructor(books) {
this.books = books;
}
removeBook(removedBook) {
this.books = this.books.filter((book) => book !== removedBook);
}
addBook(newBookTitle, newBookAuthor) {
const book = {
title: newBookTitle,
author: newBookAuthor,
};
this.books.push(book);
displayHome();
}
}
const library = new Library([]);
const addAwesomeBooksToLocalStorage = () => {
const booksArrString = JSON.stringify(library.books);
localStorage.setItem('awesomeBooks', booksArrString);
};
const showBooks = () => {
const booksDiv = document.querySelector('.books');
booksDiv.innerHTML = '';
if (library.books.length === 0) {
booksDiv.style.display = 'none';
} else {
booksDiv.style.display = 'block';
}
for (let i = 0; i < library.books.length; i += 1) {
const bookDiv = document.createElement('div');
bookDiv.classList.add('book');
booksDiv.appendChild(bookDiv);
const titleParagraph = document.createElement('p');
titleParagraph.classList.add('title-author');
titleParagraph.textContent = `"${library.books[i].title}" (Reason:) ${library.books[i].author}`;
bookDiv.appendChild(titleParagraph);
const removeBtn = document.createElement('button');
removeBtn.classList.add('remove');
removeBtn.textContent = 'Remove';
removeBtn.onclick = () => {
library.removeBook(library.books[i]);
addAwesomeBooksToLocalStorage();
showBooks();
};
bookDiv.appendChild(removeBtn);
}
};
const getAwesomeBooksFromLocalStorage = () => {
const booksArrString = localStorage.getItem('awesomeBooks');
library.books = JSON.parse(booksArrString);
showBooks();
};
if (localStorage.getItem('awesomeBooks') == null) {
addAwesomeBooksToLocalStorage();
} else {
getAwesomeBooksFromLocalStorage();
}
const addBtn = document.querySelector('#add-btn');
addBtn.addEventListener('click', () => {
const title = document.querySelector('#title-input');
const author = document.querySelector('#author-input');
library.addBook(title.value, author.value);
addAwesomeBooksToLocalStorage();
showBooks();
title.value = '';
author.value = '';
});
const getTime = () => {
const now = luxon.DateTime.now().toLocaleString(luxon.DateTime.DATETIME_MED_WITH_SECONDS);
const dateElem = document.querySelector('.display-date');
dateElem.textContent = now;
};
setInterval(getTime, 1000);