-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
106 lines (83 loc) · 2.8 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
const popUpForm = document.getElementById('popUp');
const newBookButton = document.getElementById('newBtn');
const addBookButton = document.getElementById('addBtn');
const libraryDisplay = document.getElementById('list-container');
const closePopUP = document.getElementById('close');
addBookButton.addEventListener('click', addBookToMyLibrary);
newBookButton.addEventListener('click', () => popUpForm.style.display = 'block');
closePopUP.addEventListener('click', () => popUpForm.style.display = 'none');
let myLibrary = [];
class bookInfo {
constructor(title, author, pages, read) {
this.title = form.title.value;
this.author = form.author.value;
this.pages = form.pages.value;
this.read = form.read.checked;
}
}
function addBookToMyLibrary(e) {
e.preventDefault();
let newBook = new bookInfo(title, author, pages, read);
myLibrary.push(newBook);
displayBook();
form.reset();
setData();
}
function displayBook() {
const books = document.querySelectorAll('.book');
books.forEach(book => libraryDisplay.removeChild(book));
for (let i = 0; i < myLibrary.length; i++) {
createBook(myLibrary[i]);
}
}
function createBook(item) {
const bookDiv = document.createElement('div');
const titleDiv = document.createElement('div');
const authorDiv = document.createElement('div');
const pagesDiv = document.createElement('div');
const readButton = document.createElement('button');
const removeButton = document.createElement('button');
bookDiv.classList.add('book');
titleDiv.classList.add('title');
bookDiv.appendChild(titleDiv);
titleDiv.textContent = "Title: " + item.title;
authorDiv.classList.add('author');
bookDiv.appendChild(authorDiv);
authorDiv.textContent = "Author: " + item.author;
pagesDiv.classList.add('pages');
bookDiv.appendChild(pagesDiv);
pagesDiv.textContent = "Pages: " + item.pages;
readButton.classList.add('readButton');
bookDiv.appendChild(readButton);
if (item.read === false) {
readButton.textContent = 'Not Read';
readButton.style.backgroundColor = 'rgb(206, 38, 38)';
} else {
readButton.textContent = 'Read';
readButton.style.backgroundColor = 'rgb(48, 148, 91)';
}
readButton.addEventListener('click', () => {
item.read = !item.read;
displayBook();
setData();
})
removeButton.textContent = 'Remove';
removeButton.classList.add('removeButton');
bookDiv.appendChild(removeButton);
removeButton.addEventListener('click', () => {
myLibrary.splice(myLibrary.indexOf(item), 1);
displayBook();
setData();
})
libraryDisplay.appendChild(bookDiv);
}
if (!localStorage.myLibrary) {
displayBook();
} else {
let books = JSON.parse(localStorage.getItem('myLibrary'));
myLibrary = books;
displayBook();
}
function setData() {
localStorage.setItem('myLibrary', JSON.stringify(myLibrary));
}