-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
131 lines (100 loc) · 3.54 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
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
const quoteContainer = document.getElementById('quote-container');
const twitterButton = document.getElementById('twitter');
const quoteAuthor = document.getElementById('author');
const quoteText = document.getElementById('quote');
const quoteButton = document.getElementById('new-quote');
const random = (max) => Math.floor(Math.random() * max);
const loader = document.getElementById('loader');
let apiArray = [];
function showLoadingSpinner() {
loader.hidden = false;
quoteContainer.hidden = true;
}
function hideLoadingSpinner() {
quoteContainer.hidden = false;
loader.hidden = true;
}
//Retrieve new Quote
function newQuote() {
showLoadingSpinner();
try {
let quote = apiArray[random(apiArray.length)];
//Set Quote Text
quoteText.textContent = quote.text;
//Check to see if author is null & display Unkown or author
if (!quote.author) {
quoteAuthor.textContent = 'Unknown';
} else {
quoteAuthor.textContent = quote.author;
}
//Check quote length to determine styling
if (quote.text.length > 10) {
quoteText.classList.add('quote-container__text--long');
} else {
quoteText.classList.remove('quote-container__text--long');
}
//Hide Loader
} catch (e){
alert("Oops! Please reload the page.");
quoteText.textContent = 'Please reload the page.';
quoteAuthor.textContent = 'Quote Generator';
}
hideLoadingSpinner();
}
async function getData() {
showLoadingSpinner();
const apiUrl = 'https://type.fit/api/quotes';
try {
const response = await fetch(apiUrl);
apiArray = await response.json();
newQuote();
} catch (e) {
alert("Oops! Please reload the page.");
quoteText.textContent = 'Please reload the page.';
quoteAuthor.textContent = 'Quote Generator';
}
}
//Tweet Quote
function tweetQuote() {
const twitterUrl = `https://twitter.com/intent/tweet?text=${quoteText.textContent} - ${quoteAuthor.textContent}`;
window.open(twitterUrl, '_blank');
}
//Event Listeners
quoteButton.addEventListener('click', newQuote);
twitterButton.addEventListener('click', tweetQuote);
getData();
// // Get Data FROM API THIS EVERYTHING IN ONE FUNCTIO INCLUDING GETTING THE NEW QUOTE
// async function getData() {
// try {
// const random = (max) => Math.floor(Math.random() * max);
// const response = await fetch('https://type.fit/api/quotes');
// apiArray = await response.json();
// let i = await random(apiArray.length);
// let data = await Object.values(apiArray[i]);
// quoteAuthor.textContent = data[1];
// quoteText.textContent = data[0];
// } catch (e) {
// alert(e)
// }
// }
// //------ONLOAD----------
// getData();
// // Get Data FROM API ----TEST FUNCTION RETRIEVING VALUES
// async function getData(x) {
// const random = (min, max) => Math.floor(Math.random() * (max - min)) + min;
// let apiURL = x;
// let apiArray = [];
// try {
// const response = await fetch(apiURL);
// apiArray = await response.json();
// let i = await random(0, apiArray.length);
// //-------return single object--------
// let data = await Object.values(apiArray[i]);
// //-------destructure and update state below------
// console.log(String(data))
// } catch (e) {
// alert(e)
// }
// }
// //------ONLOAD----------
// getData('https://type.fit/api/quotes')