Skip to content

Commit c694119

Browse files
luizhf42gustavosbarreto
authored andcommitted
refactor(ui): migrate Support store to Pinia
1 parent 322df69 commit c694119

File tree

5 files changed

+56
-55
lines changed

5 files changed

+56
-55
lines changed

ui/src/components/AppBar/AppBar.vue

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ import {
100100
} from "vue";
101101
import { useRouter, useRoute, RouteLocationRaw, RouteLocation } from "vue-router";
102102
import { useChatWoot } from "@productdevbook/chatwoot/vue";
103-
import { useStore } from "@/store";
104103
import handleError from "@/utils/handleError";
105104
import UserIcon from "../User/UserIcon.vue";
106105
import NotificationsMenu from "./Notifications/NotificationsMenu.vue";
@@ -113,6 +112,7 @@ import useLayoutStore from "@/store/modules/layout";
113112
import useNamespacesStore from "@/store/modules/namespaces";
114113
import useStatsStore from "@/store/modules/stats";
115114
import { IStats } from "@/interfaces/IStats";
115+
import useSupportStore from "@/store/modules/support";
116116
117117
type MenuItem = {
118118
title: string;
@@ -132,23 +132,23 @@ defineOptions({
132132
});
133133
134134
const { setUser, setConversationCustomAttributes, toggle, reset } = useChatWoot();
135-
const store = useStore();
136135
const authStore = useAuthStore();
137136
const billingStore = useBillingStore();
138137
const layoutStore = useLayoutStore();
139138
const namespacesStore = useNamespacesStore();
140139
const statsStore = useStatsStore();
140+
const supportStore = useSupportStore();
141141
const router = useRouter();
142142
const route = useRoute();
143143
const snackbar = useSnackbar();
144-
const theme = computed(() => layoutStore.theme);
145-
const isChatCreated = computed(() => store.getters["support/getCreatedStatus"]);
146144
const tenant = computed(() => authStore.tenantId);
147145
const userEmail = computed(() => authStore.email);
148146
const userId = computed(() => authStore.id);
149147
const currentUser = computed(() => authStore.username);
150148
const isBillingActive = computed(() => billingStore.isActive);
151-
const identifier = computed(() => store.getters["support/getIdentifier"]);
149+
const theme = computed(() => layoutStore.theme);
150+
const isChatCreated = computed(() => supportStore.isChatCreated);
151+
const identifier = computed(() => supportStore.identifier);
152152
const isDarkMode = ref(theme.value === "dark");
153153
const chatSupportPaywall = ref(false);
154154
const showNavigationDrawer = defineModel<boolean>();
@@ -174,7 +174,7 @@ const logout = async () => {
174174
if (isChatCreated.value) {
175175
toggle("close");
176176
reset();
177-
store.commit("support/setCreatedStatus", false);
177+
supportStore.isChatCreated = false;
178178
}
179179
await router.push({ name: "Login" });
180180
} catch (error: unknown) {
@@ -189,7 +189,7 @@ const toggleDarkMode = () => {
189189
190190
const openChatwoot = async (): Promise<void> => {
191191
try {
192-
await store.dispatch("support/get", tenant.value);
192+
await supportStore.getIdentifier(tenant.value);
193193
194194
setUser(userId.value, {
195195
name: currentUser.value,
@@ -214,7 +214,7 @@ const openChatwoot = async (): Promise<void> => {
214214
window.dispatchEvent(new CustomEvent("chatwoot:ready"));
215215
}
216216
217-
store.commit("support/setCreatedStatus", true);
217+
supportStore.isChatCreated = true;
218218
toggle("open");
219219
} catch (error) {
220220
snackbar.showError("Failed to open chat support. Please check your account's billing and try again later.");

ui/src/store/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ 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 { support, SupportState } from "./modules/support";
76
import { webEndpoints, WebEndpointsState } from "./modules/web_endpoints";
87

98
export interface State {
109
webEndpoints: WebEndpointsState;
11-
support: SupportState;
1210
tags: TagsState;
1311
users: UsersState;
1412
}
@@ -18,7 +16,6 @@ export const key: InjectionKey<Store<State>> = Symbol("store");
1816
export const store = createStore<State>({
1917
modules: {
2018
webEndpoints,
21-
support,
2219
tags,
2320
users,
2421
},

ui/src/store/modules/support.ts

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,23 @@
1-
import { Module } from "vuex";
1+
import { defineStore } from "pinia";
2+
import { ref } from "vue";
23
import { useChatWoot } from "@productdevbook/chatwoot/vue";
3-
import * as apiSupport from "../api/namespaces";
4-
import { State } from "..";
4+
import * as supportApi from "../api/namespaces";
55

6-
export interface SupportState {
7-
identifier: string;
8-
chatCreated: boolean;
9-
}
6+
const useSupportStore = defineStore("support", () => {
7+
const identifier = ref<string>("");
8+
const isChatCreated = ref<boolean>(false);
109

11-
export const support: Module<SupportState, State> = {
12-
namespaced: true,
13-
state: {
14-
identifier: "",
15-
chatCreated: false,
16-
},
10+
const getIdentifier = async (tenantId: string) => {
11+
useChatWoot().reset();
12+
const res = await supportApi.getSupportID(tenantId);
13+
identifier.value = res.data.identifier as string;
14+
};
1715

18-
getters: {
19-
getIdentifier: (state) => state.identifier,
20-
getCreatedStatus: (state) => state.chatCreated,
21-
},
16+
return {
17+
identifier,
18+
isChatCreated,
19+
getIdentifier,
20+
};
21+
});
2222

23-
mutations: {
24-
setIdentifier: (state, identifier) => {
25-
state.identifier = identifier;
26-
},
27-
28-
setCreatedStatus: (state, status) => {
29-
state.chatCreated = status;
30-
},
31-
},
32-
33-
actions: {
34-
get: async (context, data) => {
35-
useChatWoot().reset();
36-
const res = await apiSupport.getSupportID(data);
37-
context.commit("setIdentifier", res.data.identifier);
38-
},
39-
},
40-
};
23+
export default useSupportStore;

ui/tests/components/AppBar/AppBar.spec.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { envVariables } from "@/envVariables";
1212
import { SnackbarPlugin } from "@/plugins/snackbar";
1313
import useAuthStore from "@/store/modules/auth";
1414
import useBillingStore from "@/store/modules/billing";
15+
import useSupportStore from "@/store/modules/support";
1516

1617
const Component = {
1718
template: "<v-layout><AppBar /></v-layout>",
@@ -22,6 +23,7 @@ vi.mock("@productdevbook/chatwoot/vue", () => ({
2223
setUser: vi.fn(),
2324
setConversationCustomAttributes: vi.fn(),
2425
toggle: vi.fn(),
26+
reset: vi.fn(),
2527
}),
2628
}));
2729

@@ -71,6 +73,7 @@ describe("AppBar Component", () => {
7173
setActivePinia(createPinia());
7274
const authStore = useAuthStore();
7375
const billingStore = useBillingStore();
76+
const supportStore = useSupportStore();
7477

7578
beforeEach(async () => {
7679
window.matchMedia = vi.fn().mockImplementation((query) => ({
@@ -184,13 +187,13 @@ describe("AppBar Component", () => {
184187
vi.spyOn(drawer.vm, "identifier", "get").mockReturnValue("mocked_identifier");
185188

186189
const windowOpenMock = vi.spyOn(window, "open").mockImplementation(() => null);
187-
const storeDispatchMock = vi.spyOn(store, "dispatch");
190+
const storeSpy = vi.spyOn(supportStore, "getIdentifier");
188191

189192
await supportBtn.trigger("click");
190193

191194
await flushPromises();
192195

193196
expect(windowOpenMock).not.toHaveBeenCalled();
194-
expect(storeDispatchMock).toHaveBeenCalledWith("support/get", "fake-tenant-data");
197+
expect(storeSpy).toHaveBeenCalledWith("fake-tenant-data");
195198
});
196199
});
Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
import { describe, expect, it } from "vitest";
2-
import { store } from "@/store";
1+
import { createPinia, setActivePinia } from "pinia";
2+
import { describe, expect, it, vi } from "vitest";
3+
import MockAdapter from "axios-mock-adapter";
4+
import { namespacesApi } from "@/api/http";
5+
import useSupportStore from "@/store/modules/support";
6+
7+
vi.mock("@productdevbook/chatwoot/vue", () => ({
8+
useChatWoot: () => ({
9+
reset: vi.fn(),
10+
}),
11+
}));
312

413
describe("Support Store", () => {
5-
it("Returns support with default variables", () => {
6-
expect(store.getters["support/getIdentifier"]).toEqual("");
14+
const mockNamespacesApi = new MockAdapter(namespacesApi.getAxios());
15+
setActivePinia(createPinia());
16+
const supportStore = useSupportStore();
17+
18+
it("should have initial state values", () => {
19+
expect(supportStore.identifier).toEqual("");
20+
expect(supportStore.isChatCreated).toEqual(false);
721
});
822

9-
it("Commits support mutations", () => {
10-
store.commit("support/setIdentifier", "fake-identifier");
11-
expect(store.getters["support/getIdentifier"]).toEqual("fake-identifier");
23+
it("successfully gets identifier", async () => {
24+
const mockResponse = { identifier: "fake-identifier" };
25+
mockNamespacesApi.onGet("http://localhost:3000/api/namespaces/fake-tenant/support").reply(200, mockResponse);
26+
27+
await supportStore.getIdentifier("fake-tenant");
28+
29+
expect(supportStore.identifier).toEqual("fake-identifier");
1230
});
1331
});

0 commit comments

Comments
 (0)