Skip to content

Commit 322df69

Browse files
luizhf42gustavosbarreto
authored andcommitted
refactor(ui): migrate Stats store to Pinia
1 parent 6ef65e6 commit 322df69

File tree

9 files changed

+46
-60
lines changed

9 files changed

+46
-60
lines changed

ui/src/components/AppBar/AppBar.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ import useAuthStore from "@/store/modules/auth";
111111
import useBillingStore from "@/store/modules/billing";
112112
import useLayoutStore from "@/store/modules/layout";
113113
import useNamespacesStore from "@/store/modules/namespaces";
114+
import useStatsStore from "@/store/modules/stats";
115+
import { IStats } from "@/interfaces/IStats";
114116
115117
type MenuItem = {
116118
title: string;
@@ -135,6 +137,7 @@ const authStore = useAuthStore();
135137
const billingStore = useBillingStore();
136138
const layoutStore = useLayoutStore();
137139
const namespacesStore = useNamespacesStore();
140+
const statsStore = useStatsStore();
138141
const router = useRouter();
139142
const route = useRoute();
140143
const snackbar = useSnackbar();
@@ -167,7 +170,7 @@ const logout = async () => {
167170
try {
168171
authStore.logout();
169172
namespacesStore.namespaceList = [];
170-
await store.dispatch("stats/clear");
173+
statsStore.stats = {} as IStats;
171174
if (isChatCreated.value) {
172175
toggle("close");
173176
reset();

ui/src/components/Billing/BillingWarning.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,12 @@ import { useStore } from "@/store";
4747
import BaseDialog from "../BaseDialog.vue";
4848
import useAuthStore from "@/store/modules/auth";
4949
import useBillingStore from "@/store/modules/billing";
50+
import useStatsStore from "@/store/modules/stats";
5051
5152
const store = useStore();
5253
const authStore = useAuthStore();
5354
const billingStore = useBillingStore();
55+
const { stats } = useStatsStore();
5456
5557
const hasAuthorization = computed(() => {
5658
const { role } = authStore;
@@ -74,7 +76,7 @@ const showWarningDialog = computed({
7476
get() {
7577
return (
7678
(store.getters["users/statusUpdateAccountDialog"]
77-
&& store.getters["stats/stats"].registered_devices === 3
79+
&& stats.registered_devices === 3
7880
&& !billingStore.isActive)
7981
|| store.getters["users/statusUpdateAccountDialogByDeviceAction"]
8082
);

ui/src/components/User/UserWarning.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import useAuthStore from "@/store/modules/auth";
6868
import useBillingStore from "@/store/modules/billing";
6969
import useDevicesStore from "@/store/modules/devices";
7070
import useNamespacesStore from "@/store/modules/namespaces";
71+
import useStatsStore from "@/store/modules/stats";
7172
7273
defineOptions({
7374
inheritAttrs: false,
@@ -80,6 +81,7 @@ const authStore = useAuthStore();
8081
const billingStore = useBillingStore();
8182
const devicesStore = useDevicesStore();
8283
const namespacesStore = useNamespacesStore();
84+
const statsStore = useStatsStore();
8385
const router = useRouter();
8486
const showInstructions = ref(false);
8587
const showWelcome = ref<boolean>(false);
@@ -88,14 +90,14 @@ const showDeviceWarning = computed(() => store.getters["users/deviceDuplicationE
8890
const showRecoverHelper = computed(() => authStore.showRecoveryModal);
8991
const showForceRecoveryMail = computed(() => authStore.showForceRecoveryMail);
9092
const showPaywall = computed(() => store.getters["users/showPaywall"]);
91-
const stats = computed(() => store.getters["stats/stats"]);
93+
const stats = computed(() => statsStore.stats);
9294
const currentAnnouncement = computed(() => announcementStore.currentAnnouncement);
9395
const hasNamespaces = computed(() => namespacesStore.namespaceList.length !== 0);
9496
const showDeviceChooser = computed(() => devicesStore.showDeviceChooser);
9597
9698
const billingWarning = async () => {
9799
await billingStore.getSubscriptionInfo();
98-
const showDeviceChooser = store.getters["stats/stats"].registered_devices > 3 && !billingStore.isActive;
100+
const showDeviceChooser = stats.value.registered_devices > 3 && !billingStore.isActive;
99101
devicesStore.showDeviceChooser = showDeviceChooser;
100102
};
101103
@@ -153,7 +155,7 @@ const showDialogs = async () => {
153155
await namespacesStore.fetchNamespaceList({ perPage: 30 });
154156
155157
if (hasNamespaces.value) {
156-
await store.dispatch("stats/get");
158+
await statsStore.fetchStats();
157159
158160
showScreenWelcome();
159161

ui/src/components/Welcome/Welcome.vue

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383

8484
<script setup lang="ts">
8585
import { ref } from "vue";
86-
import { useStore } from "@/store";
8786
import WelcomeFirstScreen from "./WelcomeFirstScreen.vue";
8887
import WelcomeSecondScreen from "./WelcomeSecondScreen.vue";
8988
import WelcomeThirdScreen from "./WelcomeThirdScreen.vue";
@@ -95,14 +94,15 @@ import useAuthStore from "@/store/modules/auth";
9594
import { IDevice } from "@/interfaces/IDevice";
9695
import useDevicesStore from "@/store/modules/devices";
9796
import useNotificationsStore from "@/store/modules/notifications";
97+
import useStatsStore from "@/store/modules/stats";
9898
9999
type Timer = ReturnType<typeof setInterval>;
100100
101101
const showDialog = defineModel<boolean>({ required: true });
102-
const store = useStore();
103102
const authStore = useAuthStore();
104103
const devicesStore = useDevicesStore();
105104
const { fetchNotifications } = useNotificationsStore();
105+
const statsStore = useStatsStore();
106106
const snackbar = useSnackbar();
107107
const el = ref<number>(1);
108108
const firstPendingDevice = ref<IDevice>();
@@ -111,9 +111,9 @@ const enable = ref(false);
111111
const pollingDevices = () => {
112112
polling.value = setInterval(async () => {
113113
try {
114-
await store.dispatch("stats/get");
114+
await statsStore.fetchStats();
115115
116-
enable.value = store.getters["stats/stats"].pending_devices !== 0;
116+
enable.value = statsStore.stats.pending_devices !== 0;
117117
if (enable.value) {
118118
el.value = 3;
119119
clearTimeout(polling.value);
@@ -134,8 +134,8 @@ const acceptDevice = async () => {
134134
if (firstPendingDevice.value) {
135135
await devicesStore.acceptDevice(firstPendingDevice.value.uid);
136136
137-
fetchNotifications();
138-
store.dispatch("stats/get");
137+
await fetchNotifications();
138+
await statsStore.fetchStats();
139139
140140
el.value = 4;
141141
}
@@ -150,7 +150,6 @@ const command = () => {
150150
const { hostname, protocol } = window.location;
151151
const { tenantId } = authStore;
152152
153-
// eslint-disable-next-line vue/max-len
154153
return `curl -sSf ${protocol}//${hostname}${port}/install.sh | TENANT_ID=${tenantId} SERVER_ADDRESS=${protocol}//${hostname} sh`;
155154
};
156155

ui/src/store/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import { createStore, Store, useStore as vuexUseStore } from "vuex";
33

44
import { users, UsersState } from "./modules/users";
55
import { tags, TagsState } from "./modules/tags";
6-
import { stats, StatsState } from "./modules/stats";
76
import { support, SupportState } from "./modules/support";
87
import { webEndpoints, WebEndpointsState } from "./modules/web_endpoints";
98

109
export interface State {
1110
webEndpoints: WebEndpointsState;
12-
stats: StatsState;
1311
support: SupportState;
1412
tags: TagsState;
1513
users: UsersState;
@@ -20,7 +18,6 @@ export const key: InjectionKey<Store<State>> = Symbol("store");
2018
export const store = createStore<State>({
2119
modules: {
2220
webEndpoints,
23-
stats,
2421
support,
2522
tags,
2623
users,

ui/src/store/modules/stats.ts

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
1-
import { Module } from "vuex";
2-
import { AxiosResponse } from "axios";
1+
import { defineStore } from "pinia";
2+
import { ref } from "vue";
33
import getStats from "../api/stats";
44
import { IStats } from "@/interfaces/IStats";
5-
import { State } from "..";
65

7-
export interface StatsState {
8-
stats: IStats;
9-
}
6+
const useStatsStore = defineStore("stats", () => {
7+
const stats = ref({} as IStats);
108

11-
export const stats: Module<StatsState, State> = {
12-
namespaced: true,
13-
state: {
14-
stats: {} as IStats,
15-
},
9+
const fetchStats = async () => {
10+
const res = await getStats();
11+
stats.value = res.data as IStats;
12+
};
1613

17-
getters: {
18-
stats: (state) => state.stats,
19-
},
14+
return {
15+
stats,
16+
fetchStats,
17+
};
18+
});
2019

21-
mutations: {
22-
setStats: (state, res: AxiosResponse) => {
23-
state.stats = res.data;
24-
},
25-
26-
clearListState: (state) => {
27-
state.stats = {} as IStats;
28-
},
29-
},
30-
31-
actions: {
32-
async get({ commit }) {
33-
const res = await getStats();
34-
commit("setStats", res);
35-
return res;
36-
},
37-
38-
async clear({ commit }) {
39-
commit("clearListState");
40-
},
41-
},
42-
};
20+
export default useStatsStore;

ui/src/views/Home.vue

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,16 @@ import { computed, onMounted, ref } from "vue";
2323
import axios, { AxiosError } from "axios";
2424
import { StatCardItem } from "@/interfaces/IStats";
2525
import StatCard from "@/components/StatCard.vue";
26-
import { useStore } from "../store";
2726
import handleError from "@/utils/handleError";
2827
import useSnackbar from "@/helpers/snackbar";
2928
import useNamespacesStore from "@/store/modules/namespaces";
29+
import useStatsStore from "@/store/modules/stats";
3030
31-
const store = useStore();
3231
const namespacesStore = useNamespacesStore();
32+
const statsStore = useStatsStore();
3333
const snackbar = useSnackbar();
3434
const hasStatus = ref(false);
35-
const itemsStats = computed(() => store.getters["stats/stats"]);
35+
const stats = computed(() => statsStore.stats);
3636
const hasNamespace = computed(() => namespacesStore.namespaceList.length !== 0);
3737
3838
const items = computed<StatCardItem[]>(() => [
@@ -42,31 +42,31 @@ const items = computed<StatCardItem[]>(() => [
4242
icon: "mdi-devices",
4343
buttonLabel: "Add Device",
4444
path: "devices",
45-
stat: itemsStats.value.registered_devices || 0,
45+
stat: stats.value.registered_devices || 0,
4646
},
4747
{
4848
title: "Online Devices",
4949
content: "Devices are online and ready for connecting",
5050
icon: "mdi-devices",
5151
buttonLabel: "View all Devices",
5252
path: "devices",
53-
stat: itemsStats.value.online_devices || 0,
53+
stat: stats.value.online_devices || 0,
5454
},
5555
{
5656
title: "Active Sessions",
5757
content: "Active SSH Sessions opened by users",
5858
icon: "mdi-devices",
5959
buttonLabel: "View all Sessions",
6060
path: "sessions",
61-
stat: itemsStats.value.active_sessions || 0,
61+
stat: stats.value.active_sessions || 0,
6262
},
6363
]);
6464
6565
onMounted(async () => {
6666
if (!hasNamespace.value) return;
6767
6868
try {
69-
await store.dispatch("stats/get");
69+
await statsStore.fetchStats();
7070
} catch (error: unknown) {
7171
if (axios.isAxiosError(error)) {
7272
const axiosError = error as AxiosError;

ui/src/views/Login.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,16 @@ import useCountdown from "@/utils/countdownTimeout";
159159
import { envVariables } from "@/envVariables";
160160
import useAuthStore from "@/store/modules/auth";
161161
import useNamespacesStore from "@/store/modules/namespaces";
162+
import useStatsStore from "@/store/modules/stats";
163+
import { IStats } from "@/interfaces/IStats";
162164
163165
const store = useStore();
164166
const route = useRoute();
165167
const router = useRouter();
166168
const snackbar = useSnackbar();
167169
const authStore = useAuthStore();
168170
const namespacesStore = useNamespacesStore();
171+
const statsStore = useStatsStore();
169172
const showPassword = ref(false);
170173
const loginToken = ref(false);
171174
const invalid = reactive({ title: "", msg: "", timeout: false });
@@ -228,9 +231,9 @@ onMounted(async () => {
228231
229232
loginToken.value = true;
230233
231-
await store.dispatch("stats/clear");
232234
namespacesStore.namespaceList = [];
233235
authStore.logout();
236+
statsStore.stats = {} as IStats;
234237
await authStore.loginWithToken(route.query.token as string);
235238
236239
window.location.href = "/";

ui/tests/components/Billing/BillingWarning.spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import { key } from "@/store";
88
import { router } from "@/router";
99
import useAuthStore from "@/store/modules/auth";
1010
import useBillingStore from "@/store/modules/billing";
11+
import useStatsStore from "@/store/modules/stats";
1112

1213
setActivePinia(createPinia());
1314
const authStore = useAuthStore();
1415
const billingStore = useBillingStore();
16+
const statsStore = useStatsStore();
1517
const statusUpdateAccountDialog = true;
1618
const statusUpdateAccountDialogByDeviceAction = false;
1719

@@ -26,14 +28,14 @@ const stats = {
2628
const store = (statsData: typeof stats, billingEnabled: boolean, role: string) => {
2729
authStore.role = role;
2830
billingStore.billing.active = billingEnabled;
31+
statsStore.stats = statsData;
2932
return createStore({
3033
state: {
3134
stats: statsData,
3235
statusUpdateAccountDialog,
3336
statusUpdateAccountDialogByDeviceAction,
3437
},
3538
getters: {
36-
"stats/stats": (state) => state.stats,
3739
"users/statusUpdateAccountDialog": (state) => state.statusUpdateAccountDialog,
3840
"users/statusUpdateAccountDialogByDeviceAction": (state) => state.statusUpdateAccountDialogByDeviceAction,
3941
},

0 commit comments

Comments
 (0)