forked from ianistheguy/bookshelf-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
198 lines (163 loc) · 5.92 KB
/
script.js
File metadata and controls
198 lines (163 loc) · 5.92 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Book class
class Book {
constructor(title, author, pageNum, read = true) {
this.title = title;
this.author = author;
this.pageNum = pageNum;
this.read = read ? 'Yes' : 'No';
}
}
// DOM strings object with corresponding HTML/CSS IDs
const DOMstrings = {
form: '#form',
newBook: '#new-book',
idTitle: '#book-title',
idAuthor: '#book-author',
idPages: '#book-pages',
idRead: '#read',
submitType: '#submit',
cancelType: '#cancel'
}
// UI class
class UI {
// Show form when "Add a New Book" button is clicked
static showForm() {
document.querySelector(DOMstrings.newBook).addEventListener('click', () => {
let form = document.querySelector(DOMstrings.form);
form.style.display = 'block';
document.querySelector(DOMstrings.newBook).style.display = 'none';
})
}
// Hide form after submitting book info
static hideAfterSubmit() {
document.querySelector(DOMstrings.form).style.display = 'none';
document.querySelector(DOMstrings.newBook).style.display = 'block';
}
// Hide form by clicking "Cancel" button
static cancelForm() {
document.querySelector(DOMstrings.cancelType).addEventListener('click', () => {
let form = document.querySelector(DOMstrings.form);
form.style.display = 'none';
document.querySelector(DOMstrings.newBook).style.display = 'block';
})
}
// Display books in UI
static render() {
const library = Store.storeBooks();
library.forEach((book) => UI.addBooksToTable(book));
}
// Add books to UI table
static addBooksToTable(bookObj) {
const list = document.querySelector('.book-list');
let row = document.createElement('tr');
row.innerHTML = `
<td><em>${bookObj.title}</em></td>
<td>${bookObj.author}</td>
<td>${bookObj.pageNum}</td>
<td><button class="change-status">${bookObj.read}</button></td>
<td><button class="remove-book">Remove</button></td>
`
list.appendChild(row);
}
// Delete books from UI table
static deleteBooks(el) {
if (el.classList.contains('remove-book')) {
el.parentElement.parentElement.remove();
}
}
// Clear input fields
static clearFields() {
document.querySelector(DOMstrings.idTitle).value = '';
document.querySelector(DOMstrings.idAuthor).value = '';
document.querySelector(DOMstrings.idPages).value = '';
document.querySelector(DOMstrings.idRead).checked === false;
document.querySelector(DOMstrings.idTitle).focus();
}
}
// Storage class
class Store {
static storeBooks() {
let bookShelf;
if(localStorage.getItem('bookShelf') === null) {
bookShelf = [];
} else {
bookShelf = JSON.parse(localStorage.getItem('bookShelf'));
}
return bookShelf;
}
static addBookToLibrary(bookObj) {
const books = Store.storeBooks();
books.push(bookObj);
localStorage.setItem('bookShelf', JSON.stringify(books));
}
static removeBook(read) {
const books = Store.storeBooks();
books.forEach((bookObj, index) => {
if (bookObj.read === read) {
books.splice(index, 1);
}
})
localStorage.setItem('bookShelf', JSON.stringify(books));
}
static changeReadStatus(read, title) {
const books = JSON.parse(localStorage.getItem('bookShelf'));
books.forEach(book => {
if (book.title === title) {
book.read = read;
}
})
localStorage.setItem('bookShelf', JSON.stringify(books));
}
}
UI.showForm();
UI.cancelForm();
// Event to display books in UI table
document.addEventListener('DOMContentLoaded', UI.render);
// Event to add Book
document.querySelector(DOMstrings.form).addEventListener('submit', (e) => {
// Get input values from form
const title = document.querySelector(DOMstrings.idTitle).value;
const author = document.querySelector(DOMstrings.idAuthor).value;
const pageNum = document.querySelector(DOMstrings.idPages).value;
const read = document.querySelector(DOMstrings.idRead).checked;
// Validate form input values
if (title === '' || author === '' || pageNum === '') {
alert('Please fill in all the fields!')
} else {
// Instantiate Books
const book = new Book(title, author, pageNum, read);
// Add books to UI
UI.addBooksToTable(book);
// Add books to local storage
Store.addBookToLibrary(book);
// Clear fields
UI.clearFields();
/*
// Form will close
UI.hideAfterSubmit();
*/
}
// Prevent actual default
e.preventDefault();
})
// Event to remove Book
document.querySelector('.book-list').addEventListener('click', (e) => {
if (e.target.className === 'remove-book') {
// Remove book from UI
UI.deleteBooks(e.target);
// Remove book from storage
Store.removeBook(e.target.parentElement.previousElementSibling.textContent);
}
})
// Event to change Read Status
document.querySelector('.book-list').addEventListener('click', (e) => {
if (e.target.className === 'change-status') {
if (e.target.textContent === 'Yes') {
e.target.textContent = 'No';
Store.changeReadStatus(e.target.textContent, e.target.parentElement.parentElement.children[0].textContent);
} else {
e.target.textContent = 'Yes';
Store.changeReadStatus(e.target.textContent, e.target.parentElement.parentElement.children[0].textContent);
}
}
})