chore: offline & fetching fixes

This commit is contained in:
2024-07-24 17:52:20 +02:00
parent 54c1dbbf15
commit ed2b8be4dc
3 changed files with 27 additions and 33 deletions
+5 -15
View File
@@ -81,9 +81,7 @@ export default defineComponent({
isUpdateCardOpen: false,
currentLang: 'pl',
isOnProductionHost: location.hostname == 'stacjownik-td2.web.app',
nextUpdateTime: 0
isOnProductionHost: location.hostname == 'stacjownik-td2.web.app'
}),
created() {
@@ -96,22 +94,13 @@ export default defineComponent({
methods: {
init() {
if (!this.isOnProductionHost) document.title = 'Stacjownik Dev';
this.loadLang();
this.setupOfflineHandling();
this.checkAppVersion();
this.apiStore.setupAPIData();
window.requestAnimationFrame(this.update);
if (!this.isOnProductionHost) document.title = 'Stacjownik Dev';
},
update(t: number) {
if (t >= this.nextUpdateTime) {
this.apiStore.fetchActiveData();
this.nextUpdateTime = t + 20000;
}
window.requestAnimationFrame(this.update);
},
async checkAppVersion() {
@@ -131,7 +120,7 @@ export default defineComponent({
};
this.isUpdateCardOpen =
(storageVersion != '' && storageVersion != version) ||
(storageVersion != '' && storageVersion != version && this.isOnProductionHost) ||
import.meta.env.VITE_UPDATE_TEST === 'test';
} catch (error) {
console.error(`Wystąpił błąd podczas pobierania danych z API GitHuba: ${error}`);
@@ -158,6 +147,7 @@ export default defineComponent({
handleOnlineMode() {
this.store.isOffline = false;
this.apiStore.dataStatuses.connection = Status.Data.Loading;
this.apiStore.connectToAPI();
},
+22 -17
View File
@@ -18,7 +18,7 @@ export const useApiStore = defineStore('apiStore', {
donatorsData: [] as API.Donators.Response,
sceneryData: [] as StationJSONData[],
lastFetchData: new Date(),
nextUpdateTime: 0,
client: undefined as AxiosInstance | undefined,
@@ -51,27 +51,33 @@ export const useApiStore = defineStore('apiStore', {
// Static data
this.fetchDonatorsData();
this.fetchStationsGeneralInfo();
// Ponowne pobieranie danych po ServiceWorkerze
setTimeout(() => {
this.fetchStationsGeneralInfo();
}, Math.floor(Math.random() * 500) + 1000);
this.fetchVehiclesInfo();
window.requestAnimationFrame(this.updateTick);
},
updateTick(t: number) {
if (this.dataStatuses.connection == Status.Data.Offline) return;
if (t >= this.nextUpdateTime) {
this.fetchActiveData();
this.nextUpdateTime = t + 20000;
}
window.requestAnimationFrame(this.updateTick);
},
async fetchActiveData() {
if (import.meta.env.VITE_API_ACTIVE_DATA_MODE == 'mocking') {
import('../../tests/data/getActiveData.json').then((data) => {
console.warn('activeData: mocking mode');
this.activeData = data.default as API.ActiveData.Response;
this.lastFetchData = new Date();
// if (import.meta.env.VITE_API_ACTIVE_DATA_MODE == 'mocking') {
// import('../../tests/data/getActiveData.json').then((data) => {
// console.warn('activeData: mocking mode');
// this.activeData = data.default as API.ActiveData.Response;
this.dataStatuses.connection = Status.Data.Loaded;
});
// this.dataStatuses.connection = Status.Data.Loaded;
// });
return;
}
// return;
// }
if (!this.activeData) this.dataStatuses.connection = Status.Data.Loading;
@@ -79,7 +85,6 @@ export const useApiStore = defineStore('apiStore', {
const response = await this.client!.get<API.ActiveData.Response>('api/getActiveData');
this.activeData = response.data;
this.lastFetchData = new Date();
this.dataStatuses.connection = Status.Data.Loaded;
} catch (error) {
this.dataStatuses.connection = Status.Data.Error;