From d4cdebcba223e74ef5b06ffd366935e71fc84d82 Mon Sep 17 00:00:00 2001 From: Mayank Verma Date: Tue, 24 May 2022 14:56:16 +0530 Subject: [PATCH] Improved: code of product store module(#1uvvc2n) --- src/store/modules/product/ProductState.ts | 7 ++++--- src/store/modules/product/actions.ts | 4 ++-- src/store/modules/product/getters.ts | 6 +++--- src/store/modules/product/index.ts | 7 ++++--- src/store/modules/product/mutations.ts | 6 +++--- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/store/modules/product/ProductState.ts b/src/store/modules/product/ProductState.ts index 4d601566d..f4ede2736 100644 --- a/src/store/modules/product/ProductState.ts +++ b/src/store/modules/product/ProductState.ts @@ -1,5 +1,6 @@ export default interface ProductState { - cached: any, - products: any, - total: number + products: { + cached: any, + total: number + } } \ No newline at end of file diff --git a/src/store/modules/product/actions.ts b/src/store/modules/product/actions.ts index 049b46053..40e7b3a5c 100644 --- a/src/store/modules/product/actions.ts +++ b/src/store/modules/product/actions.ts @@ -10,7 +10,7 @@ import emitter from '@/event-bus' const actions: ActionTree = { 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)) { @@ -55,7 +55,7 @@ const actions: ActionTree = { 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 diff --git a/src/store/modules/product/getters.ts b/src/store/modules/product/getters.ts index 6c20ca10a..a28d42bc9 100644 --- a/src/store/modules/product/getters.ts +++ b/src/store/modules/product/getters.ts @@ -5,15 +5,15 @@ import RootState from "../../RootState"; const getters: GetterTree = { 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 ); }, }; diff --git a/src/store/modules/product/index.ts b/src/store/modules/product/index.ts index 2a2430dbb..56b4df3cf 100644 --- a/src/store/modules/product/index.ts +++ b/src/store/modules/product/index.ts @@ -8,9 +8,10 @@ import RootState from '../../RootState' const productModule: Module = { namespaced: true, state: { - cached: {}, - products:{}, - total: 0 + products: { + cached: {}, + total: 0 + }, }, getters, actions, diff --git a/src/store/modules/product/mutations.ts b/src/store/modules/product/mutations.ts index 169cbfbbb..5a03cb769 100644 --- a/src/store/modules/product/mutations.ts +++ b/src/store/modules/product/mutations.ts @@ -6,14 +6,14 @@ const mutations: MutationTree = { [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; \ No newline at end of file