From 1627f9cd36951e07412c74165eddfaac5312f392 Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 11 May 2023 08:55:34 +0200 Subject: [PATCH 1/4] refs #5655 added useUserConfig --- src/composables/useRole.js | 4 +++- src/composables/useUserConfig.js | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 src/composables/useUserConfig.js diff --git a/src/composables/useRole.js b/src/composables/useRole.js index a781785a0..0184b23da 100644 --- a/src/composables/useRole.js +++ b/src/composables/useRole.js @@ -1,4 +1,5 @@ import { useState } from './useState'; +import { useUserConfig } from './useUserConfig'; import axios from 'axios'; export function useRole() { @@ -6,6 +7,7 @@ export function useRole() { async function fetch() { const { data } = await axios.get('VnUsers/acl'); + const userConfig = await useUserConfig().fetch(); const roles = data.roles.map((userRoles) => userRoles.role.name); const userData = { @@ -13,7 +15,7 @@ export function useRole() { name: data.user.name, nickname: data.user.nickname, lang: data.user.lang || 'es', - darkMode: data.user.userConfig.darkMode, + darkMode: userConfig.darkMode, }; state.setUser(userData); state.setRoles(roles); diff --git a/src/composables/useUserConfig.js b/src/composables/useUserConfig.js new file mode 100644 index 000000000..0e4837aeb --- /dev/null +++ b/src/composables/useUserConfig.js @@ -0,0 +1,13 @@ +import axios from 'axios'; + +export function useUserConfig() { + + async function fetch() { + const { data } = await axios.get('UserConfigs/getUserConfig'); + return data; + } + + return { + fetch, + }; +} From c4f40fbb597f12cbd522ce64fa4a49108d19c57f Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 11 May 2023 09:05:48 +0200 Subject: [PATCH 2/4] refs #5655 fix unit test --- test/vitest/__tests__/composables/useRole.spec.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/test/vitest/__tests__/composables/useRole.spec.js b/test/vitest/__tests__/composables/useRole.spec.js index 285eb4861..9d06f73d8 100644 --- a/test/vitest/__tests__/composables/useRole.spec.js +++ b/test/vitest/__tests__/composables/useRole.spec.js @@ -23,9 +23,6 @@ describe('useRole', () => { name: `T'Challa`, nickname: 'Black Panther', lang: 'en', - userConfig: { - darkMode: false, - }, }; const expectedUser = { id: 999, @@ -35,8 +32,12 @@ describe('useRole', () => { darkMode: false, }; const expectedRoles = ['salesPerson', 'admin']; - vi.spyOn(axios, 'get').mockResolvedValue({ + vi.spyOn(axios, 'get') + .mockResolvedValueOnce({ data: { roles: rolesData, user: fetchedUser }, + }) + .mockResolvedValueOnce({ + data: { darkMode: false }, }); vi.spyOn(role.state, 'setUser'); From e254a1e7210057cd19afb3af92b33188dd32c75b Mon Sep 17 00:00:00 2001 From: alexandre Date: Thu, 11 May 2023 14:51:12 +0200 Subject: [PATCH 3/4] refs #5655 useRole and useUserConfig independents --- src/composables/useRole.js | 3 --- src/composables/useSession.js | 7 ++++--- src/composables/useUserConfig.js | 6 +++++- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/composables/useRole.js b/src/composables/useRole.js index 0184b23da..95b585283 100644 --- a/src/composables/useRole.js +++ b/src/composables/useRole.js @@ -1,5 +1,4 @@ import { useState } from './useState'; -import { useUserConfig } from './useUserConfig'; import axios from 'axios'; export function useRole() { @@ -7,7 +6,6 @@ export function useRole() { async function fetch() { const { data } = await axios.get('VnUsers/acl'); - const userConfig = await useUserConfig().fetch(); const roles = data.roles.map((userRoles) => userRoles.role.name); const userData = { @@ -15,7 +13,6 @@ export function useRole() { name: data.user.name, nickname: data.user.nickname, lang: data.user.lang || 'es', - darkMode: userConfig.darkMode, }; state.setUser(userData); state.setRoles(roles); diff --git a/src/composables/useSession.js b/src/composables/useSession.js index 62cb8a28b..008337b02 100644 --- a/src/composables/useSession.js +++ b/src/composables/useSession.js @@ -1,5 +1,7 @@ import { useState } from './useState'; import { useRole } from './useRole'; +import { useUserConfig } from './useUserConfig'; + export function useSession() { function getToken() { @@ -36,11 +38,10 @@ export function useSession() { } async function login(token, keepLogin) { - const { fetch } = useRole(); - setToken({ token, keepLogin }); - await fetch(); + await useRole().fetch(); + await useUserConfig().fetch(); } function isLoggedIn() { diff --git a/src/composables/useUserConfig.js b/src/composables/useUserConfig.js index 0e4837aeb..767ffb54e 100644 --- a/src/composables/useUserConfig.js +++ b/src/composables/useUserConfig.js @@ -1,10 +1,14 @@ import axios from 'axios'; +import { useState } from './useState'; export function useUserConfig() { + const state = useState(); async function fetch() { const { data } = await axios.get('UserConfigs/getUserConfig'); - return data; + const user = state.getUser().value; + user.darkMode = data.darkMode; + state.setUser(user); } return { From e2586bd6e1a5557712625b45c5b76aea62119702 Mon Sep 17 00:00:00 2001 From: alexandre Date: Fri, 12 May 2023 09:15:45 +0200 Subject: [PATCH 4/4] refs #5655 fix test --- src/router/index.js | 7 ++++--- test/vitest/__tests__/composables/useRole.spec.js | 4 ---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 4eeee8fbb..9bc199047 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -10,11 +10,11 @@ import { i18n } from 'src/boot/i18n'; import { useState } from 'src/composables/useState'; import { useSession } from 'src/composables/useSession'; import { useRole } from 'src/composables/useRole'; +import { useUserConfig } from 'src/composables/useUserConfig'; import { toLowerCamel } from 'src/filters'; const state = useState(); const session = useSession(); -const role = useRole(); const { t } = i18n.global; const createHistory = process.env.SERVER @@ -53,12 +53,13 @@ export default route(function (/* { store, ssrContext } */) { if (isLoggedIn()) { const stateRoles = state.getRoles().value; if (stateRoles.length === 0) { - await role.fetch(); + await useRole().fetch(); + await useUserConfig().fetch(); } const matches = to.matched; const hasRequiredRoles = matches.every((route) => { const meta = route.meta; - if (meta && meta.roles) return role.hasAny(meta.roles); + if (meta && meta.roles) return useRole().hasAny(meta.roles); return true; }); diff --git a/test/vitest/__tests__/composables/useRole.spec.js b/test/vitest/__tests__/composables/useRole.spec.js index 9d06f73d8..d0bca5342 100644 --- a/test/vitest/__tests__/composables/useRole.spec.js +++ b/test/vitest/__tests__/composables/useRole.spec.js @@ -29,16 +29,12 @@ describe('useRole', () => { name: `T'Challa`, nickname: 'Black Panther', lang: 'en', - darkMode: false, }; const expectedRoles = ['salesPerson', 'admin']; vi.spyOn(axios, 'get') .mockResolvedValueOnce({ data: { roles: rolesData, user: fetchedUser }, }) - .mockResolvedValueOnce({ - data: { darkMode: false }, - }); vi.spyOn(role.state, 'setUser'); vi.spyOn(role.state, 'setRoles');