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}`) }} - - - -
@@ -177,28 +188,3 @@ a { } } - - -en-US: - user: User - password: Password - remindMe: Remind me - logInAsGuest: Log in as guest - logIn: Log in - loginMail: infoverdnatura.es - loginPhone: +34 607 562 391 - haveForgottenPassword: Have you forgotten your password? - notACustomerYet: Not a customer yet? - signUp: Register -es-ES: - user: Usuario - password: Contraseña - remindMe: Recuérdame - logInAsGuest: Entrar como invitado - logIn: Iniciar sesión - loginMail: infoverdnatura.es - loginPhone: +34 963 242 100 - haveForgottenPassword: ¿Has olvidado tu contraseña? - notACustomerYet: ¿Todavía no eres cliente? - signUp: Registrarme - diff --git a/src/stores/user.js b/src/stores/user.js index c7df4d12..fe662e86 100644 --- a/src/stores/user.js +++ b/src/stores/user.js @@ -1,5 +1,10 @@ import { defineStore } from 'pinia'; import { api, jApi } from 'boot/axios'; +import { i18n } from 'src/boot/i18n'; +import useNotify from 'src/composables/useNotify.js'; + +const { t } = i18n.global; +const { notify } = useNotify(); export const useUserStore = defineStore('user', { state: () => { @@ -11,20 +16,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, + userLocaleOption: state => + state.localeOptions?.find(l => l.value === state?.user?.lang) }, actions: { + async init() { + await this.fetchUser(); + await this.supplantInit(); + this.updateSiteLocale(); + }, + 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 +61,7 @@ export const useUserStore = defineStore('user', { name: user }); }, + async logout() { if (this.token != null) { try { @@ -54,7 +76,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 +105,19 @@ export const useUserStore = defineStore('user', { await api.post('Accounts/logout'); this.getToken(); await this.fetchUser(); + }, + + updateSiteLocale(locale = null) { + i18n.global.locale.value = + locale || this.userLocaleOption.lang || 'en-US'; + }, + + async updateUserLang(lang) { + if (!this.user || this.user.lang === lang) return; + + this.user.lang = lang; + this.updateSiteLocale(); + notify(t('dataSaved'), 'positive'); } } });