From a6a8888ab6f136c69d6cc48b68137ccb2df3ecd7 Mon Sep 17 00:00:00 2001 From: Yash Maheshwari Date: Fri, 22 Mar 2024 16:24:15 +0530 Subject: [PATCH] Implemented: support for time zone switcher component(#139) --- src/components/DxpTimeZoneSwitcher.vue | 152 ++++++++++++++++++++++--- src/components/TimeZoneModal.vue | 11 -- src/index.ts | 4 + src/store/user.ts | 46 +++++++- 4 files changed, 185 insertions(+), 28 deletions(-) delete mode 100644 src/components/TimeZoneModal.vue diff --git a/src/components/DxpTimeZoneSwitcher.vue b/src/components/DxpTimeZoneSwitcher.vue index a5f279f..c75b665 100644 --- a/src/components/DxpTimeZoneSwitcher.vue +++ b/src/components/DxpTimeZoneSwitcher.vue @@ -9,35 +9,159 @@ {{ $t('The timezone you select is used to ensure automations you schedule are always accurate to the time you select.') }} - {{ userProfile && userProfile.userTimeZone ? userProfile.userTimeZone : '-' }} - {{ $t("Change") }} + {{ currentTimeZoneId }} + {{ $t("Change") }} + + + + + + + + + + {{ $t("Select time zone") }} + + + + + + + + +
+ + + {{ $t("Fetching time zones") }} + +
+
+

{{ $t("No time zone found") }}

+
+ + +
+ + + + {{ timeZone.label }} ({{ timeZone.id }}) + + + +
+ + + + + + +
+
\ No newline at end of file diff --git a/src/components/TimeZoneModal.vue b/src/components/TimeZoneModal.vue deleted file mode 100644 index 1dd0367..0000000 --- a/src/components/TimeZoneModal.vue +++ /dev/null @@ -1,11 +0,0 @@ - - - \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index e3e236c..fb9927f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -88,6 +88,10 @@ export let dxpComponents = { userContext.setUserLocale = options.setUserLocale + // TimeZone specific api from oms-api package exposed by the app + userContext.setUserTimeZone = options.setUserTimeZone + userContext.getAvailableTimeZones = options.getAvailableTimeZones + productIdentificationContext.getProductIdentificationPref = options.getProductIdentificationPref productIdentificationContext.setProductIdentificationPref = options.setProductIdentificationPref diff --git a/src/store/user.ts b/src/store/user.ts index e8b02f1..1ec6216 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -1,5 +1,8 @@ import { defineStore } from "pinia"; -import { i18n, userContext } from "../../src"; +import { appContext, i18n, translate, userContext } from "../../src"; +import { hasError } from "@hotwax/oms-api"; +import { DateTime } from "luxon"; +import { showToast } from "src/utils"; declare let process: any; @@ -7,12 +10,16 @@ export const useUserStore = defineStore('user', { state: () => { return { localeOptions: process.env.VUE_APP_LOCALES ? JSON.parse(process.env.VUE_APP_LOCALES) : { "en-US": "English" }, - locale: 'en-US' + locale: 'en-US', + currentTimeZoneId: '', + timeZones: [] } }, getters: { getLocale: (state) => state.locale, - getLocaleOptions: (state) => state.localeOptions + getLocaleOptions: (state) => state.localeOptions, + getTimeZones: (state) => state.timeZones, + getCurrentTimeZone: (state) => state.currentTimeZoneId }, actions: { async setLocale(locale: string) { @@ -34,6 +41,39 @@ export const useUserStore = defineStore('user', { i18n.global.locale.value = newLocale this.locale = newLocale } + }, + async setUserTimeZone(tzId: string) { + // Do not make any api call if the user clicks the same timeZone again that is already selected + if(this.currentTimeZoneId === tzId) { + return; + } + + try { + await userContext.setUserTimeZone({ tzId }) + this.currentTimeZoneId = tzId + + showToast(translate("Time zone updated successfully")); + return Promise.resolve(tzId) + } catch(err) { + console.error('Error', err) + return Promise.reject('') + } + }, + async getAvailableTimeZones() { + // Do not fetch timeZones information, if already available + if(this.timeZones.length) { + return; + } + + try { + const resp = await userContext.getAvailableTimeZones() + this.timeZones = resp.filter((timeZone: any) => DateTime.local().setZone(timeZone.id).isValid); + } catch(err) { + console.error('Error', err) + } + }, + updateTimeZone(tzId: string) { + this.currentTimeZoneId = tzId } }, persist: true