forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
offline.js
33 lines (27 loc) · 1.01 KB
/
offline.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
/* global window, document, fetch */
/* eslint-disable no-console */
document.getElementById('load-users').addEventListener('click', () => {
console.log('loading users')
document.querySelector('#users').innerText = ''
fetch('https://jsonplaceholder.cypress.io/users?_limit=3')
.then((r) => r.json())
.then((users) => {
console.table(users)
const usersHtml = users.map((user) => {
return `<li class="user">${user.id} - ${user.email}</li>`
}).join('\n')
document.querySelector('#users').innerHTML = usersHtml
})
.catch((e) => {
console.error('problem fetching users', e)
document.querySelector('#users').innerText = `Problem fetching users ${e.message}`
})
})
const updateNetworkStatus = () => {
const el = document.getElementById('network-status')
const text = window.navigator.onLine ? '🟢 online' : '🟥 offline'
el.innerText = text
}
updateNetworkStatus()
window.addEventListener('offline', updateNetworkStatus)
window.addEventListener('online', updateNetworkStatus)