forked from hotwax/order-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented: vuex store to maintain product information(hotwax#254)
- Loading branch information
1 parent
f819a5b
commit c2a14ae
Showing
7 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export default interface ProductState { | ||
products: any; | ||
stock: any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters