From 2758dcf2c8d75f4c9b85e55394779676cda9fdfe Mon Sep 17 00:00:00 2001 From: wbuezas Date: Wed, 11 Sep 2024 14:46:45 -0300 Subject: [PATCH] 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; } } });