Fix refresh button on view station page
This commit is contained in:
parent
c2ea04de4b
commit
32f52fe549
3 changed files with 36 additions and 4 deletions
2
sw.js
2
sw.js
|
@ -1,4 +1,4 @@
|
|||
const VERSION = 'v15'
|
||||
const VERSION = 'v16'
|
||||
const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/'
|
||||
const API_TRAINS = `${API_ORIGIN}v3/trains`
|
||||
const API_STATIONS = `${API_ORIGIN}v3/stations`
|
||||
|
|
|
@ -10,6 +10,10 @@
|
|||
color: red !important;
|
||||
}
|
||||
|
||||
.rsk {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#-date {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
|
|
|
@ -114,14 +114,25 @@ function refresh() {
|
|||
reschedule(10000)
|
||||
return
|
||||
}
|
||||
return response.json()
|
||||
var cacheDate = response.headers.get('SW-Cached-At')
|
||||
return response.json().then(function (data) {
|
||||
data['$cacheDate'] = cacheDate
|
||||
return data
|
||||
})
|
||||
}).then(function (response) {
|
||||
if (!response) {
|
||||
return
|
||||
}
|
||||
var cacheDate = response['$cacheDate']
|
||||
if (cacheDate) {
|
||||
cacheDate = new Date(cacheDate)
|
||||
}
|
||||
var success = !cacheDate
|
||||
stationData = response
|
||||
onStationData(response)
|
||||
reschedule()
|
||||
// Check in 1 seconds if network error
|
||||
reschedule(success ? undefined : 1000)
|
||||
return success
|
||||
}).catch(function (e) {
|
||||
// Check in 1 second if network error
|
||||
reschedule(1000)
|
||||
|
@ -136,7 +147,24 @@ window.addEventListener('unload', function (e) {
|
|||
})
|
||||
|
||||
function rsk() {
|
||||
refresh()
|
||||
function changeRskText(newText) {
|
||||
document.querySelectorAll('.rsk').forEach(function (elem) {
|
||||
elem.textContent = newText
|
||||
})
|
||||
}
|
||||
|
||||
changeRskText('Refreshing...')
|
||||
refresh().catch(function () { return false}).then(function (success) {
|
||||
if (!success) {
|
||||
changeRskText('Refreshing failed')
|
||||
setTimeout(function (_) {
|
||||
changeRskText('Refresh')
|
||||
}, 3000)
|
||||
}
|
||||
else {
|
||||
changeRskText('Refresh')
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
window.addEventListener('load', function (e) {
|
||||
|
|
Loading…
Add table
Reference in a new issue