96 lines
2.7 KiB
Vue
96 lines
2.7 KiB
Vue
<template>
|
|
|
|
<v-list dense>
|
|
|
|
<v-list-item>
|
|
<v-list-item-action> Data </v-list-item-action>
|
|
<v-list-item-content>
|
|
{{localDate}} {{checkData}}
|
|
</v-list-item-content>
|
|
</v-list-item>
|
|
<v-list-item v-if="errorMessage">
|
|
<v-list-item-action> Error:</v-list-item-action>
|
|
<v-list-item-content>
|
|
{{errorMessage}}
|
|
</v-list-item-content>
|
|
</v-list-item>
|
|
|
|
</v-list>
|
|
</template>
|
|
|
|
<script>
|
|
|
|
const APIURL = "https://pomber.github.io/covid19/timeseries.json"
|
|
|
|
export default {
|
|
name: "DataLoader",
|
|
props: [''],
|
|
components: {},
|
|
data() {
|
|
return {
|
|
localTimestamp: 0,
|
|
localData: {},
|
|
refetchDelay: 1000 * 3600,
|
|
errorMessage: ""
|
|
}
|
|
},
|
|
created() {
|
|
this.readLocal()
|
|
},
|
|
watch: {
|
|
localData() {
|
|
this.$emit('input', this.localData)
|
|
}
|
|
},
|
|
methods: {
|
|
readLocal() {
|
|
let ls = localStorage.getItem('localTimestamp')
|
|
if (ls) {
|
|
this.localTimestamp = parseInt(ls);
|
|
this.localData = JSON.parse(localStorage.getItem('localData'));
|
|
}
|
|
|
|
if ((this.localTimestamp + this.refetchDelay) < new Date().getTime()) {
|
|
this.fetch()
|
|
}
|
|
},
|
|
fetch() {
|
|
fetch(APIURL)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
this.saveData(data)
|
|
}).catch(() => {
|
|
this.errorMessage = "Network Error"
|
|
});
|
|
},
|
|
saveData(data) {
|
|
if ('France' in data) {
|
|
// todo control data
|
|
this.localData = data
|
|
this.localTimestamp = new Date().getTime()
|
|
localStorage.setItem('localData', JSON.stringify(this.localData))
|
|
localStorage.setItem('localTimestamp', this.localTimestamp)
|
|
} else {
|
|
this.errorMessage = "Wrong data"
|
|
}
|
|
}
|
|
},
|
|
computed: {
|
|
checkData() {
|
|
if ('France' in this.localData) {
|
|
let last = this.localData['France'].slice(-1)[0]
|
|
if ('date' in last && 'recovered' in last) return 'OK'
|
|
}
|
|
return "No data"
|
|
},
|
|
localDate() {
|
|
return new Date(this.localTimestamp).toLocaleString()
|
|
}
|
|
},
|
|
}
|
|
|
|
</script>
|
|
|
|
<style scoped>
|
|
|
|
</style> |