Skip to content

Commit 6ef65e6

Browse files
luizhf42gustavosbarreto
authored andcommitted
refactor(ui): migrate Spinner store to Pinia
1 parent 10e0116 commit 6ef65e6

File tree

7 files changed

+27
-42
lines changed

7 files changed

+27
-42
lines changed

ui/src/api/interceptors.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { AxiosError, AxiosInstance, InternalAxiosRequestConfig, AxiosResponse } from "axios";
2-
import { store } from "../store";
32
import { router } from "../router";
43
import useAuthStore from "@/store/modules/auth";
4+
import useSpinnerStore from "@/store/modules/spinner";
55

66
const onRequest = (config: InternalAxiosRequestConfig): InternalAxiosRequestConfig => {
7-
store.dispatch("spinner/setStatus", true);
7+
useSpinnerStore().status = true;
88
return config;
99
};
1010

1111
const onRequestError = (error: AxiosError) => {
12-
store.dispatch("spinner/setStatus", false);
12+
useSpinnerStore().status = false;
1313
return Promise.reject(error);
1414
};
1515

1616
const onResponse = (response: AxiosResponse): AxiosResponse => {
17-
store.dispatch("spinner/setStatus", false);
17+
useSpinnerStore().status = false;
1818
return response;
1919
};
2020

2121
const onResponseError = async (error: AxiosError): Promise<AxiosError> => {
2222
const { logout } = useAuthStore();
23-
store.dispatch("spinner/setStatus", false);
23+
useSpinnerStore().status = false;
2424
if (error.response?.status === 401) {
2525
logout();
2626
await router.push({ name: "Login", query: router.currentRoute.value.query });

ui/src/layouts/AppLayout.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,6 @@ import { useRouter } from "vue-router";
164164
import { useDisplay } from "vuetify";
165165
import Logo from "../assets/logo-inverted.png";
166166
import { envVariables } from "../envVariables";
167-
import { useStore } from "../store";
168167
import UserWarning from "../components/User/UserWarning.vue";
169168
import Namespace from "@/components/Namespace/Namespace.vue";
170169
import AppBar from "../components/AppBar/AppBar.vue";
@@ -174,15 +173,16 @@ import Snackbar from "@/components/Snackbar/Snackbar.vue";
174173
import useLayoutStore from "@/store/modules/layout";
175174
import useNamespacesStore from "@/store/modules/namespaces";
176175
import usePrivateKeysStore from "@/store/modules/private_keys";
176+
import useSpinnerStore from "@/store/modules/spinner";
177177
178178
defineOptions({
179179
inheritAttrs: false,
180180
});
181181
182182
const router = useRouter();
183-
const store = useStore();
184183
const layoutStore = useLayoutStore();
185184
const namespacesStore = useNamespacesStore();
185+
const spinnerStore = useSpinnerStore();
186186
const { getPrivateKeyList } = usePrivateKeysStore();
187187
const currentRoute = computed(() => router.currentRoute);
188188
const showNamespaceAdd = ref(false);
@@ -195,8 +195,8 @@ const permanent = computed(() => lgAndUp.value);
195195
const showNavigationDrawer = ref(lgAndUp.value);
196196
197197
const hasSpinner = computed({
198-
get() { return store.getters["spinner/status"]; },
199-
set(v) { store.dispatch("spinner/setStatus", v); },
198+
get() { return spinnerStore.status; },
199+
set(newStatus) { spinnerStore.status = newStatus; },
200200
});
201201
202202
const disableItem = (item: string) => !hasNamespaces.value && item !== "Settings";

ui/src/store/helpers/http.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Axios from "axios";
22
import { router } from "@/router";
3-
import { store } from "..";
43
import useAuthStore from "../modules/auth";
4+
import useSpinnerStore from "@/store/modules/spinner";
55

66
export default () => {
77
const axios = Axios.create({
@@ -12,10 +12,11 @@ export default () => {
1212
});
1313

1414
const { logout } = useAuthStore();
15+
const spinnerStore = useSpinnerStore();
1516

1617
axios.interceptors.request.use(
1718
(config) => {
18-
store.dispatch("spinner/setStatus", true);
19+
spinnerStore.status = true;
1920
return config;
2021
},
2122
async (error) => {
@@ -25,11 +26,11 @@ export default () => {
2526

2627
axios.interceptors.response.use(
2728
(response) => {
28-
store.dispatch("spinner/setStatus", false);
29+
spinnerStore.status = false;
2930
return response;
3031
},
3132
async (error) => {
32-
store.dispatch("spinner/setStatus", false);
33+
spinnerStore.status = false;
3334
if (error.response.status === 401) {
3435
logout();
3536
await router.push({ name: "Login" });

ui/src/store/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ import { users, UsersState } from "./modules/users";
55
import { tags, TagsState } from "./modules/tags";
66
import { stats, StatsState } from "./modules/stats";
77
import { support, SupportState } from "./modules/support";
8-
import { spinner, SpinnerState } from "./modules/spinner";
98
import { webEndpoints, WebEndpointsState } from "./modules/web_endpoints";
109

1110
export interface State {
1211
webEndpoints: WebEndpointsState;
13-
spinner: SpinnerState;
1412
stats: StatsState;
1513
support: SupportState;
1614
tags: TagsState;
@@ -22,7 +20,6 @@ export const key: InjectionKey<Store<State>> = Symbol("store");
2220
export const store = createStore<State>({
2321
modules: {
2422
webEndpoints,
25-
spinner,
2623
stats,
2724
support,
2825
tags,

ui/src/store/modules/spinner.ts

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,10 @@
1-
import { Module } from "vuex";
2-
import { State } from "..";
1+
import { defineStore } from "pinia";
2+
import { ref } from "vue";
33

4-
export interface SpinnerState {
5-
status: boolean;
6-
}
4+
const useSpinnerStore = defineStore("spinner", () => {
5+
const status = ref(false);
76

8-
export const spinner: Module<SpinnerState, State> = {
9-
namespaced: true,
10-
state: {
11-
status: false,
12-
},
13-
getters: {
14-
status: (state: SpinnerState) => state.status,
15-
},
16-
mutations: {
17-
setStatus: (state, status) => {
18-
state.status = status;
19-
},
20-
},
7+
return { status };
8+
});
219

22-
actions: {
23-
setStatus: async ({ commit }, status) => {
24-
commit("setStatus", status);
25-
},
26-
},
27-
};
10+
export default useSpinnerStore;

ui/tests/layouts/AppLayout.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import { router } from "@/router";
1212
import { SnackbarPlugin } from "@/plugins/snackbar";
1313
import { devicesApi, containersApi } from "@/api/http";
1414
import { envVariables } from "@/envVariables";
15+
import useSpinnerStore from "@/store/modules/spinner";
1516

1617
let mockDevices: MockAdapter;
1718
let mockContainers: MockAdapter;
1819

1920
describe("App Layout Component", () => {
2021
let wrapper;
2122
setActivePinia(createPinia());
23+
const spinnerStore = useSpinnerStore();
2224
const vuetify = createVuetify({
2325
components,
2426
directives,
@@ -38,7 +40,7 @@ describe("App Layout Component", () => {
3840

3941
envVariables.hasWebEndpoints = true;
4042
envVariables.isCloud = true;
41-
store.dispatch("spinner/setStatus", true);
43+
spinnerStore.status = true;
4244

4345
mockDevices = new MockAdapter(devicesApi.getAxios());
4446
mockContainers = new MockAdapter(containersApi.getAxios());
@@ -96,7 +98,7 @@ describe("App Layout Component", () => {
9698
});
9799

98100
it("Renders loading screen", async () => {
99-
await store.dispatch("spinner/setStatus", true);
101+
spinnerStore.status = true;
100102
await flushPromises();
101103

102104
const layoutWrapper = wrapper.findComponent(AppLayout);

ui/tests/store/modules/web_endpoints.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { createPinia, setActivePinia } from "pinia";
12
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
23
import MockAdapter from "axios-mock-adapter";
34
import { webEndpointsApi } from "@/api/http";
@@ -14,6 +15,7 @@ const initialWebEndpoints = {
1415
};
1516

1617
describe("WebEndpoints store", () => {
18+
setActivePinia(createPinia());
1719
let mock: MockAdapter;
1820

1921
beforeEach(() => {

0 commit comments

Comments
 (0)