diff --git a/src/components/DxpFacilitySwitcher.vue b/src/components/DxpFacilitySwitcher.vue new file mode 100644 index 0000000..edf7ea5 --- /dev/null +++ b/src/components/DxpFacilitySwitcher.vue @@ -0,0 +1,162 @@ + + + + + \ No newline at end of file diff --git a/src/components/index.ts b/src/components/index.ts index 131608c..83300c3 100644 --- a/src/components/index.ts +++ b/src/components/index.ts @@ -15,6 +15,7 @@ import '@ionic/vue/css/flex-utils.css'; import '@ionic/vue/css/display.css'; export { default as DxpAppVersionInfo } from './DxpAppVersionInfo.vue'; +export { default as DxpFacilitySwitcher } from './DxpFacilitySwitcher.vue' export { default as DxpGitBookSearch } from './DxpGitBookSearch.vue'; export { default as DxpImage } from './DxpImage.vue'; export { default as DxpLanguageSwitcher } from './DxpLanguageSwitcher.vue'; diff --git a/src/index.ts b/src/index.ts index dfdae45..26b71bf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ declare var process: any; import { createPinia } from "pinia"; import { useProductIdentificationStore } from "./store/productIdentification"; import { useAuthStore } from "./store/auth"; -import { DxpAppVersionInfo, DxpGitBookSearch, DxpImage, DxpLanguageSwitcher, DxpLogin, DxpMenuFooterNavigation, DxpOmsInstanceNavigator, DxpProductIdentifier, DxpShopifyImg, DxpTimeZoneSwitcher, DxpUserProfile } from "./components"; +import { DxpAppVersionInfo, DxpFacilitySwitcher, DxpGitBookSearch, DxpImage, DxpLanguageSwitcher, DxpLogin, DxpMenuFooterNavigation, DxpOmsInstanceNavigator, DxpProductIdentifier, DxpShopifyImg, DxpTimeZoneSwitcher, DxpUserProfile } from "./components"; import { goToOms, getProductIdentificationValue } from "./utils"; import { initialiseFirebaseApp } from "./utils/firebase" import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' @@ -25,6 +25,7 @@ let loginContext = {} as any let shopifyImgContext = {} as any let appContext = {} as any let productIdentificationContext = {} as any +let facilityContext = {} as any let notificationContext = {} as any let gitBookContext = {} as any let userContext = {} as any @@ -67,6 +68,7 @@ export let dxpComponents = { }) app.component('DxpAppVersionInfo', DxpAppVersionInfo) + app.component('DxpFacilitySwitcher', DxpFacilitySwitcher) app.component('DxpGitBookSearch', DxpGitBookSearch) app.component('DxpImage', DxpImage) app.component('DxpLanguageSwitcher', DxpLanguageSwitcher) @@ -96,6 +98,10 @@ export let dxpComponents = { productIdentificationContext.getProductIdentificationPref = options.getProductIdentificationPref productIdentificationContext.setProductIdentificationPref = options.setProductIdentificationPref + + facilityContext.getUserFacilities = options.getUserFacilities + facilityContext.setUserPreference = options.setUserPreference + facilityContext.getUserPreference = options.getUserPreference notificationContext.addNotification = options.addNotification notificationContext.appFirebaseConfig = options.appFirebaseConfig @@ -138,6 +144,7 @@ export { loginContext, notificationContext, productIdentificationContext, + facilityContext, shopifyImgContext, translate, useAuthStore, diff --git a/src/store/auth.ts b/src/store/auth.ts index d8a8ca8..c9f2cf8 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -14,6 +14,10 @@ export const useAuthStore = defineStore('userAuth', { getters: { getToken: (state) => state.token, getOms: (state) => state.oms, + getBaseUrl: (state) => { + let baseURL = state.oms + return baseURL.startsWith('http') ? baseURL.includes('/api') ? baseURL : `${baseURL}/api/` : `https://${baseURL}.hotwax.io/api/`; + }, isAuthenticated: (state) => { let isTokenExpired = false if (state.token.expiration) { diff --git a/src/store/user.ts b/src/store/user.ts index 1ec6216..488a374 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -1,8 +1,8 @@ import { defineStore } from "pinia"; -import { appContext, i18n, translate, userContext } from "../../src"; -import { hasError } from "@hotwax/oms-api"; +import { i18n, translate, userContext, useAuthStore } from "../../src"; import { DateTime } from "luxon"; import { showToast } from "src/utils"; +import { facilityContext } from "../index"; declare let process: any; @@ -12,14 +12,18 @@ export const useUserStore = defineStore('user', { localeOptions: process.env.VUE_APP_LOCALES ? JSON.parse(process.env.VUE_APP_LOCALES) : { "en-US": "English" }, locale: 'en-US', currentTimeZoneId: '', - timeZones: [] + timeZones: [], + facilities: [], + currentFacility: {} as any } }, getters: { getLocale: (state) => state.locale, getLocaleOptions: (state) => state.localeOptions, getTimeZones: (state) => state.timeZones, - getCurrentTimeZone: (state) => state.currentTimeZoneId + getCurrentTimeZone: (state) => state.currentTimeZoneId, + getFacilites: (state) => state.facilities, + getCurrentFacility: (state) => state.currentFacility }, actions: { async setLocale(locale: string) { @@ -74,7 +78,52 @@ export const useUserStore = defineStore('user', { }, updateTimeZone(tzId: string) { this.currentTimeZoneId = tzId - } + }, + + async getUserFacilities(partyId: any, facilityGroupId: any, isAdminUser: boolean) { + const authStore = useAuthStore(); + + try { + const response = await facilityContext.getUserFacilities(authStore.getToken.value, authStore.getBaseUrl, partyId, facilityGroupId, isAdminUser); + this.facilities = response; + } catch (error) { + console.error(error); + } + return this.facilities + }, + + async setFacilityPreference(payload: any) { + + try { + await facilityContext.setUserPreference({ + userPrefTypeId: 'SELECTED_FACILITY', + userPrefValue: payload.facilityId + }) + } catch (error) { + console.error('error', error) + } + this.currentFacility = payload; + }, + + async getFacilityPreference(userPrefTypeId: any) { + const authStore = useAuthStore(); + + if (!this.facilities.length) { + return; + } + let preferredFacility = this.facilities[0]; + + try { + let preferredFacilityId = await facilityContext.getUserPreference(authStore.getToken.value, authStore.getBaseUrl, userPrefTypeId); + if(preferredFacilityId) { + const facility = this.facilities.find((facility: any) => facility.facilityId === preferredFacilityId); + facility && (preferredFacility = facility) + } + } catch (error) { + console.error(error); + } + this.currentFacility = preferredFacility; + }, }, persist: true })