-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
390 lines (288 loc) · 11.2 KB
/
Copy pathscript.js
File metadata and controls
390 lines (288 loc) · 11.2 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//APIKEY YaX4wGwjQYBAP1KnRbq7VSTeTbypkxM5
/*
Al cargar la web deben de aparecer todas las listas con los siguientes datos:
1-Nombre completo de la lista
2-Fecha del libro más antiguo en la lista
3-Fecha del último libro incorporado
4-Frecuencia de actualización
5-Link para poder cargar la lista */
//https://api.nytimes.com/svc/books/v3/lists/names.json 1-4
//https://api.nytimes.com/svc/books/v3/lists.json 5? query list (required)
const urlLists = 'https://api.nytimes.com/svc/books/v3/lists/names.json?api-key=YaX4wGwjQYBAP1KnRbq7VSTeTbypkxM5'
const fetcLists = async () => {
try {
const response = await fetch(urlLists).then(res => res.json())
document.querySelector('#container_loader').style.display = 'none'
console.log(response)
printLists(response.results)
} catch (error) {
console.log("Error",error)
}
}
fetcLists();
async function setList(event) {
let list
event.preventDefault();
let selected = event.target.value;
list = selected;
const urlType = `https://api.nytimes.com/svc/books/v3/lists/current/${list}.json?api-key=YaX4wGwjQYBAP1KnRbq7VSTeTbypkxM5`
fetcListType(urlType)
document.querySelector('#dashboard').style.display = 'none'
document.querySelector('#list').style.display = 'flex'
document.querySelector('#btn_back').style.display = 'block'
document.querySelector('#h3_files').style.display = 'none'
document.querySelector('#files').style.display = 'none'
/* document.querySelector('#fav_list').style.display = 'none'
document.querySelector('#btn_back_fav').style.display = 'none' */
}
const fetcListType = async (urlType) => {
document.querySelector('#container_loader').style.display = 'flex'
try {
const response = await fetch(urlType).then(res => res.json())
document.querySelector('#container_loader').style.display = 'none'
printListType(response.results.books)
} catch (error) {
console.log("Error",error)
}
}
async function printLists(data) {
for (let i = 0; i < data.length; i++) {
document.querySelector('#dashboard').innerHTML += ` <div class="list_card">
<h3>${data[i].list_name}</h3>
<p>OLDEST: ${data[i].oldest_published_date}</p>
<p>NEWEST: ${data[i].newest_published_date}</p>
<p>UPDATED: ${data[i].updated}</p>
<button value="${data[i].list_name_encoded}" onclick="setList(event)">READ MORE!</button>
</div>`
}
}
async function printListType(data) {
for (let i = 0; i < data.length; i++) {
let valueFav = [data[i].book_image, data[i].rank, data[i].title, data[i].weeks_on_list, data[i].description, data[i].amazon_product_url];
//console.log(valueFav);
document.querySelector('#list').innerHTML += `<div class="list_card books">
<img src="${data[i].book_image}" alt="">
<h3>#${data[i].rank} ${data[i].title}</h3>
<p id="weeks"> Weeks on list: ${data[i].weeks_on_list}</p>
<p id="desc">${data[i].description}</p>
<a href="${data[i].amazon_product_url}" class="amazon_link">BUY AT AMAZON</a>
<button class="add_fav" id="btn_fav${[i]}" onclick=" addFav(event)" disabled>Add to favorites</button>
</div>`
}
auth.onAuthStateChanged(user => {
if(user){
//console.log('auth: sign in' ,user.email);
document.querySelector('#fav').style.display = 'flex';
document.querySelector('#pic').style.display = 'flex';
document.querySelector('#pic').src = user.photoURL;
const btnFav = document.querySelectorAll('.add_fav');
btnFav.forEach(btn => {
// btn.removeAttribute("disabled");
btn.disabled = false;
})
}else{
//console.log('log out');
document.querySelector('#fav').style.display = 'none';
const btnFav = document.querySelectorAll('.add_fav');
btnFav.forEach(btn => {
//btn.setAttribute("disabled","");
btn.disabled = true;
})
}})
}
async function backToIndex() {
document.querySelector('#dashboard').style.display = 'flex';
document.querySelector('#list').innerHTML = `<div id="list"></div>`;
document.querySelector('#btn_back').style.display = 'none';
//document.querySelector('#fav_list').style.display = 'none'
//document.querySelector('#btn_back_fav').style.display = 'none'
}
//Log links
const openS = document.getElementById('openS');
const openI = document.getElementById('openI');
const signForm = document.getElementById('sign-form');
const loginForm = document.getElementById('login-form');
const outForm = document.getElementById('out-form');
const closeS = document.getElementById('closeS');
const closeI = document.getElementById('closeI');
openS.addEventListener('click', (e) => {
e.preventDefault();
signForm.classList.add('show');
});
openI.addEventListener('click', (e) => {
e.preventDefault();
loginForm.classList.add('show');
});
closeS.addEventListener('click', (e) => {
e.preventDefault();
signForm.classList.remove('show');
});
closeI.addEventListener('click', (e) => {
e.preventDefault();
loginForm.classList.remove('show');
});
//FIREBASE
const firebaseConfig = {
apiKey: "AIzaSyAl3mBzF1JrIegDkaZCM4ISAN2OvVCalT8",
authDomain: "nyt-library-7b667.firebaseapp.com",
projectId: "nyt-library-7b667",
storageBucket: "nyt-library-7b667.appspot.com",
messagingSenderId: "1061177010377",
appId: "1:1061177010377:web:1cfbd338b94b65b19ac518"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
// Initialize Cloud Firestore and get a reference to the service
const db = firebase.firestore();
// Initialize Firebase Authentication and get a reference to the service
const auth = firebase.auth();
//Sign In
const signInForm = document.querySelector('#sign-form');
signInForm.addEventListener("submit", (e) => {
e.preventDefault();
const email = document.querySelector("#sign-email").value;
const password = document.querySelector("#sign-pass").value;
auth
.createUserWithEmailAndPassword(email, password)
.then(userCredential => {
signInForm.reset();
alert('Congratulations! You have successfully signed up ')
//console.log('sign in');
})
})
//Log In
const logInForm = document.querySelector("#login-form");
logInForm.addEventListener("submit", (e)=> {
e.preventDefault();
const email = document.querySelector("#login-email").value;
const password = document.querySelector("#login-pass").value;
auth
.signInWithEmailAndPassword(email, password)
.then(userCredential => {
signInForm.reset();
alert('You are logged in')
//console.log('log in');
})
})
//Log Out
const logOut = document.querySelector("#log_out");
logOut.addEventListener("click", e => {
e.preventDefault();
auth
.signOut()
.then(() => {
//console.log('log out');
alert('You have successfully logged out')
document.querySelector('#pic').style.display = 'none'
})
})
const user = firebase.auth().currentUser;
//Add favorites
function addFav(event) {
//console.log(event);
event.preventDefault();
const btn = event.target
btn.style.visibility = 'hidden';
auth.onAuthStateChanged(user => {
if(user){
if (event.target.classList.contains('add_fav')) {
setFav(event.target.parentElement);
//console.log(event.target.parentElement);
}
}
})
}
function setFav(obj) {
const book ={
img: obj.querySelector("img").src,
rank_title: obj.querySelector("h3").textContent,
weeks: obj.querySelector("#weeks").textContent,
desc: obj.querySelector("#desc").textContent,
amazon: obj.querySelector("a").href,
}
auth.onAuthStateChanged(user => {
if(user){
//console.log('auth: sign in');
db.collection("users").add({
user: user.email,
book: book,
})
.then((docRef) => {
//console.log("Document written with ID: ", docRef.id);
})
.catch((error) => {
//console.error("Error adding document: ", error);
});
}else{
alert('Log in to save your favorites')
}
})
//console.log(book);
}
async function showFav() {
document.querySelector('#dashboard').style.display = 'none';
document.querySelector('#list').style.display = 'none';
document.querySelector('#btn_back').style.display = 'none';
document.querySelector('#fav_list').style.display = 'flex';
document.querySelector('#h3_files').style.display = 'flex';
document.querySelector('#files').style.display = 'flex';
auth.onAuthStateChanged(user => {
if(user){
db.collection("users").where("user", "==", user.email)
.get()
.then((querySnapshot) => {querySnapshot.forEach((doc) => {
//console.log(`${doc.id} => ${doc.data().book.amazon}`);
document.querySelector('#fav_list').innerHTML += `<div class="list_card books">
<img src="${doc.data().book.img}" alt="">
<h3>${doc.data().book.rank_title}</h3>
<p id="weeks"> Weeks on list: ${doc.data().book.weeks}</p>
<p id="desc">${doc.data().book.desc}</p>
<a class="amazon_link" href="${doc.data().book.amazon}">BUY AT AMAZON</a>
</div>`
});
});
}
})
}
document.querySelector('#files').addEventListener('change',uploadFile)
//function to save file
function uploadFile() {
// Created a Storage Reference with root dir
var storage = firebase.storage();
var storageRef = storage.ref();
// Get the file from DOM
var file = document.getElementById("files").files[0];
//console.log(file);
//dynamically set reference to the file name
var thisRef = storageRef.child(file.name);
//put request upload file to firebase storage
thisRef.put(file)
.then(function (snapshot) {
alert("File Uploaded")
getFileUrl(file.name) ;
});
}
// Return URL of a certain image
function getFileUrl(filename) {
//create a storage reference
var storage = firebase.storage().ref();
//get file url
storage.child(filename)
.getDownloadURL()
.then(function (url) {
//console.log(url);
document.querySelector('#pic').src = url
const user = firebase.auth().currentUser;
user.updateProfile({
photoURL: url
}).then(() => {
//console.log('Update successful');
// ...
}).catch((error) => {
console.log(error);
});
})
.catch(function (error) {
console.log("error encountered");
});
}