Skip to content
This repository was archived by the owner on Jan 26, 2024. It is now read-only.

Commit cc8d33a

Browse files
author
Tomasz Kostuch
authored
Merge branch 'hotfix/v1.10.6' into bugfix/update-current-token-with-refresh-token
2 parents 1683e6f + ed5d8a8 commit cc8d33a

File tree

7 files changed

+63
-10
lines changed

7 files changed

+63
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Fix hiding overlay for newsletter modal - @gibkigonzo (#3970)
1717
- Fix problem with storeView as dependency in filters - @gibkigonzo (#3968)
1818
- Fix v-model not working in BaseRadioButton - @lukeromanowicz (#4035)
19+
- add disconnect and sync options for clear/cart - @gibkigonzo (#4062)
1920
- Fix current token invalidation with refresh token - @gibkigonzo (#3928, #3620, #3626)
2021

2122
## [1.10.5] - 28.11.2019

core/modules/cart/store/actions.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,21 @@ const actions: ActionTree<CartState, RootState> = {
135135
async disconnect ({ commit }) {
136136
commit(types.CART_LOAD_CART_SERVER_TOKEN, null)
137137
},
138-
/** Clear the cart content */
139-
async clear ({ commit, dispatch, getters }) {
138+
/**
139+
* It will always clear cart items on frontend.
140+
* Options:
141+
* sync - if you want to sync it with backend.
142+
* disconnect - if you want to clear cart token.
143+
*/
144+
async clear ({ commit, dispatch }, { disconnect = true, sync = true } = {}) {
140145
await commit(types.CART_LOAD_CART, [])
141-
await commit(types.CART_LOAD_CART_SERVER_TOKEN, null)
142-
await commit(types.CART_SET_ITEMS_HASH, null)
146+
if (sync) {
147+
await dispatch('sync', { forceClientState: true })
148+
}
149+
if (disconnect) {
150+
await commit(types.CART_SET_ITEMS_HASH, null)
151+
await dispatch('disconnect')
152+
}
143153
},
144154
/** Refresh the payment methods with the backend */
145155
async syncPaymentMethods ({ getters, rootGetters, dispatch }, { forceServerSync = false }) {

core/modules/cart/test/unit/store/actions.spec.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ describe('Cart actions', () => {
7474
it('clear deletes all cart products and token', async () => {
7575
const contextMock = {
7676
commit: jest.fn(),
77+
dispatch: jest.fn(),
7778
getters: { isCartSyncEnabled: false }
7879
};
7980
const wrapper = (actions: any) => actions.clear(contextMock);
@@ -82,7 +83,43 @@ describe('Cart actions', () => {
8283

8384
await wrapper(cartActions);
8485

85-
expect(contextMock.commit).toBeCalledWith(types.CART_LOAD_CART, []);
86+
expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
87+
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'sync', { forceClientState: true });
88+
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_SET_ITEMS_HASH, null);
89+
expect(contextMock.dispatch).toHaveBeenNthCalledWith(2, 'disconnect');
90+
});
91+
92+
it('clear deletes all cart products but keep token', async () => {
93+
const contextMock = {
94+
commit: jest.fn(),
95+
dispatch: jest.fn(),
96+
getters: { isCartSyncEnabled: false }
97+
};
98+
const wrapper = (actions: any) => actions.clear(contextMock, { disconnect: false });
99+
100+
config.cart = { synchronize: false };
101+
102+
await wrapper(cartActions);
103+
104+
expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
105+
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'sync', { forceClientState: true });
106+
});
107+
108+
it('clear deletes all cart products and token, but not sync with backend', async () => {
109+
const contextMock = {
110+
commit: jest.fn(),
111+
dispatch: jest.fn(),
112+
getters: { isCartSyncEnabled: false }
113+
};
114+
const wrapper = (actions: any) => actions.clear(contextMock, { sync: false });
115+
116+
config.cart = { synchronize: false };
117+
118+
await wrapper(cartActions);
119+
120+
expect(contextMock.commit).toHaveBeenNthCalledWith(1, types.CART_LOAD_CART, []);
121+
expect(contextMock.commit).toHaveBeenNthCalledWith(2, types.CART_SET_ITEMS_HASH, null);
122+
expect(contextMock.dispatch).toHaveBeenNthCalledWith(1, 'disconnect');
86123
});
87124

88125
describe('create', () => {

core/modules/checkout/store/checkout/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const actions: ActionTree<CheckoutState, RootState> = {
1616
const result = await dispatch('order/placeOrder', order, {root: true})
1717
if (!result.resultCode || result.resultCode === 200) {
1818
Vue.prototype.$db.usersCollection.setItem('last-cart-bypass-ts', new Date().getTime())
19-
await dispatch('cart/clear', null, {root: true})
19+
// clear cart without sync, because after order cart will be already cleared on backend
20+
await dispatch('cart/clear', { sync: false }, {root: true})
2021
if (state.personalDetails.createAccount) {
2122
commit(types.CHECKOUT_DROP_PASSWORD)
2223
}

core/modules/user/store/actions.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ const actions: ActionTree<UserState, RootState> = {
297297
context.dispatch('cart/disconnect', {}, { root: true })
298298
.then(() => { context.dispatch('clearCurrentUser') })
299299
.then(() => { Vue.prototype.$bus.$emit('user-after-logout') })
300-
.then(() => { context.dispatch('cart/clear', null, { root: true }) })
300+
// clear cart without sync, because after logout we don't want to clear cart on backend
301+
// user should have items when he comes back
302+
.then(() => { context.dispatch('cart/clear', { sync: false }, { root: true }) })
301303
if (!silent) {
302304
rootStore.dispatch('notification/spawnNotification', {
303305
type: 'success',

src/modules/instant-checkout/components/InstantCheckout.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,8 @@ export default {
143143
this.$store.dispatch('checkout/setThankYouPage', true)
144144
this.$store.commit('ui/setMicrocart', false)
145145
this.$router.push(this.localizedRoute('/checkout'))
146-
this.$store.dispatch('cart/clear', null, {root: true})
146+
// clear cart without sync, because after order cart will be already cleared on backend
147+
this.$store.dispatch('cart/clear', { sync: false }, {root: true})
147148
}
148149
})
149150
})

src/themes/default/components/core/blocks/Microcart/Microcart.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,8 +210,9 @@ export default {
210210
action1: { label: i18n.t('Cancel'), action: 'close' },
211211
action2: { label: i18n.t('OK'),
212212
action: async () => {
213-
await this.$store.dispatch('cart/clear') // just clear the items without sync
214-
await this.$store.dispatch('cart/sync', { forceClientState: true })
213+
// We just need to clear cart on frontend and backend.
214+
// but cart token can be reused
215+
await this.$store.dispatch('cart/clear', { disconnect: false })
215216
}
216217
},
217218
hasNoTimeout: true

0 commit comments

Comments
 (0)