Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Totals localstorage sync #5352

Merged
merged 7 commits into from
Dec 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added states.json in core/i18n/resource (#4531)
- Added phone validation helper (#4980)
- Configurable enabling min & max price aggregations
- Storing totals in localStorage to sync it between tabs ([#4733](https://github.com/vuestorefront/vue-storefront/issues/4733))

### Fixed

Expand Down
2 changes: 2 additions & 0 deletions core/modules/cart/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cartCacheHandlerFactory } from './cartCacheHandler'
import { totalsCacheHandlerFactory } from './totalsCacheHandler'
import optimizeProduct from './optimizeProduct'
import prepareProductsToAdd from './prepareProductsToAdd'
import productChecksum from './productChecksum'
Expand All @@ -19,6 +20,7 @@ import * as syncCartWhenLocalStorageChange from './syncCartWhenLocalStorageChang

export {
cartCacheHandlerFactory,
totalsCacheHandlerFactory,
optimizeProduct,
prepareProductsToAdd,
productChecksum,
Expand Down
6 changes: 4 additions & 2 deletions core/modules/cart/helpers/syncCartWhenLocalStorageChange.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import rootStore from '@vue-storefront/core/store';

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

Expand Down
19 changes: 19 additions & 0 deletions core/modules/cart/helpers/totalsCacheHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as types from '../store/mutation-types'
import { Logger } from '@vue-storefront/core/lib/logger'

import { StorageManager } from '@vue-storefront/core/lib/storage-manager'

export const totalsCacheHandlerFactory = (mutation, state) => {
Fifciu marked this conversation as resolved.
Show resolved Hide resolved
const type = mutation.type;

if (
type.endsWith(types.CART_UPD_TOTALS)
) {
return StorageManager.get('cart').setItem('current-totals', {
platformTotalSegments: state.cart.platformTotalSegments,
platformTotals: state.cart.platformTotals
}).catch((reason) => {
Logger.error(reason)()
})
}
}
3 changes: 2 additions & 1 deletion core/modules/cart/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StorefrontModule } from '@vue-storefront/core/lib/modules'
import { cartStore } from './store'
import { cartCacheHandlerFactory } from './helpers';
import { cartCacheHandlerFactory, totalsCacheHandlerFactory } from './helpers';
import { isServer } from '@vue-storefront/core/helpers'
import Vue from 'vue'
import { StorageManager } from '@vue-storefront/core/lib/storage-manager'
Expand All @@ -12,4 +12,5 @@ export const CartModule: StorefrontModule = function ({ store }) {

if (!isServer) store.dispatch('cart/load')
store.subscribe(cartCacheHandlerFactory(Vue))
store.subscribe(totalsCacheHandlerFactory);
}
3 changes: 3 additions & 0 deletions core/modules/cart/store/actions/totalsActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
import EventBus from '@vue-storefront/core/compatibility/plugins/event-bus'

const totalsActions = {
async syncTotalsWhenLocalStorageChange ({ commit }, payload) {
Fifciu marked this conversation as resolved.
Show resolved Hide resolved
commit(types.CART_UPD_TOTALS, payload)
},
async getTotals (context, { addressInformation, hasShippingInformation }) {
if (hasShippingInformation) {
return CartService.setShippingInfo(addressInformation)
Expand Down