-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
295 lines (232 loc) · 7.5 KB
/
main.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
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
const currentReadArea = document.querySelector('#current-read-area')
const stackArea = document.querySelector('#stack-area')
const shelfArea = document.querySelector('#shelf-area')
const addBtn = document.querySelector('#add-book-btn')
const addBookModal = document.querySelector('#add-book-modal')
const exitBtn = document.querySelector('#add-modal-exit-btn')
const bookTitle = document.querySelector('#book_title')
const bookAuthor = document.querySelector('#book_author')
const bookSeries = document.querySelector('#book_series')
const bookLocationBtns = document.querySelectorAll('input[name="location"]')
const addForm = document.querySelector('#add-book-form')
const bookCards = document.querySelectorAll('.book-card')
const app = document.querySelector('#app')
addBtn.addEventListener('click', () => {
clearForm()
addBookModal.showModal()
})
exitBtn.addEventListener('click', () => {
addBookModal.close()
})
const generateID = () => {
return new Date().getTime()
}
addForm.addEventListener("submit", (e) => {
e.preventDefault()
let bookLocation
for (const bookLocationBtn of bookLocationBtns) {
if (bookLocationBtn.checked) {
bookLocation = bookLocationBtn.value
}
}
const newBookId = generateID()
const newBook = new Book(
newBookId,
bookTitle.value,
bookAuthor.value,
bookSeries.value,
bookLocation
)
library.addBook(newBook)
library.saveLibraryToLocalStorage()
genBooks()
addBookModal.close()
console.log(library)
})
class Book {
constructor (id, title, author, series, location){
this.bookId = id
this.title = title
this.author = author
this.series = series
this.location = location
}
}
class Library {
constructor() {
this.library = []
}
addBook(newBook) {
this.library.push(newBook)
}
editBook(index, updatedTitle, updatedAuthor, updatedSeries, updatedLocation) {
const book = this.library[index]
book.title = updatedTitle
book.author = updatedAuthor
book.series = updatedSeries
book.location = updatedLocation
console.log(`Editting Book: ${book.bookId}`)
}
changeBookLocation(index, newLocation) {
const book = this.library[index]
book.location = newLocation
}
getBook(index) {
return this.library[index]
}
getLibrary() {
return this.library
}
saveLibraryToLocalStorage() {
localStorage.setItem('library', JSON.stringify(this.library))
console.log('Save Successful')
}
loadLibraryFromLocalStorage(){
const libraryData = localStorage.getItem('library')
if (libraryData) {
this.library = JSON.parse(libraryData)
console.log('Retrieval Successful')
}
}
}
const library = new Library()
const clearForm = () => {
bookTitle.value = ""
bookAuthor.value = ""
bookSeries.value = ""
}
const createBookCard = (book) => {
console.log(`Created Book ${book.title} at ${book.location}`)
const bookCard = document.createElement('div')
const bookTextInfo = document.createElement('div')
const title = document.createElement('div')
const author = document.createElement('div')
const series = document.createElement('div')
bookCard.classList.add('book-card')
bookTextInfo.classList.add('book-text-info')
title.classList.add('title')
author.classList.add('author')
series.classList.add('series')
console.log(`Book ID: ${book.bookId}`);
title.textContent = `${book.title}`
author.textContent = `${book.author}`
series.textContent = `${book.series}`
bookCard.setAttribute('data-book-id', book.bookId)
console.log(`Data Book ID Attribute: ${bookCard.getAttribute('data-book-id')}`);
bookCard.appendChild(bookTextInfo)
bookTextInfo.appendChild(title)
bookTextInfo.appendChild(author)
bookTextInfo.appendChild(series)
switch (book.location){
case "shelf-area":
shelfArea.appendChild(bookCard)
bookCard.classList.add('completed')
bookTextInfo.classList.add('shelved')
break
case "current-read-area":
currentReadArea.appendChild(bookCard)
bookCard.classList.add('current-read')
break
default:
stackArea.appendChild(bookCard)
}
}
const bookCardListener = (bookCard) => {
console.log(`Clicked: ${bookCard.dataset.bookId}`)
const clickedBookId = bookCard.dataset.bookId
const libraryData = JSON.parse(localStorage.getItem('library'))
const bookIndex = libraryData.findIndex((book) => book.bookId == clickedBookId)
console.log(libraryData)
if (bookIndex != -1) {
populateEditBookForm(bookIndex)
return index = bookIndex
} else {
console.log('Book Not Found in Local Storage')
}
}
const applyEventListeners = () => {
const bookCardArray = [...document.querySelectorAll(".book-card")]
console.log("Applying Event Listeners")
bookCardArray.forEach((bookCard) => {
console.log(`Applying Event Listener to: ${bookCard.dataset.bookId}`)
bookCard.addEventListener('click', () => {
bookCardListener(bookCard),
{once: true}
})
})
}
const genBooks = () => {
console.log("Running genBooks")
library.loadLibraryFromLocalStorage()
stackArea.innerHTML = ''
currentReadArea.innerHTML = ''
shelfArea.innerHTML = ''
library.library.forEach((bookData) => {
const newBook = new Book(
bookData.bookId,
bookData.title,
bookData.author,
bookData.series,
bookData.location
)
createBookCard(newBook)
})
applyEventListeners()
}
const bookTitleEdit = document.querySelector('#book_title_edit')
const bookAuthorEdit = document.querySelector('#book_author_edit')
const bookSeriesEdit = document.querySelector('#book_series_edit')
const stackBtnEdit = document.querySelector('#stack-btn-edit')
const currentBtnEdit = document.querySelector('#current-btn-edit')
const shelfBtnEdit = document.querySelector('#shelf-btn-edit')
const editForm = document.querySelector('#edit-book-form')
const editBookModal = document.querySelector('#edit-book-modal')
const editExitBtn = document.querySelector('#edit-modal-exit-btn')
const editBookLocationBtns = document.querySelectorAll('input[name="edit-location"]')
let index
editForm.addEventListener('submit', (e) => submitEditForm(e, index))
editExitBtn.addEventListener('click', () => {
editBookModal.close()
})
const clearEditForm = () => {
console.log("Clearing Edit Form")
bookTitleEdit.value = ""
bookAuthorEdit.value = ""
bookSeriesEdit.value = ""
}
const populateEditBookForm = (index) => {
clearEditForm()
console.log("Populating Edit Book Form")
const bookToEdit = library.getBook(index)
console.log(bookToEdit)
bookTitleEdit.value = bookToEdit.title
bookAuthorEdit.value = bookToEdit.author
bookSeriesEdit.value = bookToEdit.series
for(const editBookLocationBtn of editBookLocationBtns) {
if(bookToEdit.location === editBookLocationBtn.value){
editBookLocationBtn.checked = true
}
}
// editForm.removeEventListener('submit', (e) => submitEditForm(e,index));
editBookModal.showModal()
return index
}
const submitEditForm = (e, index) => {
editBookModal.close()
e.preventDefault()
e.stopPropagation()
const updatedTitle = bookTitleEdit.value
const updatedAuthor = bookAuthorEdit.value
const updatedSeries = bookSeriesEdit.value
let updatedLocation = ""
for (const editBookLocationBtn of editBookLocationBtns) {
if (editBookLocationBtn.checked) {
updatedLocation = editBookLocationBtn.value
}
}
library.editBook(index, updatedTitle, updatedAuthor, updatedSeries, updatedLocation)
library.saveLibraryToLocalStorage()
console.log("Edit Form Submitted")
genBooks()
}
genBooks()