Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix hiding overlay for newsletter modal - @gibkigonzo (#3970)
- Fix problem with storeView as dependency in filters - @gibkigonzo (#3968)
- Fix v-model not working in BaseRadioButton - @lukeromanowicz (#4035)
- add disconnect and sync options for clear/cart - @gibkigonzo (#4062)

## [1.10.5] - 28.11.2019

Expand Down
18 changes: 14 additions & 4 deletions core/modules/cart/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,21 @@ const actions: ActionTree<CartState, RootState> = {
async disconnect ({ commit }) {
commit(types.CART_LOAD_CART_SERVER_TOKEN, null)
},
/** Clear the cart content */
async clear ({ commit, dispatch, getters }) {
/**
* It will always clear cart items on frontend.
* Options:
* sync - if you want to sync it with backend.
* disconnect - if you want to clear cart token.
*/
async clear ({ commit, dispatch }, { disconnect = true, sync = true } = {}) {
await commit(types.CART_LOAD_CART, [])
await commit(types.CART_LOAD_CART_SERVER_TOKEN, null)
await commit(types.CART_SET_ITEMS_HASH, null)
if (sync) {
await dispatch('sync', { forceClientState: true })
}
if (disconnect) {
await commit(types.CART_SET_ITEMS_HASH, null)
await dispatch('disconnect')
}
},
/** Refresh the payment methods with the backend */
async syncPaymentMethods ({ getters, rootGetters, dispatch }, { forceServerSync = false }) {
Expand Down
39 changes: 38 additions & 1 deletion core/modules/cart/test/unit/store/actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('Cart actions', () => {
it('clear deletes all cart products and token', async () => {
const contextMock = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: { isCartSyncEnabled: false }
};
const wrapper = (actions: any) => actions.clear(contextMock);
Expand All @@ -82,7 +83,43 @@ describe('Cart actions', () => {

await wrapper(cartActions);

expect(contextMock.commit).toBeCalledWith(types.CART_LOAD_CART, []);
expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'sync', { forceClientState: true });
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_SET_ITEMS_HASH, null);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(2, 'disconnect');
});

it('clear deletes all cart products but keep token', async () => {
const contextMock = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: { isCartSyncEnabled: false }
};
const wrapper = (actions: any) => actions.clear(contextMock, { disconnect: false });

config.cart = { synchronize: false };

await wrapper(cartActions);

expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'sync', { forceClientState: true });
});

it('clear deletes all cart products and token, but not sync with backend', async () => {
const contextMock = {
commit: jest.fn(),
dispatch: jest.fn(),
getters: { isCartSyncEnabled: false }
};
const wrapper = (actions: any) => actions.clear(contextMock, { sync: false });

config.cart = { synchronize: false };

await wrapper(cartActions);

expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_SET_ITEMS_HASH, null);
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'disconnect');
});

describe('create', () => {
Expand Down
3 changes: 2 additions & 1 deletion core/modules/checkout/store/checkout/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ const actions: ActionTree<CheckoutState, RootState> = {
const result = await dispatch('order/placeOrder', order, {root: true})
if (!result.resultCode || result.resultCode === 200) {
Vue.prototype.$db.usersCollection.setItem('last-cart-bypass-ts', new Date().getTime())
await dispatch('cart/clear', null, {root: true})
// clear cart without sync, because after order cart will be already cleared on backend
await dispatch('cart/clear', { sync: false }, {root: true})
if (state.personalDetails.createAccount) {
commit(types.CHECKOUT_DROP_PASSWORD)
}
Expand Down
4 changes: 3 additions & 1 deletion core/modules/user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ const actions: ActionTree<UserState, RootState> = {
context.dispatch('cart/disconnect', {}, { root: true })
.then(() => { context.dispatch('clearCurrentUser') })
.then(() => { Vue.prototype.$bus.$emit('user-after-logout') })
.then(() => { context.dispatch('cart/clear', null, { root: true }) })
// clear cart without sync, because after logout we don't want to clear cart on backend
// user should have items when he comes back
.then(() => { context.dispatch('cart/clear', { sync: false }, { root: true }) })
if (!silent) {
rootStore.dispatch('notification/spawnNotification', {
type: 'success',
Expand Down
3 changes: 2 additions & 1 deletion src/modules/instant-checkout/components/InstantCheckout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ export default {
this.$store.dispatch('checkout/setThankYouPage', true)
this.$store.commit('ui/setMicrocart', false)
this.$router.push(this.localizedRoute('/checkout'))
this.$store.dispatch('cart/clear', null, {root: true})
// clear cart without sync, because after order cart will be already cleared on backend
this.$store.dispatch('cart/clear', { sync: false }, {root: true})
}
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,9 @@ export default {
action1: { label: i18n.t('Cancel'), action: 'close' },
action2: { label: i18n.t('OK'),
action: async () => {
await this.$store.dispatch('cart/clear') // just clear the items without sync
await this.$store.dispatch('cart/sync', { forceClientState: true })
// We just need to clear cart on frontend and backend.
// but cart token can be reused
await this.$store.dispatch('cart/clear', { disconnect: false })
}
},
hasNoTimeout: true
Expand Down