diff --git a/CHANGELOG.md b/CHANGELOG.md index 78dd98dd9e..119db64dd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added clear filters button on desktop also and only show if filters are applied - @DaanKouters (#2342) - Improved docs at contributing.md and configuration.md (spelling etc.) - @ruthgeridema (#2421, #2422, #2423, #2425, #2426) - Fixed design issue of Country label on Edge 17 & Firefox - @ananth-iyer (#2390,#2399) +- Wishlist and compare items are loaded from local cache only once, instead of every time when module component is rendered - @patzick (#2431) - Country field is filled by first counry from the list in cart in paymen section - @RakowskiPrzemyslaw (#2428) - Added video support in Product Gallery component. - @rain2o (#2433) - Improved product quantity change component in product and cart - @patzick (#2398, #2437) diff --git a/core/modules/compare/store/actions.ts b/core/modules/compare/store/actions.ts index 97fe43c0bd..e3b89b53ee 100644 --- a/core/modules/compare/store/actions.ts +++ b/core/modules/compare/store/actions.ts @@ -9,8 +9,9 @@ import CompareState from '../types/CompareState' import { cacheStorage } from '../' import { Logger } from '@vue-storefront/core/lib/logger' const actions: ActionTree = { - load (context) { - const commit = context.commit + load ({ commit, getters }, force: boolean = false) { + if (!force && getters.isCompareLoaded) return + commit(types.SET_COMPARE_LOADED) cacheStorage.getItem('current-compare', (err, storedItems) => { if (err) throw new Error(err) commit(types.COMPARE_LOAD_COMPARE, storedItems) diff --git a/core/modules/compare/store/getters.ts b/core/modules/compare/store/getters.ts index 873c11444b..c5aeb19d8b 100644 --- a/core/modules/compare/store/getters.ts +++ b/core/modules/compare/store/getters.ts @@ -4,7 +4,8 @@ import CompareState from '../types/CompareState' const getters: GetterTree = { isEmpty: (state) => state.items.length === 0, - isOnCompare: (state) => (product) => state.items.find(p => p.sku === product.sku) + isOnCompare: (state) => (product) => state.items.find(p => p.sku === product.sku), + isCompareLoaded: state => state.loaded } export default getters diff --git a/core/modules/compare/store/index.ts b/core/modules/compare/store/index.ts index f602fa224b..03e5c1536d 100644 --- a/core/modules/compare/store/index.ts +++ b/core/modules/compare/store/index.ts @@ -8,6 +8,7 @@ import CompareState from '../types/CompareState' export const module: Module = { namespaced: true, state: { + loaded: false, items: [] }, getters, diff --git a/core/modules/compare/store/mutation-types.ts b/core/modules/compare/store/mutation-types.ts index d54b4e40ca..e149cfd1f3 100644 --- a/core/modules/compare/store/mutation-types.ts +++ b/core/modules/compare/store/mutation-types.ts @@ -2,3 +2,4 @@ export const SN_COMPARE = 'compare' export const COMPARE_ADD_ITEM = SN_COMPARE + '/ADD' export const COMPARE_DEL_ITEM = SN_COMPARE + '/DEL' export const COMPARE_LOAD_COMPARE = SN_COMPARE + '/LOAD' +export const SET_COMPARE_LOADED = `${SN_COMPARE}/SET_COMPARE_LOADED` diff --git a/core/modules/compare/store/mutations.ts b/core/modules/compare/store/mutations.ts index 7c5c65bfa0..771feb2c3e 100644 --- a/core/modules/compare/store/mutations.ts +++ b/core/modules/compare/store/mutations.ts @@ -20,6 +20,9 @@ const mutations: MutationTree = { }, [types.COMPARE_LOAD_COMPARE] (state, storedItems) { state.items = storedItems || [] + }, + [types.SET_COMPARE_LOADED] (state, isLoaded: boolean = true) { + state.loaded = isLoaded } } diff --git a/core/modules/compare/types/CompareState.ts b/core/modules/compare/types/CompareState.ts index 03d133c98c..8f013bc0f7 100644 --- a/core/modules/compare/types/CompareState.ts +++ b/core/modules/compare/types/CompareState.ts @@ -1,3 +1,7 @@ export default interface CompareState { + /** + * Informs if items to compare are already loaded from local cache. + */ + loaded: boolean, items: any[] } diff --git a/core/modules/wishlist/store/actions.ts b/core/modules/wishlist/store/actions.ts index 0f4f3f07c1..e072d5e5ed 100644 --- a/core/modules/wishlist/store/actions.ts +++ b/core/modules/wishlist/store/actions.ts @@ -13,8 +13,9 @@ const actions: ActionTree = { clear (context) { context.commit(types.WISH_LOAD_WISH, []) }, - load (context) { - const commit = context.commit + load ({ commit, getters }, force: boolean = false) { + if (!force && getters.isWishlistLoaded) return + commit(types.SET_WISHLIST_LOADED) cacheStorage.getItem('current-wishlist', (err, storedItems) => { if (err) throw new Error(err) commit(types.WISH_LOAD_WISH, storedItems) diff --git a/core/modules/wishlist/store/index.ts b/core/modules/wishlist/store/index.ts index b3bd28993d..c770df0e70 100644 --- a/core/modules/wishlist/store/index.ts +++ b/core/modules/wishlist/store/index.ts @@ -7,9 +7,13 @@ import WishlistState from '../types/WishlistState' export const module:Module = { namespaced: true, state: { + loaded: false, items: [] }, actions, - mutations + mutations, + getters: { + isWishlistLoaded: state => state.loaded + } } diff --git a/core/modules/wishlist/store/mutation-types.ts b/core/modules/wishlist/store/mutation-types.ts index 95bd3aaa68..9278c74337 100644 --- a/core/modules/wishlist/store/mutation-types.ts +++ b/core/modules/wishlist/store/mutation-types.ts @@ -1,4 +1,5 @@ export const SN_WISHLIST = 'wishlist' export const WISH_ADD_ITEM = SN_WISHLIST + '/ADD' export const WISH_DEL_ITEM = SN_WISHLIST + '/DEL' -export const WISH_LOAD_WISH = SN_WISHLIST + '/LOAD' \ No newline at end of file +export const WISH_LOAD_WISH = SN_WISHLIST + '/LOAD' +export const SET_WISHLIST_LOADED = `${SN_WISHLIST}/SET_WISHLIST_LOADED` \ No newline at end of file diff --git a/core/modules/wishlist/store/mutations.ts b/core/modules/wishlist/store/mutations.ts index 80ddc95cbe..a4ede21607 100644 --- a/core/modules/wishlist/store/mutations.ts +++ b/core/modules/wishlist/store/mutations.ts @@ -21,6 +21,9 @@ const mutations: MutationTree = { }, [types.WISH_LOAD_WISH] (state, storedItems) { state.items = storedItems || [] + }, + [types.SET_WISHLIST_LOADED] (state, isLoaded: boolean = true) { + state.loaded = isLoaded } } diff --git a/core/modules/wishlist/types/WishlistState.ts b/core/modules/wishlist/types/WishlistState.ts index 3cfe8294ca..38e55fe9c3 100644 --- a/core/modules/wishlist/types/WishlistState.ts +++ b/core/modules/wishlist/types/WishlistState.ts @@ -1,3 +1,7 @@ export default interface WishlistState { + /** + * Informs if wishlist is already loaded from local cache. + */ + loaded: boolean, items: any[] }