Browse Source

Fix refresh button on view station page

master
Kenneth Bruen 2 years ago
parent
commit
32f52fe549
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 2
      sw.js
  2. 4
      view-station.css
  3. 34
      view-station.js

2
sw.js

@ -1,4 +1,4 @@
const VERSION = 'v15' const VERSION = 'v16'
const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/' const API_ORIGIN = 'https://scraper.infotren.dcdev.ro/'
const API_TRAINS = `${API_ORIGIN}v3/trains` const API_TRAINS = `${API_ORIGIN}v3/trains`
const API_STATIONS = `${API_ORIGIN}v3/stations` const API_STATIONS = `${API_ORIGIN}v3/stations`

4
view-station.css

@ -10,6 +10,10 @@
color: red !important; color: red !important;
} }
.rsk {
cursor: pointer;
}
#-date { #-date {
display: flex; display: flex;
justify-content: space-between; justify-content: space-between;

34
view-station.js

@ -114,14 +114,25 @@ function refresh() {
reschedule(10000) reschedule(10000)
return 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) { }).then(function (response) {
if (!response) { if (!response) {
return return
} }
var cacheDate = response['$cacheDate']
if (cacheDate) {
cacheDate = new Date(cacheDate)
}
var success = !cacheDate
stationData = response stationData = response
onStationData(response) onStationData(response)
reschedule() // Check in 1 seconds if network error
reschedule(success ? undefined : 1000)
return success
}).catch(function (e) { }).catch(function (e) {
// Check in 1 second if network error // Check in 1 second if network error
reschedule(1000) reschedule(1000)
@ -136,7 +147,24 @@ window.addEventListener('unload', function (e) {
}) })
function rsk() { 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) { window.addEventListener('load', function (e) {

Loading…
Cancel
Save