Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: centralized product store selector (#193) #200

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/components/ProductStoreSelector.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<template>
<ion-card>
<ion-card-header>
<ion-card-subtitle>
{{ $t("Product Store") }}
</ion-card-subtitle>
<ion-card-title>
{{ $t("Store") }}
</ion-card-title>
</ion-card-header>

<ion-card-content>
{{ $t('A store represents a company or a unique catalog of products. If your OMS is connected to multiple eCommerce stores selling different collections of products, you may have multiple Product Stores set up in HotWax Commerce.') }}
</ion-card-content>

<ion-item lines="none">
<ion-label> {{ $t("Select store") }} </ion-label>
<ion-select interface="popover" :placeholder="$t('store name')" :value="currentEComStore?.productStoreId" @ionChange="setEComStore($event)">
<ion-select-option v-for="store in (productStores ? productStores : [])" :key="store.productStoreId" :value="store.productStoreId">{{ store.storeName }}</ion-select-option>
</ion-select>
</ion-item>
</ion-card>
</template>

<script setup lang="ts">
import { IonCard, IonCardContent, IonCardHeader, IonCardTitle, IonCardSubtitle, IonItem, IonLabel, IonSelect, IonSelectOption } from '@ionic/vue';
import { useUserStore } from 'src';
import { computed } from 'vue';

const userStore = useUserStore();

const productStores = computed(() => userStore.current?.stores);
const currentEComStore = computed(() => userStore.currentEComStore);

const setEComStore = (event: any) => {
if (currentEComStore.value?.productStoreId !== event.detail.value) {
userStore.setEComStore({ eComStore: productStores.value.find((store: any) => store.productStoreId == event.detail.value) })
}
}
</script>
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export { default as AppVersionInfo } from './AppVersionInfo.vue';
export { default as LanguageSwitcher } from './LanguageSwitcher.vue';
export { default as OmsInstanceNavigator } from './OmsInstanceNavigator.vue'
export { default as ProductIdentifier } from "./ProductIdentifier.vue";
export { default as ProductStoreSelector } from "./ProductStoreSelector.vue";
export { default as Scanner } from './Scanner.vue';
export { default as ShopifyImg } from './ShopifyImg.vue';
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ declare var process: any;
import { createPinia } from "pinia";
import { useProductIdentificationStore } from "./store/productIdentification";
import { useAuthStore } from "./store/auth";
import { AppVersionInfo, LanguageSwitcher, OmsInstanceNavigator, ProductIdentifier, Scanner, ShopifyImg } from "./components";
import { AppVersionInfo, LanguageSwitcher, OmsInstanceNavigator, ProductIdentifier, ProductStoreSelector, Scanner, ShopifyImg } from "./components";
import Login from "./components/Login";
import { goToOms, getProductIdentificationValue } from "./utils";
import { initialiseFirebaseApp } from "./utils/firebase"
Expand Down Expand Up @@ -46,6 +46,7 @@ export let dxpComponents = {
app.component('Login', Login)
app.component('OmsInstanceNavigator', OmsInstanceNavigator)
app.component('ProductIdentifier', ProductIdentifier)
app.component('ProductStoreSelector', ProductStoreSelector)
app.component('Scanner', Scanner)
app.component('ShopifyImg', ShopifyImg)

Expand Down
7 changes: 7 additions & 0 deletions src/store/auth.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
declare const process: any;

import { defineStore } from "pinia";
import { DateTime } from 'luxon'

Expand All @@ -14,6 +16,11 @@ export const useAuthStore = defineStore('userAuth', {
getters: {
getToken: (state) => state.token,
getOms: (state) => state.oms,
getBaseUrl: (state) => {
let baseURL = process.env.VUE_APP_BASE_URL;
if (!baseURL) baseURL = state.oms;
return baseURL.startsWith('http') ? baseURL : `https://${baseURL}.hotwax.io/api/`;
},
isAuthenticated: (state) => {
let isTokenExpired = false
if (state.token.expiration) {
Expand Down
96 changes: 94 additions & 2 deletions src/store/user.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,112 @@
import { defineStore } from "pinia";
import { i18n } from "../../src";
import { i18n, useAuthStore } from "../../src";
import { api, client, hasError } from '@hotwax/oms-api'

declare let process: any;

export const useUserStore = defineStore('user', {
state: () => {
return {
current: {} as any,
currentEComStore: {} as any,
locale: 'en',
localeOptions: process.env.VUE_APP_LOCALES ? JSON.parse(process.env.VUE_APP_LOCALES) : { "en": "English" }
}
},
getters: {
getCurrentEComStore: (state) => state.currentEComStore,
getLocale: (state) => state.locale,
getLocaleOptions: (state) => state.localeOptions
getLocaleOptions: (state) => state.localeOptions,
getUserProfile: (state) => state.current
},
actions: {
async getEComStores(facilityId: string) {
const authStore = useAuthStore();

// If the facilityId is undefined, it may be case of user not associated with any facility
if (!facilityId) {
this.current.stores = [];
}

try {
const params = {
"inputFields": {
"storeName_op": "not-empty",
"facilityId": facilityId
},
"fieldList": ["productStoreId", "storeName"],
"entityName": "ProductStoreFacilityDetail",
"distinct": "Y",
"noConditionFind": "Y",
"filterByDate": 'Y',
}
const resp = await client({
url: "performFind",
method: "get",
baseURL: authStore.getBaseUrl,
params,
headers: {
Authorization: 'Bearer ' + authStore.getToken.value,
'Content-Type': 'application/json'
}
});
if (hasError(resp) || resp.data.docs.length === 0) {
return Promise.reject(resp.data)
}
this.current.stores = resp.data.docs;
} catch (error: any) {
return Promise.reject(error)
}
},
async getPreferredStore() {
const authStore = useAuthStore();
let preferredStore = {} as any;

// Handling case if stores are not present, it may be case of user not associated with any facility
if (this.current.stores.length) {
preferredStore = this.current.stores[0];
let preferredStoreId = '';

try {
const resp = await client({
url: "service/getUserPreference",
method: "post",
data: {
'userPrefTypeId': 'SELECTED_BRAND'
},
baseURL: authStore.getBaseUrl,
headers: {
Authorization: 'Bearer ' + authStore.getToken.value,
'Content-Type': 'application/json'
}
});
if (hasError(resp)) {
return Promise.reject(resp.data);
}
preferredStoreId = resp.data.userPrefValue;
} catch (error: any) {
return Promise.reject(error)
}

if (preferredStoreId) {
const store = this.current.stores.find((store: any) => store.productStoreId === preferredStoreId);
store && (preferredStore = store)
}
}

this.currentEComStore = preferredStore;
},
async setEComStore(payload: any) {
await api({
url: "service/setUserPreference",
method: "post",
data: {
'userPrefTypeId': 'SELECTED_BRAND',
'userPrefValue': payload.eComStore.productStoreId
}
});
this.currentEComStore = payload.eComStore;
},
setLocale(payload: string) {
// update locale in state and globally
i18n.global.locale.value = payload
Expand Down
Loading