-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
151 lines (121 loc) · 4.27 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//button event listeners for create new book, add new book to page, close popup
const addBtn = document.querySelector('#addBtn');
addBtn.addEventListener('click', addBookToLibrary);
const newBookBtn = document.querySelector('#newBtn');
newBookBtn.addEventListener('click', () => popUpForm.style.display = 'block');
const popUpForm = document.getElementById('popUp');
const closePopUp = document.getElementsByTagName('span')[0];
closePopUp.addEventListener('click', () => popUpForm.style.display = 'none');
const form = document.querySelector('#form');
//Book Constructor
class Book {
constructor(title, author, pages, read) {
this.title = title
this.author = author
this.pages = pages
this.read = read
}
}
//creates book from Book Constructor, adds to library
let myLibrary = [];
let newBook;
function addBookToLibrary(event) {
event.preventDefault();
let titleInput = document.querySelector("#title");
if (!titleInput.value) {
titleInput.setCustomValidity("Enter a valid title");
titleInput.reportValidity();
return;
}
let authorInput = document.querySelector("#author");
if (!authorInput.value) {
authorInput.setCustomValidity("Enter a valid author");
authorInput.reportValidity();
return;
}
let pageInput = document.querySelector("#pages");
if (!pageInput.value) {
pageInput.setCustomValidity("Enter a valid number");
pageInput.reportValidity();
return;
}
let title = form.title.value;
let author = form.author.value;
let pages = form.pages.value;
let read = form.read.checked;
newBook = new Book(title, author, pages, read);
myLibrary.push(newBook);
setData(); //saves updated array in local storage
render();
form.reset();
};
//Creates book visual in browser
function render() {
const display = document.getElementById('Library-Container');
const books = document.querySelectorAll('.book');
books.forEach(book => display.removeChild(book));
for (let i=0; i<myLibrary.length; i++){
createBook(myLibrary[i]);
}
}
//creates book DOM elements, to use in render();
function createBook(item) {
const library = document.querySelector('#Library-Container');
const bookDiv = document.createElement('div');
const titleDiv = document.createElement('div');
const authDiv = document.createElement('div');
const pageDiv = document.createElement('div');
const removeBtn = document.createElement('button');
const readBtn = document.createElement('button');
bookDiv.classList.add('book');
bookDiv.setAttribute('id', myLibrary.indexOf(item));
titleDiv.textContent = item.title;
titleDiv.classList.add('title');
bookDiv.appendChild(titleDiv);
authDiv.textContent = item.author;
authDiv.classList.add('author');
bookDiv.appendChild(authDiv);
pageDiv.textContent = item.pages;
pageDiv.classList.add('pages');
bookDiv.appendChild(pageDiv);
readBtn.classList.add('readBtn');
bookDiv.appendChild(readBtn);
if(item.read===false) {
readBtn.textContent = 'Not Read';
readBtn.style.backgroundColor = '#e04f63';
}else {
readBtn.textContent = 'Read';
readBtn.style.backgroundColor = '#63da63'
}
removeBtn.textContent = 'Remove';
removeBtn.setAttribute('id', 'removeBtn');
bookDiv.appendChild(removeBtn);
library.appendChild(bookDiv);
removeBtn.addEventListener('click', () => {
myLibrary.splice(myLibrary.indexOf(item),1);
setData();
render();
});
//add toggle ability to each book 'read' button on click
readBtn.addEventListener('click', () => {
item.read = !item.read;
setData();
render();
});
};
// setting Library to be stored in local storage
function setData() {
localStorage.setItem(`myLibrary`, JSON.stringify(myLibrary));
}
//pulls books from local storage when page is refreshed
function restore() {
if(!localStorage.myLibrary) {
render();
}else {
let objects = localStorage.getItem('myLibrary') // gets information from local storage to use in below loop to create DOM/display
objects = JSON.parse(objects);
myLibrary = objects;
render();
}
}
restore();