-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathquoteAPI.js
169 lines (140 loc) · 4.8 KB
/
quoteAPI.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
let quoteContainer = document.querySelector('#quotes'),
pageChangeArrows = document.querySelector('#page-change-arrows'),
searchButton = document.querySelector('#search-button'),
searchQueryInput = document.querySelector('#search-query-input'),
pageControls = document.querySelector('#page-controls'),
previousPageArrow = document.querySelector('#previous-page-arrow'),
nextPageArrow = document.querySelector('#next-page-arrow'),
currentPageNumber = document.querySelector('#current-page-number')
// global state variables,
// these are manipulated at a global level
// because they are used in various places
let currentPage = 1
let searchQuery = ''
// Fetch the quote of the day from FAVQs api and render it on the page
function fetchQuoteOfTheDay () {
let url = 'https://favqs.com/api/qotd'
// let quotes = []
// create an object for HTTP headers
const headers = {
'Content-Type': 'application/json'
}
// axios.get(url, configObject)
axios
.get(url, { headers: headers })
.then(response => {
quote = response.data.quote
renderQuotes([quote])
renderPageArrows([quote])
})
.catch(error => console.log(error))
}
// search for the filterQuery
function fetchQuotes (pageNumber = 1, filterQuery = '') {
let url = 'https://favqs.com/api/quotes'
let quotes, isLastPage
// create an object for HTTP headers
const headers = {
'Content-Type': 'application/json',
Authorization: `Token token=${FAVQ_API_KEY}`
}
// create an object for the URL query parameters
const params = {
page: pageNumber,
filter: filterQuery
}
// axios.get(url, configObject)
axios
.get(url, {
headers: headers,
params: params
})
.then(response => {
// pull the data out of the HTTP response
quotes = response.data.quotes
currentPage = response.data.page
isLastPage = response.data.last_page
//
renderQuotes(quotes)
renderPageArrows(quotes, pageNumber, isLastPage)
})
.catch(error => console.log(error))
}
// render the page navigation arrows based on the currentPage and
function renderPageArrows (quotes, pageNumber, isLastPage) {
// hide the arrows unless there is more than one quote
if (quotes.length < 2) {
pageControls.style.display = 'none'
} else {
pageControls.style.display = 'flex'
// hide the left arrow if we're on the first page
if (pageNumber === 1) {
previousPageArrow.style.display = 'none'
} else {
previousPageArrow.style.display = 'block'
}
// hide the right arrow if we're on the last page
if (isLastPage) {
nextPageArrow.style.display = 'none'
} else {
nextPageArrow.style.display = 'block'
}
}
currentPageNumber.innerHTML = currentPage
}
function renderQuotes (quotes) {
let quote, blockquote, cite
quoteContainer.innerHTML = ''
if (quotes[0].body.toLowerCase() === 'no quotes found') {
quoteContainer.innerHTML = `<h1>No quotes found</h1>`
} else {
for (quote of quotes) {
blockquote = document.createElement('blockquote')
cite = document.createElement('cite')
blockquote.classList.add('quote')
blockquote.innerHTML = quote.body + '<br/>'
cite.innerHTML = `- ${quote.author}`
blockquote.appendChild(cite)
quoteContainer.appendChild(blockquote)
}
}
}
// when the searchButton is clicked,
searchButton.addEventListener('click', function () {
// set the searchQuery to the current value of the searchQueryInput,
// reset the currentPage to 1
searchQuery = searchQueryInput.value
currentPage = 1
// set loading message
quoteContainer.innerHTML =
'<h1><i class="fas fa-stroopwafel spinner"></i></h1>'
// call the API with the new currentPage
// number and previous searchQuery
fetchQuotes(currentPage, searchQuery)
})
function handlePageChange (direction) {
// set loading message
quoteContainer.innerHTML =
'<h1><i class="fas fa-stroopwafel spinner"></i></h1>'
// change the currentPage based on the direction
if (direction === 'previous') {
currentPage--
} else if (direction === 'next') {
currentPage++
}
// call the API with the new currentPage
// number and previous searchQuery
fetchQuotes(currentPage, searchQuery)
}
// handlePageChange could also be defined like this
// using a ternary if/else operation and an arrow function:
// const handlePageChange = direction =>
// fetchQuotes(direction === 'next' ? currentPage++ : currentPage--, searchQuery)
// when the previousPageArrow is clicked,
// call handlePageChange to decrease page number
previousPageArrow.addEventListener('click', () => handlePageChange('previous'))
// when the nextPageArrow is clicked,
// call handlePageChange to increase page number
nextPageArrow.addEventListener('click', () => handlePageChange('next'))
// load the initial quote
fetchQuoteOfTheDay()