generated from microverseinc/readme-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
93 lines (83 loc) · 2.77 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
class Book {
static books = [];
constructor(title, author) {
this.title = title;
this.author = author;
}
addBook() {
Book.books.push(this);
Book.store(Book.books);
}
static store(books) {
localStorage.setItem('books', JSON.stringify(books));
}
static displayBooks() {
if (!JSON.parse(localStorage.getItem('books'))) {
Book.books = [];
} else {
Book.books = JSON.parse(localStorage.getItem('books'));
}
let html = '<table class="">';
if (Book.books) {
Book.books.forEach((book, index) => {
html += `<tr class="${index % 2 === 0 ? 'bg-green-100' : ''}">
<td class="book-information">
<span class="book-title break-all">${book.title}</span>
<span class="book-author break-all"> by ${book.author}</span>
</td>
<td class="text-right">
<button type="button" class="removeBtn" id="${index}">
Remove
</button>
</td>
</tr>`;
});
html += '</table>';
document.querySelector('#books-container').innerHTML = html;
const removeBook = document.querySelectorAll('.removeBtn');
[...removeBook].forEach((button) => {
button.addEventListener('click', (e) => {
Book.books.splice(e.target.id, 1);
Book.store(Book.books);
Book.displayBooks();
});
});
}
}
}
const addBookForm = document.querySelector('form');
const bookTitle = document.querySelector('#book-title');
const bookAuthor = document.querySelector('#book-author');
addBookForm.addEventListener('submit', (e) => {
e.preventDefault();
if (/\w/.test(bookTitle.value) && /\w/.test(bookAuthor.value)) {
const book = new Book(bookTitle.value, bookAuthor.value);
book.addBook();
}
Book.displayBooks();
});
const linkItem1 = document.querySelector('.link-Item1');
const linkItem2 = document.querySelector('.link-Item2');
const linkItem3 = document.querySelector('.link-Item3');
linkItem1.addEventListener('click', (e) => {
e.target.classList.add('text-green-400');
linkItem2.classList.remove('text-green-400');
linkItem3.classList.remove('text-green-400');
});
linkItem2.addEventListener('click', (e) => {
e.target.classList.add('text-green-400');
linkItem1.classList.remove('text-green-400');
linkItem3.classList.remove('text-green-400');
});
linkItem3.addEventListener('click', (e) => {
e.target.classList.add('text-green-400');
linkItem2.classList.remove('text-green-400');
linkItem1.classList.remove('text-green-400');
});
window.onload = () => {
const { DateTime } = luxon; // eslint-disable-line
const divTime = document.querySelector('#time');
const now = DateTime.now().toLocaleString(DateTime.DATETIME_MED);
divTime.textContent = now;
Book.displayBooks();
};