-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
212 lines (162 loc) · 8.1 KB
/
app.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
"use strict";
const signIn = document.querySelector('.sign-in');
signIn.addEventListener('click', onSignIn)
function onSignIn() {
const googleUser = gapi.auth2.getAuthInstance().currentUser.get();
const profile = googleUser.getBasicProfile();
console.log('ID: ' + profile.getId()); // Do not send to your backend! Use an ID token instead.
console.log('Name: ' + profile.getName());
console.log('Image URL: ' + profile.getImageUrl());
console.log('Email: ' + profile.getEmail()); // This is null if the 'email' scope is not present.
}
console.log('hello');
(function () {
const form = document.querySelector('#searchForm');
const searchField = document.querySelector('#searchInput');
const responseContainer = document.querySelector('#responseContainer');
const backToTop = document.querySelector('.back-to-top')
form.addEventListener('submit', function (e) {
e.preventDefault();
const searchedForText = searchField.value;
/***
const sortOptions = Array.from(document.querySelectorAll('input[type="radio"]'));
console.log(sortOptions)
// OR using map and converting it to string using the join method
const sortBy = (sortOptions.map(sortOption => sortOption.checked ? sortOption.value : '')).join('');
console.log(sortBy)
*/
responseContainer.innerHTML = '<div id="loading"></div>';
// ROTATING LOADING CIRCLE
const loading = document.querySelector('#loading');
let increasing = true,
currentDegree = 0,
increment = 50;
function rotate() {
if (increasing) {
currentDegree += increment;
loading.style.transform = `rotate(${currentDegree}deg)`
}
}
setInterval(rotate, 100);
console.log(searchedForText)
// const booksAPI = `https://www.googleapis.com/books/v1/volumes?q=${searchedForText}&orderBy=${sortBy}&printType=all&maxResults=30&key=AIzaSyCGJTXSKXeWA2MByvqJvx2EZZ7BZB71FSE`;
const booksAPI = `https://www.googleapis.com/books/v1/volumes?q=${searchedForText}&orderBy=newest&printType=all&maxResults=30&key=AIzaSyCGJTXSKXeWA2MByvqJvx2EZZ7BZB71FSE`;
console.log(booksAPI)
fetch(booksAPI)
.then(data => data.json())
.then(addContent)
.catch(e => requestError(e))
/*** ADDING CONTENTS FOR DISPLAY ***/
function addContent(response) {
let htmlContent = '';
const books = response.items;
if (books) {
console.log(books)
// checking for initial search results and removing them
if (responseContainer.hasChildNodes()) {
responseContainer.firstElementChild.remove()
displayBooks(books)
} else {
displayBooks(books)
}
} else {
// WHEN THERE IS NO BOOKS
const errorMessage = `<p class="text-center error-message">Sorry.... There is no books for <em>${searchedForText}</em>. <em><strong><a href="https://www.google.com/search?tbm=bks&q=${searchedForText}" target="_blank">Search deeper</a></strong></em></p>`;
// checking for initial search results and removing them
if (responseContainer.hasChildNodes()) {
responseContainer.firstElementChild.remove()
htmlContent = errorMessage
} else {
htmlContent = errorMessage;
}
}
// TO DISPLAY BOOKS ON THE PAGE
function displayBooks(books) {
htmlContent = '<ul>' + (books.map(book =>
`<li>
<div class="card">
<figure>
<img src="${book.volumeInfo.imageLinks ? book.volumeInfo.imageLinks.thumbnail : ''}" class="card-img-top" alt="${searchedForText}">
<figcaption><strong>${book.volumeInfo.authors ? book.volumeInfo.authors : ''}</strong> <br> 📅 <em>${book.volumeInfo.publishedDate ? book.volumeInfo.publishedDate : ''}</em></figcaption>
</figure>
<div class="card-body">
<h3 class="card-title"><a href="${book.volumeInfo.previewLink}" target="_blank">${book.volumeInfo.title}</a></h3>
<h4 class="subtitle">${book.volumeInfo.subtitle ? book.volumeInfo.subtitle : searchedForText}</h4>
<p class="card-text">${book.searchInfo ? book.searchInfo.textSnippet : 'A books on ' + searchedForText}</p>
</div>
</div>
</li>`
).join('') +
`<p class="google-search">Couldn't find what you are looking for? <a href="https://www.google.com/search?tbm=bks&q=${searchedForText}" target="_blank">Search deeper</p>`)
+ '</ul>'
}
responseContainer.insertAdjacentHTML('afterbegin', htmlContent)
}
// WHEN REQUEST FAILS DUE TO NETWORK ERRORS
function requestError(e) {
const errorMessage = `<div class="text-center error-message">A <em><strong>${e.message}</strong></em> occured... Please, check internet connection and try again</div>`;
// console.dir(e, e.message)
console.dir(responseContainer)
console.log(responseContainer.hasChildNodes())
if (!responseContainer.hasChildNodes()) {
responseContainer.insertAdjacentHTML('afterbegin', errorMessage);
} else {
responseContainer.firstElementChild.remove();
responseContainer.insertAdjacentHTML('afterbegin', errorMessage);
}
}
})
// FOR THE BACK TO TOP BUTTON
window.addEventListener('scroll', () => (document.body.scrollTop > 100 || document.documentElement.scrollTop > 100) ? backToTop.classList.remove('hide') : backToTop.classList.add('hide'));
backToTop.addEventListener('click', () => {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
})
// HEADER
const header = document.querySelector('header');
let lastScrollTop = 0;
window.addEventListener('scroll', function () {
let currentPosition = document.documentElement.scrollTop;
// console.log(currentPosition)
if (currentPosition > lastScrollTop) {
header.style.display = 'none';
} else {
header.style.display = 'inline-block'
}
lastScrollTop = (currentPosition <= 0) ? 0 : currentPosition;
})
// SIDE (HAMBURGER) BAR
const hamburger = document.querySelector('.hamburger');
const closeButton = document.querySelector('.btn-close');
const mainContent = document.querySelector('main');
const sideMenu = document.querySelector('.side-nav');
// When the hamburger is clicked, we call openSideMenu
hamburger.addEventListener('click', openSideMenu);
// Opens sideMenu by increasing its width and bringing down the main content
function openSideMenu() {
sideMenu.style.width = '100%';
mainContent.style.marginTop = '280px';
// mainContent.style.marginTop = '300px';
}
closeButton.addEventListener('click', closeSideMenu);
function closeSideMenu() {
sideMenu.style.width = '0px';
mainContent.style.marginTop = '0px';
}
})()
// adding service worker
// check for service worker
// register serviceWorker
if ('servicwWorker' in navigator) {
window.addEventListener('load', async () => {
try {
const registration = await navigator.serviceWorker.register('index.sw.js');
console.log('service worker registered sucessfully!')
console.log(`registered with scope: ${registration.scope}`)
} catch (e) {
debugger;
console.log('service worker registratio failed');
console.log(e)
}
})
}