From a467b18391343971bad7bde55de860f6894f97dd Mon Sep 17 00:00:00 2001 From: k2maan Date: Thu, 12 Oct 2023 21:29:47 +0530 Subject: [PATCH 1/2] Fixed: login failing on missing locale preference in userProfile --- src/store/user.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/store/user.ts b/src/store/user.ts index d894434..32e86fe 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -16,10 +16,16 @@ export const useUserStore = defineStore('user', { }, actions: { async setLocale(locale: string) { - let matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale) - // If exact locale is not found, try to match the first two characters i.e primary code - matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2)) - const newLocale = matchingLocale || this.locale + let newLocale, matchingLocale + // handling if locale is not coming from userProfile + if (!locale) { + newLocale = this.locale + } else { + matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale) + // If exact locale is not found, try to match the first two characters i.e primary code + matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2)) + newLocale = matchingLocale || this.locale + } try { // update locale in state and globally From 66ee00d6f0eb65720158b8fce1a83f20784defc7 Mon Sep 17 00:00:00 2001 From: k2maan Date: Mon, 16 Oct 2023 12:02:09 +0530 Subject: [PATCH 2/2] Improved: condition checks for setting locale (#196) --- src/store/user.ts | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/store/user.ts b/src/store/user.ts index 32e86fe..1dea5a0 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -17,23 +17,22 @@ export const useUserStore = defineStore('user', { actions: { async setLocale(locale: string) { let newLocale, matchingLocale + newLocale = this.locale // handling if locale is not coming from userProfile - if (!locale) { - newLocale = this.locale - } else { - matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale) - // If exact locale is not found, try to match the first two characters i.e primary code - matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2)) - newLocale = matchingLocale || this.locale - } - try { - // update locale in state and globally - i18n.global.locale.value = newLocale - this.locale = newLocale - await userContext.setUserLocale({ newLocale }) + if (locale) { + matchingLocale = Object.keys(this.localeOptions).find((option: string) => option === locale) + // If exact locale is not found, try to match the first two characters i.e primary code + matchingLocale = matchingLocale || Object.keys(this.localeOptions).find((option: string) => option.slice(0, 2) === locale.slice(0, 2)) + newLocale = matchingLocale || this.locale + // update locale in state and globally + await userContext.setUserLocale({ newLocale }) + } } catch (error) { console.error(error) + } finally { + i18n.global.locale.value = newLocale + this.locale = newLocale } } },