Skip to content

Commit

Permalink
Implemented: vuex store to maintain product information(hotwax#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymaheshwari1 committed Dec 16, 2024
1 parent f819a5b commit c2a14ae
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/store/modules/product/ProductState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default interface ProductState {
products: any;
stock: any;
}
78 changes: 78 additions & 0 deletions src/store/modules/product/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { ActionTree } from "vuex"
import RootState from "@/store/RootState"
import ProductState from "./ProductState"
import logger from "@/logger"
import { hasError } from "@/utils"
import * as types from "./mutation-types"
import { ProductService } from "@/services/ProductService"

const actions: ActionTree<ProductState, RootState> = {
async fetchProducts({ commit, state }, productIds) {
const cachedProductIds = Object.keys(state.products);
const productIdFilter= productIds.reduce((filter: string, productId: any) => {
// If product already exist in cached products skip
if (cachedProductIds.includes(productId)) {
return filter;
} else {
// checking condition that if the filter is not empty then adding 'OR' to the filter
if (filter !== '') filter += ' OR '
return filter += productId;
}
}, '');

// If there are no products skip the API call
if (productIdFilter === '') return;

try {
// adding viewSize as by default we are having the viewSize of 10
const resp = await ProductService.fetchProducts({
"filters": ['productId: (' + productIdFilter + ')'],
"viewSize": productIds.length
})
if(resp.data.response && !hasError(resp)) {
const products = resp.data.response.docs.reduce((products: any, product: any) => {
products[product.productId] = product
return products;
}, {});
// Handled empty response in case of failed query
if(resp.data) commit(types.PRODUCT_LIST_UPDATED, products);
} else {
throw resp.data;
}
} catch(err) {
logger.error("Failed to fetch product information", err)
}
},

async fetchStock({ commit, state }, shipGroup) {
const productIds = shipGroup.map((item: any) => item.productId)
const facilityId = shipGroup[0].facilityId
for(const productId of productIds) {
if(state.stock[productId]?.[facilityId]) {
continue;
}

try {
const payload = {
productId,
facilityId
}

const resp: any = await ProductService.getInventoryAvailableByFacility(payload);
if (!hasError(resp)) {
commit(types.PRODUCT_STOCK_UPDATED, { productId: payload.productId, facilityId: facilityId, stock: resp.data })
} else {
throw resp.data;
}
} catch (err) {
logger.error(err)
}
}
},

async clearProductState({ commit }) {
commit(types.PRODUCT_CLEARED)
}
}

export default actions;
18 changes: 18 additions & 0 deletions src/store/modules/product/getters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { GetterTree } from "vuex"
import ProductState from "./ProductState"
import RootState from "@/store/RootState"

const getters: GetterTree<ProductState, RootState> = {
getProducts(state) {
return state.products
},
getProductById: (state) => (id: string) => {
console.log('id', id)
return state.products[id] || {}
},
getProductStock: (state) => (productId: string, facilityId: string) => {
return state.stock[productId] ? state.stock[productId][facilityId] ? state.stock[productId][facilityId] : {} : {}
},
}

export default getters;
19 changes: 19 additions & 0 deletions src/store/modules/product/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import actions from "./actions"
import getters from "./getters"
import mutations from "./mutations"
import { Module } from "vuex"
import ProductState from "./ProductState"
import RootState from "@/store/RootState"

const productModule: Module<ProductState, RootState> = {
namespaced: true,
state: {
products: {},
stock: {}
},
getters,
actions,
mutations,
}

export default productModule;
5 changes: 5 additions & 0 deletions src/store/modules/product/mutation-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const SN_PRODUCT = "product"
export const PRODUCT_LIST_UPDATED = SN_PRODUCT + "/LIST_UPDATED"
export const PRODUCT_STOCK_UPDATED = SN_PRODUCT + "/STOCK_UPDATED"
export const PRODUCT_CLEARED = SN_PRODUCT + "/CLEARED"

22 changes: 22 additions & 0 deletions src/store/modules/product/mutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { MutationTree } from "vuex"
import ProductState from "./ProductState"
import * as types from "./mutation-types"

const mutations: MutationTree<ProductState> = {
[types.PRODUCT_LIST_UPDATED](state, payload) {
state.products = payload
},
[types.PRODUCT_CLEARED](state) {
state.products = {}
},
[types.PRODUCT_STOCK_UPDATED] (state, payload) {
if(state.stock[payload.productId]) {
state.stock[payload.productId][payload.facilityId] = payload.stock
} else {
state.stock[payload.productId] = {
[payload.facilityId]: payload.stock
}
}
},
}
export default mutations;
5 changes: 3 additions & 2 deletions src/store/modules/user/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ const actions: ActionTree<UserState, RootState> = {
Settings.defaultZone = userProfile.timeZone;
}

setPermissions(appPermissions);
// setPermissions(appPermissions);
if(omsRedirectionUrl && token) {
dispatch("setOmsRedirectionInfo", { url: omsRedirectionUrl, token })
}
commit(types.USER_TOKEN_CHANGED, { newToken: api_key })
commit(types.USER_INFO_UPDATED, userProfile);
commit(types.USER_PERMISSIONS_UPDATED, appPermissions);
// commit(types.USER_PERMISSIONS_UPDATED, appPermissions);
commit(types.USER_CURRENT_ECOM_STORE_UPDATED, userProfile.stores.length ? userProfile.stores[0] : {});
emitter.emit("dismissLoader")
} catch (err: any) {
Expand All @@ -90,6 +90,7 @@ const actions: ActionTree<UserState, RootState> = {
commit(types.USER_END_SESSION)
this.dispatch("orderRouting/clearRouting")
this.dispatch("util/clearUtilState")
this.dispatch("product/clearProductState")
dispatch("setOmsRedirectionInfo", { url: "", token: "" })
resetConfig();
resetPermissions();
Expand Down

0 comments on commit c2a14ae

Please sign in to comment.