Skip to content

Commit

Permalink
Merge pull request #4083 from Michal-Dziedzinski/fix/3838
Browse files Browse the repository at this point in the history
Fix sync cart between tabs
  • Loading branch information
andrzejewsky authored Feb 17, 2020
2 parents 6db6b02 + 762ce18 commit 8f19eb7
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix possibility to add same SKU with different custom options to the cart - @Michal-Dziedzinski (#3595)
- Fix `calculateProductTax` to find matching tax rules from ES for current product - @DylannCordel (#4056)
- Set `totals` in products in cart always in reactive way - @psmyrek (#4079)
- Fix sync cart between tabs - @Michal-Dziedzinski (#3838)
- Add currentRoute to url module and return cached requests - @gibkigonzo (pr#4077, issue#4066)

## [1.11.1] - 2020.02.05
Expand Down
4 changes: 3 additions & 1 deletion core/modules/cart/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import getProductOptions from './getProductOptions'
import getProductConfiguration from './getProductConfiguration'
import createOrderData from './createOrderData'
import createShippingInfoData from './createShippingInfoData'
import * as syncCartWhenLocalStorageChange from './syncCartWhenLocalStorageChange'

export {
cartCacheHandlerFactory,
Expand All @@ -33,5 +34,6 @@ export {
getProductOptions,
getProductConfiguration,
createOrderData,
createShippingInfoData
createShippingInfoData,
syncCartWhenLocalStorageChange
}
21 changes: 21 additions & 0 deletions core/modules/cart/helpers/syncCartWhenLocalStorageChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import rootStore from '@vue-storefront/core/store';

function getItemsFromStorage ({key}) {
if (key === 'shop/cart/current-cart') {
const storedItems = JSON.parse(localStorage[key])
rootStore.dispatch('cart/syncCartWhenLocalStorageChange', {items: storedItems})
}
}

function addEventListener () {
window.addEventListener('storage', getItemsFromStorage)
}

function removeEventListener () {
window.removeEventListener('storage', getItemsFromStorage)
}

export {
addEventListener,
removeEventListener
}
3 changes: 3 additions & 0 deletions core/modules/cart/store/actions/synchronizeActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const synchronizeActions = {

cartHooksExecutors.afterLoad(storedItems)
},
syncCartWhenLocalStorageChange ({commit}, {items}) {
commit(types.CART_LOAD_CART, items)
},
async synchronizeCart ({ commit, dispatch }, { forceClientState }) {
const { synchronize, serverMergeByDefault } = config.cart
if (!synchronize) return
Expand Down
1 change: 1 addition & 0 deletions core/modules/cart/test/unit/components/Product.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ jest.mock('@vue-storefront/i18n', () => ({ t: jest.fn(str => str) }));
jest.mock('@vue-storefront/core/app', () => jest.fn())
jest.mock('@vue-storefront/core/lib/multistore', () => jest.fn())
jest.mock('@vue-storefront/core/lib/storage-manager', () => jest.fn())
jest.mock('@vue-storefront/core/store', () => ({}))

describe('MicrocartProduct', () => {
beforeEach(() => {
Expand Down
1 change: 1 addition & 0 deletions core/modules/cart/test/unit/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ jest.mock('@vue-storefront/core/helpers', () => ({ isServer: false }))
jest.mock('@vue-storefront/core/lib/storage-manager', () => ({ initCacheStorage: jest.fn() }));
jest.mock('@vue-storefront/i18n', () => ({ t: jest.fn(str => str) }));
jest.mock('@vue-storefront/core/app', () => jest.fn())
jest.mock('@vue-storefront/core/store', () => ({}))
jest.mock('@vue-storefront/core/lib/multistore', () => ({
currentStoreView: jest.fn(),
localizedRoute: jest.fn()
Expand Down
1 change: 1 addition & 0 deletions core/modules/cart/test/unit/store/getters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jest.mock('@vue-storefront/i18n', () => ({ t: jest.fn(str => str) }));
jest.mock('@vue-storefront/core/lib/storage-manager', () => jest.fn())
jest.mock('@vue-storefront/core/app', () => jest.fn())
jest.mock('@vue-storefront/core/lib/multistore', () => jest.fn())
jest.mock('@vue-storefront/core/store', () => ({}))
jest.mock('@vue-storefront/core/helpers', () => ({
onlineHelper: {
get isOnline () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
<script>
import { mapGetters, mapActions } from 'vuex'
import MicrocartIcon from '@vue-storefront/core/compatibility/components/blocks/Header/MicrocartIcon'
import {syncCartWhenLocalStorageChange} from '@vue-storefront/core/modules/cart/helpers'
export default {
// mixins: [MicrocartIcon],
mounted () {
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
this.$store.dispatch('cart/load')
}
syncCartWhenLocalStorageChange.addEventListener()
this.$once('hook:beforeDestroy', () => {
syncCartWhenLocalStorageChange.removeEventListener()
})
},
computed: {
Expand Down

0 comments on commit 8f19eb7

Please sign in to comment.