-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.js
218 lines (205 loc) · 7.38 KB
/
library.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
document.addEventListener('DOMContentLoaded', () => {
// Expand and Center new book form
document.querySelector('#add .btn').addEventListener('click', () => {
document.getElementById('add').classList.toggle('center');
});
initialBooks();
});
// Function to restore initial books on DOM refresh
const initialBooks = async () => {
// If localstorage is set
if (!localStorage.getItem('isAuth')) return;
const data = JSON.parse(localStorage.getItem('isAuth'));
// If localstorage is set and contains books
if (Object.keys(data.library).length === 0) return;
// For each book add the book info
for (const [key, value] of Object.entries(data.library)) {
await addBook(value.isbn, value.title, value.author, value.didRead);
}
};
// On Form Submit
const addBookToLibrary = async () => {
const form = document.getElementById('newbook');
const isbn = document.getElementById('isbn').value;
const title = document.getElementById('title').value;
const author = document.getElementById('author').value;
const didRead = document.getElementById('read').checked ? 1 : 0;
if (localStorage.getItem('isAuth')) {
const data = JSON.parse(localStorage.getItem('isAuth'));
data.collection = data.collection + 1;
data.read = data.read + didRead;
data.library[`${parseInt(isbn)}`] = { isbn, title, author, didRead };
localStorage.setItem('isAuth', JSON.stringify(data));
} else {
isAuth.collection = isAuth.collection + 1;
isAuth.read = isAuth.read + didRead;
isAuth.library[`${parseInt(isbn)}`] = { isbn, title, author, didRead };
localStorage.setItem('isAuth', JSON.stringify(isAuth));
}
// Add book to container
addBook(isbn, title, author, didRead);
// Reset Form
form.reset();
};
const addBook = async (isbn, title, author, didRead) => {
// Remove FAQ if exists
const faq = document.getElementById('reminder');
if (faq) faq.remove();
// Create Element
const el = document.createElement('div');
el.id = `id_${isbn}`;
el.classList.add('bookcard');
el.classList.add(didRead ? 'read' : 'notread');
// Book Volume Details
let volumeInfo = {};
let url = `https://www.googleapis.com/books/v1/volumes?q=isbn:${isbn}`;
let res = await fetch(url);
let json = await res.json();
if (json.totalItems != 0) {
try {
volumeInfo = json.items[0].volumeInfo;
volumeInfo.thumb = volumeInfo.imageLinks.thumbnail.replace(
'http',
'https'
);
} catch {
console.log('Invalid Book ISBN found.');
}
}
if (!volumeInfo.thumb) {
url = `https://www.googleapis.com/books/v1/volumes?q=intitle:${title}`;
res = await fetch(url.replace(' ', '+'));
json = await res.json();
if (json.totalItems != 0) {
try {
volumeInfo = json.items[0].volumeInfo;
volumeInfo.thumb = volumeInfo.imageLinks.thumbnail.replace(
'http',
'https'
);
} catch {
console.log('Invalid Book ISBN found.');
}
}
if (!volumeInfo.thumb) {
// Fallback image
await storage
.ref('assets/book-default.png')
.getDownloadURL()
.then((url) => {
volumeInfo.thumb = url;
})
.catch((err) => {
console.log('Default Book Thumbnail not found!');
});
}
}
// Insert HTML
el.innerHTML = `
<div class="contentWrapper">
<img class="pe-none user-select-none" loading="lazy" width="128px" alt="${title}" src="${
volumeInfo.thumb
}" />
<div class="cardContent">
<div class="title">${title}</div>
<div class="author">by ${author}</div>
${
volumeInfo.description
? `<div class="desc">${volumeInfo.description.slice(
0,
200
)}...</div>`
: ''
}
</div>
</div>
<div class="more d-flex gap-sm justify-content-space-between align-items-center">
<div class="d-flex flex-column">
<span>ISBN: ${isbn}</span>
${
volumeInfo.pageCount
? `<span>Pages: ${volumeInfo.pageCount}</span>`
: ''
}
<span class="metastatus">Status: ${
didRead === 1 ? 'Read' : 'Not read'
}</span>
</div>
<div class="actions">
<button class="btn status secondary" onclick="changeReadStatus(${isbn})">${
didRead === 1 ? 'Mark as Unread' : 'Mark as Read'
}</button>
<button class="btn delete bordered" onclick="removeBook(${isbn})">Remove</button>
</div>
</div>
`;
// Append Element
document.getElementById('books').appendChild(el);
};
// Open delete confirmation modal
const removeBook = (isbn) => {
if (document.querySelector('.overlay'))
document.querySelector('.overlay').remove();
const modal = document.createElement('div');
modal.classList.add('overlay');
modal.innerHTML = `
<div id="modal" class="delete">
<div class="header d-flex flex-column gap-sm">
<h2>Are you sure?</h2>
<span>You are about to delete book with ISBN: ${isbn} from your collection.</span>
</div>
<div class="d-flex gap-sm align-items-center w100">
<button class="confirm" onclick="confirmRemove(${isbn})">Continue</button>
<button onclick="cancelRemove()">Cancel</button>
</div>
</div>
`;
// Insert Modal
document.body.appendChild(modal);
};
const confirmRemove = (isbn) => {
if (document.querySelector('.overlay'))
document.querySelector('.overlay').remove();
// Remove from localStorage
if (!localStorage.getItem('isAuth')) return;
const data = JSON.parse(localStorage.getItem('isAuth'));
data.collection = data.collection - 1;
if (data.library[`${isbn}`].didRead) data.read = data.read - 1;
delete data.library[`${isbn}`];
localStorage.setItem('isAuth', JSON.stringify(data));
// Remove Book from container
document.getElementById(`id_${isbn}`).remove();
};
const cancelRemove = () => {
if (document.querySelector('.overlay'))
document.querySelector('.overlay').remove();
};
// Changes Read status of books
const changeReadStatus = async (isbn) => {
if (!localStorage.getItem('isAuth')) return;
const data = JSON.parse(localStorage.getItem('isAuth'));
const didRead = data.library[`${isbn}`].didRead;
if (didRead) {
// Change Book's read status
data.library[`${isbn}`].didRead = 0;
// Update User's read count
data.read = data.read - 1;
// Update Book's Card visuals
document.getElementById(`id_${isbn}`).classList.remove('read');
document.getElementById(`id_${isbn}`).classList.add('notread');
// Update Action Button's text and Meta info status
document.querySelector(`#id_${isbn} .metastatus`).innerText =
'Status: Not read';
document.querySelector(`#id_${isbn} .status`).innerText = 'Mark as Read';
} else {
data.library[`${isbn}`].didRead = 1;
data.read = data.read + 1;
document.getElementById(`id_${isbn}`).classList.remove('notread');
document.getElementById(`id_${isbn}`).classList.add('read');
document.querySelector(`#id_${isbn} .metastatus`).innerText =
'Status: Read';
document.querySelector(`#id_${isbn} .status`).innerText = 'Mark as Unread';
}
// Update localStorage
localStorage.setItem('isAuth', JSON.stringify(data));
};