From 2758dcf2c8d75f4c9b85e55394779676cda9fdfe Mon Sep 17 00:00:00 2001 From: wbuezas Date: Wed, 11 Sep 2024 14:46:45 -0300 Subject: [PATCH 1/4] WIP --- src/App.vue | 7 ++++- src/i18n/ca-ES/index.js | 21 ++++++++++++- src/i18n/en-US/index.js | 19 +++++++++++- src/i18n/fr-FR/index.js | 22 +++++++++++++- src/i18n/pt-PT/index.js | 22 +++++++++++++- src/layouts/MainLayout.vue | 4 +-- src/pages/Login/LoginView.vue | 55 ++++++++++++++++++++--------------- src/stores/user.js | 39 +++++++++++++++++++++++-- 8 files changed, 155 insertions(+), 34 deletions(-) diff --git a/src/App.vue b/src/App.vue index 8b15cff2..1e865e99 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,9 +1,14 @@ -
+
- - - - - {{ $t(`langs.${lang}`) }} - - - - +
diff --git a/src/stores/user.js b/src/stores/user.js index c7df4d12..a446a199 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -1,5 +1,8 @@ import { defineStore } from 'pinia'; import { api, jApi } from 'boot/axios'; +import { i18n } from 'src/boot/i18n'; + +const { t } = i18n.global; export const useUserStore = defineStore('user', { state: () => { @@ -11,20 +14,36 @@ export const useUserStore = defineStore('user', { token, isGuest: false, user: null, - supplantedUser: null + supplantedUser: null, + localeOptions: [ + { label: t('langs.en'), lang: 'en-US', value: 'en' }, + { label: t('langs.es'), lang: 'es-ES', value: 'es' }, + { label: t('langs.ca'), lang: 'ca-ES', value: 'ca' }, + { label: t('langs.fr'), lang: 'fr-FR', value: 'fr' }, + { label: t('langs.pt'), lang: 'pt-PT', value: 'pt' } + ] }; }, getters: { - loggedIn: state => state.token != null + loggedIn: state => state.token != null, + userLang: state => + state.localeOptions?.find(l => l.value === state?.user?.lang)?.lang }, actions: { + async init() { + await this.fetchUser(); + await this.supplantInit(); + i18n.global.locale.value = this.userLang || 'en-US'; + }, + async getToken() { this.token = sessionStorage.getItem('vnToken') || localStorage.getItem('vnToken'); }, + async login(user, password, remember) { const params = { user, password }; const res = await api.post('Accounts/login', params); @@ -40,6 +59,7 @@ export const useUserStore = defineStore('user', { name: user }); }, + async logout() { if (this.token != null) { try { @@ -54,7 +74,7 @@ export const useUserStore = defineStore('user', { async fetchUser(userType = 'user') { try { const userData = await jApi.getObject( - 'SELECT id, nickname, name FROM account.myUser' + 'SELECT id, nickname, name, lang FROM account.myUser' ); this.$patch({ [userType]: userData }); } catch (error) { @@ -83,6 +103,19 @@ export const useUserStore = defineStore('user', { await api.post('Accounts/logout'); this.getToken(); await this.fetchUser(); + }, + + async updateUserLang(lang) { + console.log('lang to update: ', lang); + if (!this.user || this.user.lang === lang) return; + await jApi.execQuery( + `START TRANSACTION; + UPDATE account.myUser SET lang = #lang WHERE (id = #id); + SELECT lang FROM account.myUser WHERE (id = #id); + COMMIT;`, + { id: this.user.id, lang } + ); + this.user.lang = lang; } } }); From f1a8fd27100e4979eb3b0147fc5abfe63c866794 Mon Sep 17 00:00:00 2001 From: wbuezas Date: Fri, 13 Sep 2024 21:42:40 -0300 Subject: [PATCH 2/4] Integrate update config language dropdown --- src/pages/Account/AccountConfig.vue | 4 +++- src/stores/user.js | 16 ++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/src/pages/Account/AccountConfig.vue b/src/pages/Account/AccountConfig.vue index 7f1694ae..5f22f0fb 100644 --- a/src/pages/Account/AccountConfig.vue +++ b/src/pages/Account/AccountConfig.vue @@ -99,7 +99,9 @@ onMounted(() => fetchLanguagesSql()); option-label="name" option-value="code" :options="langOptions" - @update:model-value="vnFormRef.submit()" + @update:model-value=" + userStore.updateUserLang(data.lang) + " />