refactor(dashboard,history): replace ViewModels with Pinia stores and improve backend
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import http from '@/lib/http';
|
||||
import DashboardData = App.Features.Dashboard.Data.DashboardData;
|
||||
|
||||
export const useDashboardStore = defineStore('dashboard', () => {
|
||||
const isLoading = ref(false);
|
||||
const error = ref<string>();
|
||||
const dashboardData = ref<DashboardData>();
|
||||
|
||||
async function fetchDashboardData() {
|
||||
isLoading.value = true;
|
||||
error.value = undefined;
|
||||
|
||||
try {
|
||||
const { data } = await http.get<DashboardData>('/api/energy-prices');
|
||||
dashboardData.value = data;
|
||||
} catch (err: unknown) {
|
||||
const axiosError = err as { response?: { data?: { message?: string } } };
|
||||
error.value =
|
||||
axiosError.response?.data?.message ||
|
||||
'Kon dashboard data niet ophalen';
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
error,
|
||||
dashboardData,
|
||||
fetchDashboardData,
|
||||
};
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { ref } from 'vue';
|
||||
import http from '@/lib/http';
|
||||
import HistoryData = App.Features.History.Data.HistoryData;
|
||||
|
||||
export const useHistoryStore = defineStore('history', () => {
|
||||
const isLoading = ref(false);
|
||||
const error = ref<string>();
|
||||
const historyData = ref<HistoryData>();
|
||||
const selectedDate = ref<string>(new Date().toISOString().split('T')[0]);
|
||||
|
||||
async function fetchHistoryData(date: string) {
|
||||
isLoading.value = true;
|
||||
error.value = undefined;
|
||||
selectedDate.value = date;
|
||||
|
||||
try {
|
||||
const { data } = await http.get<HistoryData>('/api/history', {
|
||||
params: { date },
|
||||
});
|
||||
historyData.value = data;
|
||||
} catch (err: unknown) {
|
||||
const axiosError = err as {
|
||||
response?: { data?: { message?: string } };
|
||||
};
|
||||
error.value =
|
||||
axiosError.response?.data?.message ||
|
||||
'Kon prijshistorie niet ophalen';
|
||||
} finally {
|
||||
isLoading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isLoading,
|
||||
error,
|
||||
historyData,
|
||||
selectedDate,
|
||||
fetchHistoryData,
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user