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

Refactored: setEcomStore and setShopifyConfig actions to accept ID. (#2tzh44d) #241

Merged
merged 11 commits into from
Oct 10, 2022
Merged
28 changes: 21 additions & 7 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,20 @@ const actions: ActionTree<UserState, RootState> = {
/**
* update current eComStore information
*/
async setEcomStore({ commit, dispatch }, payload) {
async setEcomStore({ commit, dispatch, getters }, payload) {
dispatch('job/clearJobState', null, { root: true });
commit(types.USER_CURRENT_ECOM_STORE_UPDATED, payload.eComStore);
dispatch('getShopifyConfig', payload.eComStore.productStoreId);
let productStore;

if(payload.productStore) {
commit(types.USER_CURRENT_ECOM_STORE_UPDATED, payload.productStore);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should commit in both the cases, here we should assign productStore with payload.productStore

} else {
productStore = getters.getUserProfile.stores.find((store: any) => store.productStoreId === payload.productStoreId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to use getter, I think we could directly access the state

}

dispatch('getShopifyConfig', productStore ? productStore.productStoreId : payload.productStore.productStoreId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we have productStore set for both the cases, we will not need this ternary operation we could use productStore.productStoreId for both the cases

await UserService.setUserPreference({
'userPrefTypeId': 'SELECTED_BRAND',
'userPrefValue': payload.eComStore.productStoreId
'userPrefValue': productStore ? productStore.productStoreId : payload.productStore.productStoreId
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. refer my previous comment

});
},
/**
Expand Down Expand Up @@ -204,9 +211,16 @@ const actions: ActionTree<UserState, RootState> = {
/**
* update current shopify config id
*/
async setCurrentShopifyConfig({ commit, dispatch }, shopifyConfig) {
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, shopifyConfig ? shopifyConfig : {});
dispatch('job/clearJobState', null, { root: true });
async setCurrentShopifyConfig({ commit, dispatch, state }, payload) {
if(payload.shopifyConfig) {
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, payload.shopifyConfig);
} else {
const shopifyConfigs = state.shopifyConfigs;
const currentShopifyConfig = shopifyConfigs.find((configs: any) => configs.shopifyConfigId === payload.shopifyConfigId)
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, currentShopifyConfig);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest following code for better readability

Suggested change
if(payload.shopifyConfig) {
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, payload.shopifyConfig);
} else {
const shopifyConfigs = state.shopifyConfigs;
const currentShopifyConfig = shopifyConfigs.find((configs: any) => configs.shopifyConfigId === payload.shopifyConfigId)
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, currentShopifyConfig);
}
let currentShopifyConfig = payload.shopifyConfig;
if(!currentShopifyConfig) {
currentShopifyConfig = state.shopifyConfigs.find((shopifyConfig: any) => shopifyConfig.shopifyConfigId === payload.shopifyConfigId)
}
commit(types.USER_CURRENT_SHOPIFY_CONFIG_UPDATED, currentShopifyConfig ? currentShopifyConfig : {});


dispatch('job/clearJobState', null, { root: true });
},

/**
Expand Down
10 changes: 5 additions & 5 deletions src/views/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ export default defineComponent({
methods: {
setEComStore(store: any) {
if(this.userProfile) {
this.store.dispatch('user/setEcomStore', {
'eComStore': this.userProfile.stores.find((str: any) => str.productStoreId == store['detail'].value)
})
const productStoreId = store['detail'].value
this.store.dispatch('user/setEcomStore', { 'productStoreId': productStoreId })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

Suggested change
const productStoreId = store['detail'].value
this.store.dispatch('user/setEcomStore', { 'productStoreId': productStoreId })
this.store.dispatch('user/setEcomStore', { 'productStoreId': store['detail'].value })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, we could rename store to event like setShopifyConfig() method and access directly event.detail.value

}
},
setShopifyConfig(event: any){
const shopifyConfig = this.shopifyConfigs.find((shopifyConfig: any) => shopifyConfig.shopifyConfigId === event.detail.value)
this.store.dispatch('user/setCurrentShopifyConfig', shopifyConfig);
// const currentShopifyConfig = this.shopifyConfigs.find((shopifyConfig: any) => shopifyConfig.shopifyConfigId === event.detail.value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove unwanted code

const shopifyConfigId = event.detail.value;
this.store.dispatch('user/setCurrentShopifyConfig', { 'shopifyConfigId': shopifyConfigId });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

Suggested change
const shopifyConfigId = event.detail.value;
this.store.dispatch('user/setCurrentShopifyConfig', { 'shopifyConfigId': shopifyConfigId });
this.store.dispatch('user/setCurrentShopifyConfig', { 'shopifyConfigId': event.detail.value });

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented the changes, sir, please review.

},
async changeTimeZone() {
const timeZoneModal = await modalController.create({
Expand Down