-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
74 lines (64 loc) · 2.09 KB
/
script.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
const quoteTxt = document.querySelector('#quote');
const authorTxt = document.querySelector('#author');
const quoteBtn = document.querySelector('#new-quote');
const twitterBtn = document.querySelector('#twitter');
const loader = document.querySelector('#loader');
const quoteContainer = document.querySelector('#quote-container');
let quotes = [];
// show loading
function loading() {
loader.hidden = false;
quoteContainer.hidden = true;
}
// Hide Loading
function complete() {
quoteContainer.hidden = false;
loader.hidden = true;
}
// getQuotes from api
const fetchQuotes = async (url) => {
loading();
try {
const response = await fetch(url, { headers: { 'X-Api-Key': '/Wk5aTr/b1fqJWCmv11mcg==UObrUHGXSKCy64o0' } });
quotes = await response.json();
console.log(quotes[0]);
displayQuotes();
} catch (e) {
console.log(e);
}
};
// randommise quote to display
const randomQuotesIdentifier = (quotes) => {
const length = quotes.length;
const num = Math.round(Math.random() * length);
return num;
};
const displayQuotes = async () => {
loading();
//Display quote to the DOM
//const quoteObj = quotes[`${randomQuotesIdentifier(quotes)}`];
// Only a single quote is returned.
const quoteObj = quotes[0];
//console.log(quoteObj);
//if (quoteObj.text.length > 120) {
quoteTxt.classList.add('long-quote');
//} else {
// quoteTxt.classList.remove('long-quote');
//}
// set quote, hide loader
//quoteTxt.textContent = quoteObj.text;
quoteTxt.textContent = quoteObj.quote;
authorTxt.textContent = quoteObj.author;
complete();
//console.log(quotes[randomQuotesIdentifier(quotes)].text);
};
function tweetQuote() {
const twitterUrl = `https://twitter.com/intent/tweet?text=${quoteTxt.textContent} - ${authorTxt.textContent}`;
window.open(twitterUrl, '_blank');
}
//Onload
//new api link for learning purpose.
fetchQuotes('https://api.api-ninjas.com/v1/quotes');
//quoteBtn.addEventListener('click', displayQuotes);
quoteBtn.addEventListener('click', fetchQuotes.bind(null, 'https://api.api-ninjas.com/v1/quotes'));
twitterBtn.addEventListener('click', tweetQuote);