forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
32 lines (26 loc) · 869 Bytes
/
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
/* global document, fetch */
function updateFavoriteFruits (contents) {
if (typeof contents !== 'string') {
contents = contents.map((item) => `<li>${item}</li>`).join('')
}
document.querySelector('.favorite-fruits').innerHTML = contents
}
function getFavoriteFruits () {
document.querySelector('.favorite-fruits').innerHTML = '<div class="loader"></div>'
fetch('/favorite-fruits')
.then((response) => {
if (response.ok) {
return response.json()
}
const errorMessage = response.headers.get('status-text') || response.statusText
throw new Error(errorMessage)
})
.then((response) => {
updateFavoriteFruits(response.length ? response : 'No favorites')
})
.catch((error) => {
updateFavoriteFruits(`Failed loading favorite fruits: ${error.message}`)
})
}
getFavoriteFruits()
setInterval(getFavoriteFruits, 30000)