-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
83 lines (82 loc) · 2.4 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
//Global QuotesCollection Array to store API response
//Accessible accross functions
let QuotesCollection = [];
let Quote;
const quoteContainer = document.getElementById("quote");
/**
* function:: getQuotesFromAPI
* Use:: To Call API and get Quotes to display them on UI
* Author:: Ashish
**/
async function getQuotesFromAPI() {
const apiUrl = 'https://type.fit/api/quotes';
//Using Type fit API for Inspirational Quotes
try {
const response = await fetch(apiUrl);
//Storing the response in an Array
//Converting response stream to json
QuotesCollection = await response.json();
//Quotes fetched and stored
showQuotes();
}
catch (error){
}
}
/**
* function:: ShowQuotes
* Use:: To access DOM Element and show Quotes and there Auothor Name
* Author:: Ashish
*/
function showQuotes(){
//Generate Random Number bw 0 to 1600
Quote = QuotesCollection[Math.floor((Math.random() * QuotesCollection.length))];
//If Quote is fetched then show Quote else Alert User
if (Quote.text) {
//Remove the loader to show Quotes
removeLoader();
if( Quote.text.length > 35 ) {
//Change font size for Responsiveness on Mobile Devices
quoteContainer.classList.add('long-quote-text');
}
//Add the Quote to UI
quoteContainer.innerHTML = Quote.text;
//Add the Author Name to UI
if (Quote.author)
document.getElementById("author").innerHTML = Quote.author;
else
document.getElementById("author").innerHTML = 'Unknown!';
}
else {
alert("Unable to fetch Quotes from API!");
console.log(Quote);
}
}
/**
* function:: TweetQuote
* Use:: To tweet the Quote
* Author:: Ashish
*/
function TweetQuote(){
//Using Twitter API to tweet Quotes
const TwitterUrl = `https://twitter.com/intent/tweet?text=${Quote.text} - ${Quote.author}`;
window.open(TwitterUrl,'_blank');
}
/** function:: loader
* Use:: Show Loader till Quotes are fetched
* Author:: Ashish
*/
function loader(){
document.getElementById('quote-container').hidden = true;
}
/** function:: removeLoader
* Use:: Remove loader once Quotes are fetched
* Author:: Ashish
*
*/
function removeLoader(){
document.getElementById('quote-container').hidden = false;
document.getElementById('loader').hidden = true;
}
//On Load
loader();
getQuotesFromAPI();