Skip to content

Commit 6c4314a

Browse files
luizhf42gustavosbarreto
authored andcommitted
refactor(ui): refactor admin's Sessions store to setup syntax
1 parent 2b5b4c5 commit 6c4314a

File tree

8 files changed

+132
-182
lines changed

8 files changed

+132
-182
lines changed

ui/admin/src/components/Sessions/SessionList.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ import handleError from "@/utils/handleError";
100100
const router = useRouter();
101101
const snackbar = useSnackbar();
102102
const sessionStore = useSessionsStore();
103-
const sessions = computed(() => sessionStore.getSessions);
104-
const sessionCount = computed(() => sessionStore.getNumberSessions);
103+
const sessions = computed(() => sessionStore.sessions);
104+
const sessionCount = computed(() => sessionStore.sessionCount);
105105
const itemsPerPage = ref(10);
106106
const loading = ref(false);
107107
const page = ref(1);
@@ -147,7 +147,7 @@ const headers = ref([
147147
const fetchSessions = async () => {
148148
try {
149149
loading.value = true;
150-
await sessionStore.fetch({
150+
await sessionStore.fetchSessionList({
151151
perPage: itemsPerPage.value,
152152
page: page.value,
153153
});
Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
11
import { defineStore } from "pinia";
2+
import { ref } from "vue";
23
import { IAdminSession } from "@admin/interfaces/ISession";
3-
import * as apiSession from "../api/sessions";
4-
5-
export const useSessionsStore = defineStore("sessions", {
6-
state: () => ({
7-
sessions: [] as Array<IAdminSession>,
8-
session: {} as IAdminSession,
9-
numberSessions: 0,
10-
}),
11-
12-
getters: {
13-
getSessions: (state) => state.sessions,
14-
getSession: (state) => state.session,
15-
getNumberSessions: (state) => state.numberSessions,
16-
},
17-
18-
actions: {
19-
async fetch(data: { perPage: number; page: number }) {
20-
const res = await apiSession.fetchSessions(data.perPage, data.page);
21-
22-
if (res.data.length) {
23-
this.sessions = res.data as Array<IAdminSession>;
24-
this.numberSessions = parseInt(res.headers["x-total-count"], 10);
25-
return res;
26-
}
27-
28-
return false;
29-
},
30-
31-
async get(uid: string) {
32-
const res = await apiSession.getSession(uid);
33-
this.session = res.data as IAdminSession;
34-
},
35-
36-
clearListSessions() {
37-
this.sessions = [];
38-
this.numberSessions = 0;
39-
},
40-
41-
clearObjectSession() {
42-
this.session = {} as IAdminSession;
43-
},
44-
},
4+
import * as sessionsApi from "../api/sessions";
5+
6+
const useSessionsStore = defineStore("sessions", () => {
7+
const sessions = ref<Array<IAdminSession>>([]);
8+
const sessionCount = ref<number>(0);
9+
10+
const fetchSessionList = async (data: { perPage: number; page: number }) => {
11+
const res = await sessionsApi.fetchSessions(data.perPage, data.page);
12+
13+
sessions.value = res.data as Array<IAdminSession>;
14+
sessionCount.value = parseInt(res.headers["x-total-count"], 10);
15+
};
16+
17+
const fetchSessionById = async (uid: string) => {
18+
const { data } = await sessionsApi.getSession(uid);
19+
return data as IAdminSession;
20+
};
21+
22+
return {
23+
sessions,
24+
sessionCount,
25+
fetchSessionList,
26+
fetchSessionById,
27+
};
4528
});
4629

4730
export default useSessionsStore;

ui/admin/src/views/SessionDetails.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<div>
5252
<div class="text-overline mt-3">
53-
<h3>Ip Adress:</h3>
53+
<h3>Ip Address:</h3>
5454
</div>
5555
<div :data-test="session.ip_address">
5656
<p>{{ session.ip_address }}</p>
@@ -110,25 +110,22 @@ const route = useRoute();
110110
const router = useRouter();
111111
const snackbar = useSnackbar();
112112
const sessionStore = useSessionsStore();
113-
114-
const session = ref({} as IAdminSession);
115113
const sessionId = computed(() => route.params.id);
114+
const session = ref({} as IAdminSession);
115+
const sessionIsEmpty = computed(() => session.value && session.value.device_uid?.length === 0);
116+
117+
const goToDevice = (deviceId: string) => {
118+
router.push({ name: "deviceDetails", params: { id: deviceId } });
119+
};
116120
117121
onMounted(async () => {
118122
try {
119-
await sessionStore.get(sessionId.value as string);
120-
session.value = sessionStore.getSession;
123+
session.value = await sessionStore.fetchSessionById(sessionId.value as string);
121124
} catch {
122125
snackbar.showError("Failed to get session details.");
123126
}
124127
});
125128
126-
const goToDevice = (deviceId: string) => {
127-
router.push({ name: "deviceDetails", params: { id: deviceId } });
128-
};
129-
130-
const sessionIsEmpty = computed(() => sessionStore.getSession && sessionStore.getSession.device_uid?.length === 0);
131-
132129
defineExpose({ session });
133130
</script>
134131

ui/admin/tests/unit/components/Session/SessionList/index.spec.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,15 @@ const sessions = [
6767

6868
describe("Sessions List", () => {
6969
let wrapper: SessionListWrapper;
70+
setActivePinia(createPinia());
71+
const sessionStore = useSessionsStore();
72+
const vuetify = createVuetify();
7073

71-
beforeEach(() => {
72-
setActivePinia(createPinia());
73-
74-
const vuetify = createVuetify();
75-
76-
const sessionStore = useSessionsStore();
77-
78-
sessionStore.sessions = sessions as IAdminSession[];
79-
sessionStore.numberSessions = sessions.length;
80-
sessionStore.fetch = vi.fn();
74+
sessionStore.sessions = sessions as IAdminSession[];
75+
sessionStore.fetchSessionList = vi.fn();
76+
sessionStore.sessionCount = sessions.length;
8177

78+
beforeEach(() => {
8279
wrapper = mount(SessionList, {
8380
global: {
8481
plugins: [vuetify, routes, SnackbarPlugin],
@@ -95,11 +92,9 @@ describe("Sessions List", () => {
9592
});
9693

9794
it("Renders the template with session data", () => {
98-
const dt = wrapper.find("[data-test='session-list']");
99-
expect(dt.exists()).toBe(true);
100-
101-
const store = useSessionsStore();
102-
expect(store.getSessions).toEqual(sessions);
103-
expect(store.getNumberSessions).toBe(sessions.length);
95+
const sessionList = wrapper.find("[data-test='session-list']");
96+
expect(sessionList.exists()).toBe(true);
97+
expect(sessionStore.sessions).toEqual(sessions);
98+
expect(sessionStore.sessionCount).toBe(sessions.length);
10499
});
105100
});
Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,64 @@
1-
import { describe, expect, it, beforeEach } from "vitest";
1+
import { describe, expect, it } from "vitest";
22
import { setActivePinia, createPinia } from "pinia";
33
import useSessionsStore from "@admin/store/modules/sessions";
44
import { IAdminSession } from "@admin/interfaces/ISession";
55

6-
describe("Sessions Pinia Store", () => {
7-
let sessionsStore: ReturnType<typeof useSessionsStore>;
8-
9-
const session = {
10-
uid: "8c354a00f50",
11-
device_uid: "a582b47a42d",
12-
device: {
13-
uid: "a582b47a42d",
14-
name: "39-5e-2a",
15-
identity: {
16-
mac: "00:00:00:00:00:00",
17-
},
18-
info: {
19-
id: "debian",
20-
pretty_name: "Debian GNU/Linux 10 (buster)",
21-
version: "v0.2.5",
22-
},
23-
public_key: "----- PUBLIC KEY -----",
24-
tenant_id: "00000000",
25-
last_seen: "2020-05-18T13:27:02.498Z",
26-
online: false,
27-
namespace: "user",
6+
const session = {
7+
uid: "8c354a00f50",
8+
device_uid: "a582b47a42d",
9+
device: {
10+
uid: "a582b47a42d",
11+
name: "39-5e-2a",
12+
identity: {
13+
mac: "00:00:00:00:00:00",
14+
},
15+
info: {
16+
id: "debian",
17+
pretty_name: "Debian GNU/Linux 10 (buster)",
18+
version: "v0.2.5",
2819
},
20+
public_key: "----- PUBLIC KEY -----",
2921
tenant_id: "00000000",
30-
username: "user",
31-
ip_address: "000.000.000.000",
32-
started_at: "2020-05-18T12:30:28.824Z",
33-
last_seen: "2020-05-18T12:30:30.205Z",
34-
active: false,
35-
authenticated: false,
36-
} as IAdminSession;
22+
last_seen: "2020-05-18T13:27:02.498Z",
23+
online: false,
24+
namespace: "user",
25+
},
26+
tenant_id: "00000000",
27+
username: "user",
28+
ip_address: "000.000.000.000",
29+
started_at: "2020-05-18T12:30:28.824Z",
30+
last_seen: "2020-05-18T12:30:30.205Z",
31+
active: false,
32+
authenticated: false,
33+
} as IAdminSession;
3734

38-
const sessions = [
39-
{ ...session },
40-
{
41-
...session,
42-
device: {
43-
...session.device,
44-
name: "b4-2e-99",
45-
},
35+
const sessions = [
36+
{ ...session },
37+
{
38+
...session,
39+
device: {
40+
...session.device,
41+
name: "b4-2e-99",
4642
},
47-
];
43+
},
44+
];
4845

49-
const numberSessions = 2;
46+
const sessionCount = 2;
5047

51-
beforeEach(() => {
52-
setActivePinia(createPinia());
53-
sessionsStore = useSessionsStore();
54-
});
48+
describe("Sessions Pinia Store", () => {
49+
setActivePinia(createPinia());
50+
const sessionsStore = useSessionsStore();
5551

5652
it("returns default session state", () => {
57-
expect(sessionsStore.getSessions).toEqual([]);
58-
expect(sessionsStore.getSession).toEqual({});
53+
expect(sessionsStore.sessions).toEqual([]);
54+
expect(sessionsStore.sessionCount).toEqual(0);
5955
});
6056

6157
it("sets sessions and total count", () => {
6258
sessionsStore.sessions = sessions;
63-
sessionsStore.numberSessions = numberSessions;
64-
65-
expect(sessionsStore.getSessions).toEqual(sessions);
66-
expect(sessionsStore.getNumberSessions).toEqual(numberSessions);
67-
});
68-
69-
it("sets a single session", () => {
70-
sessionsStore.session = session;
71-
expect(sessionsStore.getSession).toEqual(session);
72-
});
73-
74-
it("clears the session object", () => {
75-
sessionsStore.session = session;
76-
sessionsStore.clearObjectSession();
77-
expect(sessionsStore.getSession).toEqual({});
78-
});
79-
80-
it("clears the sessions list", () => {
81-
sessionsStore.sessions = sessions;
82-
sessionsStore.numberSessions = numberSessions;
83-
84-
sessionsStore.clearListSessions();
59+
sessionsStore.sessionCount = sessionCount;
8560

86-
expect(sessionsStore.getSessions).toEqual([]);
87-
expect(sessionsStore.getNumberSessions).toEqual(0);
61+
expect(sessionsStore.sessions).toEqual(sessions);
62+
expect(sessionsStore.sessionCount).toEqual(sessionCount);
8863
});
8964
});

0 commit comments

Comments
 (0)