-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
59 lines (51 loc) · 1.54 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
const container = document.getElementById('quote-coantainer');
const quote = document.getElementById('quote');
const twitter = document.getElementById('twitter');
const author = document.getElementById('author');
const newQuoteBtn = document.getElementById('new-quote');
const loader = document.getElementById('loader');
// Get quote from API
async function getQuote() {
loadingQuote();
const APIURL = 'https://api.quotable.io/random';
try {
const res = await fetch(APIURL);
const data = await res.json();
console.log(data);
quote.innerText = data.content;
if (data.content.length > 120) {
quote.classList.add('long-quote');
} else {
quote.classList.remove('long-quote');
}
author.innerText = data.author;
} catch (err) {
console.warn('No quote', err);
getQuote();
}
complete();
}
//Twitter btn funcion
function tweetQuote() {
const quoteText = quote.innerText;
const authorName = author.innerText;
const twitterUrl = `https://twitter.com/intent/tweet?text=${quoteText} - ${authorName}`;
window.open(twitterUrl, '_blank');
}
// Show loader
function loadingQuote() {
loader.hidden = false;
container.hidden = true;
}
// Hide loader when complete
function complete() {
if (!loader.hidden) {
container.hidden = false;
loader.hidden = true;
}
}
// Run funciotn on page Load
getQuote();
//Even Listeners
newQuoteBtn.addEventListener('click', getQuote);
twitter.addEventListener('click', tweetQuote);