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; } } });