Browse Source

migrate to useFetch

this makes sure that there is no double fetching
pull/1244/head
Bernd Storath 12 months ago
parent
commit
7158567389
  1. 10
      src/pages/index.vue
  2. 42
      src/stores/global.ts
  3. 10
      src/utils/api.ts

10
src/pages/index.vue

@ -50,6 +50,10 @@ const clientsStore = useClientsStore();
const intervalId = ref<NodeJS.Timeout | null>(null); const intervalId = ref<NodeJS.Timeout | null>(null);
globalStore.fetchUITrafficStats();
globalStore.fetchChartType();
globalStore.fetchRelease();
onMounted(() => { onMounted(() => {
// TODO?: replace with websocket or similar // TODO?: replace with websocket or similar
intervalId.value = setInterval(() => { intervalId.value = setInterval(() => {
@ -59,12 +63,6 @@ onMounted(() => {
}) })
.catch(console.error); .catch(console.error);
}, 1000); }, 1000);
globalStore.fetchUITrafficStats();
globalStore.fetchChartType();
globalStore.fetchRelease();
}); });
onUnmounted(() => { onUnmounted(() => {

42
src/stores/global.ts

@ -12,40 +12,34 @@ export const useGlobalStore = defineStore('Global', () => {
const { availableLocales, locale } = useI18n(); const { availableLocales, locale } = useI18n();
async function fetchRelease() { async function fetchRelease() {
const lang = await api.getLang(); const { data: lang } = await api.getLang();
if (lang !== getItem('lang') && availableLocales.includes(lang)) { if (
setItem('lang', lang); lang.value !== getItem('lang') &&
locale.value = lang; availableLocales.includes(lang.value!)
) {
setItem('lang', lang.value!);
locale.value = lang.value!;
} }
const release = await api.getRelease(); // this is still called on client. why?
const { data: release } = await api.getRelease();
if (release.currentRelease >= release.latestRelease.version) return; if (release.value!.currentRelease >= release.value!.latestRelease.version) {
return;
}
currentRelease.value = release.currentRelease; currentRelease.value = release.value!.currentRelease;
latestRelease.value = release.latestRelease; latestRelease.value = release.value!.latestRelease;
} }
async function fetchChartType() { async function fetchChartType() {
api const { data: chartType } = await api.getChartType();
.getChartType() uiChartType.value = chartType.value ?? 0;
.then((res) => {
uiChartType.value = res;
})
.catch(() => {
uiChartType.value = 0;
});
} }
async function fetchUITrafficStats() { async function fetchUITrafficStats() {
api const { data: trafficStats } = await api.getTrafficStats();
.getUITrafficStats() uiTrafficStats.value = trafficStats.value ?? false;
.then((res) => {
uiTrafficStats.value = res;
})
.catch(() => {
uiTrafficStats.value = false;
});
} }
const updateCharts = computed(() => { const updateCharts = computed(() => {

10
src/utils/api.ts

@ -1,24 +1,24 @@
class API { class API {
async getRelease() { async getRelease() {
return $fetch('/api/release', { return useFetch('/api/release', {
method: 'get', method: 'get',
}); });
} }
async getLang() { async getLang() {
return $fetch('/api/lang', { return useFetch('/api/lang', {
method: 'get', method: 'get',
}); });
} }
async getUITrafficStats() { async getTrafficStats() {
return $fetch('/api/ui-traffic-stats', { return useFetch('/api/ui-traffic-stats', {
method: 'get', method: 'get',
}); });
} }
async getChartType() { async getChartType() {
return $fetch('/api/ui-chart-type', { return useFetch('/api/ui-chart-type', {
method: 'get', method: 'get',
}); });
} }

Loading…
Cancel
Save