-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
84 lines (74 loc) · 2.02 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
const displayBook = document.querySelector('.display-books');
const form = document.querySelector('form');
class Books {
constructor() {
if (localStorage.getItem('books') === null) {
this.books = [];
} else {
this.books = JSON.parse(localStorage.getItem('books'));
}
}
// add book methods
addBook(book) {
this.books.push(book);
localStorage.setItem('books', JSON.stringify(this.books));
}
// remove book methods
removeBook(bookId) {
this.books = this.books.filter((item, index) => {
if (index !== bookId) {
return item;
}
return undefined;
});
localStorage.setItem('books', JSON.stringify(this.books));
}
}
// create an object
const listBooks = new Books();
function onPageReload() {
displayBook.innerHTML = listBooks.books.map((book, index) => `
<div class="display">
<p>"${book.title}" by ${book.author}</p>
<button class="remove-btn" onclick="removeBook(${index})">Remove</button>
</div>
`).join('');
if (listBooks.books.length === 0) {
displayBook.style.cssText = 'border: none;';
} else {
displayBook.style.cssText = 'border: 3px solid black;';
}
}
form.addEventListener('submit', (event) => {
event.preventDefault();
const formData = new FormData(form);
const title = formData.get('title');
const author = formData.get('author');
const bookObj = {
title,
author,
};
listBooks.addBook(bookObj);
onPageReload();
form.reset();
});
/* eslint-disable no-unused-vars */
const removeBook = (bookId) => {
listBooks.removeBook(bookId);
onPageReload();
};
/* eslint-disable no-unused-vars */
onPageReload();
document.querySelector('span').innerHTML = new Date();
const section = document.querySelectorAll('.section');
const addNew = document.querySelector('.list-books');
function makeActive(className) {
section.forEach((item) => {
if (item.classList.contains(className)) {
item.classList.add('show');
addNew.classList.toggle('show');
} else {
item.classList.remove('show');
}
});
}