Skip to content

Commit

Permalink
Improved: code of product store module(#1uvvc2n)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank909 committed May 24, 2022
1 parent 0292504 commit d4cdebc
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
7 changes: 4 additions & 3 deletions src/store/modules/product/ProductState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export default interface ProductState {
cached: any,
products: any,
total: number
products: {
cached: any,
total: number
}
}
4 changes: 2 additions & 2 deletions src/store/modules/product/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import emitter from '@/event-bus'

const actions: ActionTree<ProductState, RootState> = {
async fetchProducts ( { commit, state }, { productIds }) {
const cachedProductIds = Object.keys(state.cached);
const cachedProductIds = Object.keys(state.products.cached);
const productIdFilter= productIds.reduce((filter: string, productId: any) => {
// If product already exist in cached products skip
if (cachedProductIds.includes(productId)) {
Expand Down Expand Up @@ -55,7 +55,7 @@ const actions: ActionTree<ProductState, RootState> = {
let products = resp.data.response.docs;
const totalProductsCount = resp.data.response.numFound;

if (payload.viewIndex && payload.viewIndex > 0) products = state.products.concat(products)
if (payload.viewIndex && payload.viewIndex > 0) products = state.products.cached.concat(products)
commit(types.PRODUCT_SEARCH_UPDATED, { products: products, totalProductsCount: totalProductsCount })
} else {
//showing error whenever getting no products in the response or having any other error
Expand Down
6 changes: 3 additions & 3 deletions src/store/modules/product/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import RootState from "../../RootState";
const getters: GetterTree<ProductState, RootState> = {
getProduct: (state) => (productId: string) => {
// Returning empty object so that it doesn't breaks the UI
return state.products[productId] ? state.products[productId] : {};
return state.products.cached[productId] ? state.products.cached[productId] : {};
},
findProduct: (state) => {
return state.products ? state.products : {};
},
isScrollable(state) {
return (
state.products.length > 0 &&
state.products.length < state.total
state.products.cached.length > 0 &&
state.products.cached.length < state.products.total
);
},
};
Expand Down
7 changes: 4 additions & 3 deletions src/store/modules/product/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import RootState from '../../RootState'
const productModule: Module<ProductState, RootState> = {
namespaced: true,
state: {
cached: {},
products:{},
total: 0
products: {
cached: {},
total: 0
},
},
getters,
actions,
Expand Down
6 changes: 3 additions & 3 deletions src/store/modules/product/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ const mutations: MutationTree <ProductState> = {
[types.PRODUCT_ADD_TO_CACHED_MULTIPLE] (state, payload) {
if (payload.products) {
payload.products.forEach((product: any) => {
state.cached[product.productId] = product
state.products.cached[product.productId] = product
});
state.total = payload.totalProductsCount
state.products.total = payload.totalProductsCount
}
},
[types.PRODUCT_SEARCH_UPDATED] (state, payload) {
state.products = payload.products;
state.total = payload.totalProductsCount;
state.products.total = payload.totalProductsCount;
}
}
export default mutations;

0 comments on commit d4cdebc

Please sign in to comment.