@riosst100/pwa-marketplace 2.4.7 → 2.4.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/componentOverrideMapping.js +3 -0
- package/src/components/Seller/seller.js +8 -0
- package/src/components/SellerDetail/sellerDetail.js +4 -3
- package/src/components/SellerMegaMenu/index.js +1 -0
- package/src/components/SellerMegaMenu/sellerMegaMenu.js +92 -0
- package/src/components/SellerMegaMenu/sellerMegaMenu.module.css +12 -0
- package/src/components/SellerMegaMenu/sellerMegaMenuItem.js +164 -0
- package/src/components/SellerMegaMenu/sellerMegaMenuItem.module.css +31 -0
- package/src/components/SellerMegaMenu/sellerSubmenu.js +106 -0
- package/src/components/SellerMegaMenu/sellerSubmenu.module.css +57 -0
- package/src/components/SellerMegaMenu/sellerSubmenuColumn.js +173 -0
- package/src/components/SellerMegaMenu/sellerSubmenuColumn.module.css +33 -0
- package/src/components/SellerProducts/productContent.js +70 -11
- package/src/components/SellerProducts/sellerProducts.js +51 -12
- package/src/intercept.js +14 -0
- package/src/overwrites/peregrine/lib/context/cart.js +136 -0
- package/src/overwrites/peregrine/lib/store/actions/cart/asyncActions.js +589 -0
- package/src/overwrites/peregrine/lib/talons/CartPage/PriceSummary/usePriceSummary.js +4 -1
- package/src/overwrites/peregrine/lib/talons/CheckoutPage/useCheckoutPage.js +434 -0
- package/src/overwrites/venia-ui/lib/components/Adapter/adapter.js +0 -9
- package/src/overwrites/venia-ui/lib/components/CartPage/ProductListing/product.js +2 -2
- package/src/overwrites/venia-ui/lib/components/CartPage/ProductListing/productListing.js +8 -8
- package/src/overwrites/venia-ui/lib/components/Header/header.js +1 -1
- package/src/talons/Seller/useSeller.js +1 -1
- package/src/talons/SellerMegaMenu/megaMenu.gql.js +96 -0
- package/src/talons/SellerMegaMenu/useSellerMegaMenu.js +199 -0
- package/src/talons/SellerMegaMenu/useSellerMegaMenuItem.js +66 -0
- package/src/talons/SellerMegaMenu/useSellerSubMenu.js +21 -0
- package/src/talons/SellerProducts/sellerProducts.gql.js +24 -1
- package/src/talons/SellerProducts/useProductContent.js +17 -23
- package/src/talons/SellerProducts/useSellerProducts.js +44 -28
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
import BrowserPersistence from '@magento/peregrine/lib/util/simplePersistence';
|
|
2
|
+
import { signOut } from '@magento/peregrine/lib/store/actions/user';
|
|
3
|
+
import actions from '@magento/peregrine/lib/store/actions/cart/actions';
|
|
4
|
+
|
|
5
|
+
const storage = new BrowserPersistence();
|
|
6
|
+
|
|
7
|
+
export const createSellerCart = payload =>
|
|
8
|
+
async function thunk(dispatch, getState) {
|
|
9
|
+
const { initCheckoutSplitCart, sellerUrl } = payload;
|
|
10
|
+
const { cart } = getState();
|
|
11
|
+
|
|
12
|
+
// Request a new cart.
|
|
13
|
+
dispatch(actions.getSellerCart.request());
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
// errors can come from graphql and are not thrown
|
|
17
|
+
const { data, errors } = await initCheckoutSplitCart({
|
|
18
|
+
variables: {
|
|
19
|
+
input: {
|
|
20
|
+
seller_url: sellerUrl,
|
|
21
|
+
cart_id: cart.cartId
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
console.log('datadatadatadata')
|
|
27
|
+
console.log(data)
|
|
28
|
+
|
|
29
|
+
console.log('data.initCheckoutSplitCart')
|
|
30
|
+
console.log(data.initCheckoutSplitCart)
|
|
31
|
+
|
|
32
|
+
console.log('data.initCheckoutSplitCart.quote_id')
|
|
33
|
+
console.log(data.initCheckoutSplitCart.quote_id)
|
|
34
|
+
|
|
35
|
+
let receivePayload;
|
|
36
|
+
|
|
37
|
+
if (errors) {
|
|
38
|
+
receivePayload = new Error(errors);
|
|
39
|
+
console.log(errors)
|
|
40
|
+
} else {
|
|
41
|
+
receivePayload = data.initCheckoutSplitCart.quote_id;
|
|
42
|
+
// write to storage in the background
|
|
43
|
+
saveSellerCartId(data.initCheckoutSplitCart.quote_id);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
dispatch(actions.getSellerCart.receive(receivePayload));
|
|
47
|
+
} catch (error) {
|
|
48
|
+
// If we are unable to create a cart, the cart can't function, so
|
|
49
|
+
// we forcibly throw so the upstream actions won't retry.
|
|
50
|
+
dispatch(actions.getSellerCart.receive(error));
|
|
51
|
+
throw new Error('Unable to create seller cart');
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const resetSplitCart = payload =>
|
|
56
|
+
async function thunk(dispatch, getState) {
|
|
57
|
+
const { removeSplitCart } = payload;
|
|
58
|
+
const { cart } = getState();
|
|
59
|
+
|
|
60
|
+
// if a cart already exists in the store, exit
|
|
61
|
+
if (!cart.cartId) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
try {
|
|
66
|
+
// errors can come from graphql and are not thrown
|
|
67
|
+
await removeSplitCart({ variables: { input: { cart_id: cart.cartId }} });
|
|
68
|
+
|
|
69
|
+
await clearSellerCartId();
|
|
70
|
+
} catch (error) {}
|
|
71
|
+
};
|
|
72
|
+
export const createCart = payload =>
|
|
73
|
+
async function thunk(dispatch, getState) {
|
|
74
|
+
const { fetchCartId } = payload;
|
|
75
|
+
const { cart } = getState();
|
|
76
|
+
|
|
77
|
+
// if a cart already exists in the store, exit
|
|
78
|
+
if (cart.cartId) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Request a new cart.
|
|
83
|
+
dispatch(actions.getCart.request());
|
|
84
|
+
|
|
85
|
+
// if a cart exists in storage, act like we just received it
|
|
86
|
+
const cartId = await retrieveCartId();
|
|
87
|
+
if (cartId) {
|
|
88
|
+
dispatch(actions.getCart.receive(cartId));
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
// errors can come from graphql and are not thrown
|
|
94
|
+
const { data, errors } = await fetchCartId({
|
|
95
|
+
fetchPolicy: 'no-cache'
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
let receivePayload;
|
|
99
|
+
|
|
100
|
+
if (errors) {
|
|
101
|
+
receivePayload = new Error(errors);
|
|
102
|
+
} else {
|
|
103
|
+
receivePayload = data.cartId;
|
|
104
|
+
// write to storage in the background
|
|
105
|
+
saveCartId(data.cartId);
|
|
106
|
+
// saveSellerCartId(data.cartId);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
dispatch(actions.getCart.receive(receivePayload));
|
|
110
|
+
} catch (error) {
|
|
111
|
+
// If we are unable to create a cart, the cart can't function, so
|
|
112
|
+
// we forcibly throw so the upstream actions won't retry.
|
|
113
|
+
dispatch(actions.getCart.receive(error));
|
|
114
|
+
throw new Error('Unable to create cart');
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
export const addItemToCart = (payload = {}) => {
|
|
119
|
+
const {
|
|
120
|
+
addItemMutation,
|
|
121
|
+
fetchCartDetails,
|
|
122
|
+
fetchCartId,
|
|
123
|
+
item,
|
|
124
|
+
quantity,
|
|
125
|
+
parentSku
|
|
126
|
+
} = payload;
|
|
127
|
+
|
|
128
|
+
const writingImageToCache = writeImageToCache(item);
|
|
129
|
+
|
|
130
|
+
return async function thunk(dispatch, getState) {
|
|
131
|
+
await writingImageToCache;
|
|
132
|
+
dispatch(actions.addItem.request(payload));
|
|
133
|
+
|
|
134
|
+
const { cart, user } = getState();
|
|
135
|
+
const { cartId } = cart;
|
|
136
|
+
const { isSignedIn } = user;
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const variables = {
|
|
140
|
+
cartId,
|
|
141
|
+
parentSku,
|
|
142
|
+
product: item,
|
|
143
|
+
quantity,
|
|
144
|
+
sku: item.sku
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
await addItemMutation({
|
|
148
|
+
variables
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// 2019-02-07 Moved these dispatches to the success clause of
|
|
152
|
+
// addItemToCart. The cart should only open on success.
|
|
153
|
+
// In the catch clause, this action creator calls its own thunk,
|
|
154
|
+
// so a successful retry will wind up here anyway.
|
|
155
|
+
await dispatch(
|
|
156
|
+
getCartDetails({
|
|
157
|
+
fetchCartId,
|
|
158
|
+
fetchCartDetails
|
|
159
|
+
})
|
|
160
|
+
);
|
|
161
|
+
dispatch(actions.addItem.receive());
|
|
162
|
+
} catch (error) {
|
|
163
|
+
dispatch(actions.addItem.receive(error));
|
|
164
|
+
|
|
165
|
+
const shouldRetry = !error.networkError && isInvalidCart(error);
|
|
166
|
+
|
|
167
|
+
// Only retry if the cart is invalid or the cartId is missing.
|
|
168
|
+
if (shouldRetry) {
|
|
169
|
+
if (isSignedIn) {
|
|
170
|
+
// Since simple persistence just deletes auth token without
|
|
171
|
+
// informing Redux, we need to perform the sign out action
|
|
172
|
+
// to reset the user and cart slices back to initial state.
|
|
173
|
+
await dispatch(signOut());
|
|
174
|
+
} else {
|
|
175
|
+
// Delete the cached ID from local storage and Redux.
|
|
176
|
+
// In contrast to the save, make sure storage deletion is
|
|
177
|
+
// complete before dispatching the error--you don't want an
|
|
178
|
+
// upstream action to try and reuse the known-bad ID.
|
|
179
|
+
await dispatch(removeCart());
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// then create a new one
|
|
183
|
+
try {
|
|
184
|
+
await dispatch(
|
|
185
|
+
createCart({
|
|
186
|
+
fetchCartId
|
|
187
|
+
})
|
|
188
|
+
);
|
|
189
|
+
} catch (error) {
|
|
190
|
+
// If creating a cart fails, all is not lost. Return so that the
|
|
191
|
+
// user can continue to at least browse the site.
|
|
192
|
+
return;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// and fetch details
|
|
196
|
+
await dispatch(
|
|
197
|
+
getCartDetails({
|
|
198
|
+
fetchCartId,
|
|
199
|
+
fetchCartDetails
|
|
200
|
+
})
|
|
201
|
+
);
|
|
202
|
+
|
|
203
|
+
// then retry this operation
|
|
204
|
+
return thunk(...arguments);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Applies changes in options/quantity to a cart item.
|
|
212
|
+
*
|
|
213
|
+
* @param payload.cartItemId {Number} the id of the cart item we are updating
|
|
214
|
+
* @param payload.item {Object} the new configuration item if changes are selected.
|
|
215
|
+
* @param payload.quantity {Number} the quantity of the item being updated
|
|
216
|
+
* @param payload.productType {String} 'ConfigurableProduct' or other.
|
|
217
|
+
*/
|
|
218
|
+
export const updateItemInCart = (payload = {}) => {
|
|
219
|
+
const {
|
|
220
|
+
cartItemId,
|
|
221
|
+
fetchCartDetails,
|
|
222
|
+
fetchCartId,
|
|
223
|
+
item,
|
|
224
|
+
productType,
|
|
225
|
+
quantity,
|
|
226
|
+
removeItem,
|
|
227
|
+
updateItem
|
|
228
|
+
} = payload;
|
|
229
|
+
const writingImageToCache = writeImageToCache(item);
|
|
230
|
+
|
|
231
|
+
return async function thunk(dispatch, getState) {
|
|
232
|
+
await writingImageToCache;
|
|
233
|
+
dispatch(actions.updateItem.request(payload));
|
|
234
|
+
|
|
235
|
+
const { cart, user } = getState();
|
|
236
|
+
const { cartId } = cart;
|
|
237
|
+
const { isSignedIn } = user;
|
|
238
|
+
|
|
239
|
+
try {
|
|
240
|
+
if (productType === 'ConfigurableProduct') {
|
|
241
|
+
// You _must_ remove before adding or risk deleting the item
|
|
242
|
+
// entirely if only quantity has been modified.
|
|
243
|
+
await dispatch(
|
|
244
|
+
removeItemFromCart({
|
|
245
|
+
item: {
|
|
246
|
+
id: cartItemId
|
|
247
|
+
},
|
|
248
|
+
fetchCartDetails,
|
|
249
|
+
fetchCartId,
|
|
250
|
+
removeItem
|
|
251
|
+
})
|
|
252
|
+
);
|
|
253
|
+
await dispatch(
|
|
254
|
+
addItemToCart({
|
|
255
|
+
...payload
|
|
256
|
+
})
|
|
257
|
+
);
|
|
258
|
+
} else {
|
|
259
|
+
// If the product is a simple product we can just use the
|
|
260
|
+
// updateCartItems graphql mutation.
|
|
261
|
+
await updateItem({
|
|
262
|
+
variables: {
|
|
263
|
+
cartId,
|
|
264
|
+
itemId: cartItemId,
|
|
265
|
+
quantity
|
|
266
|
+
}
|
|
267
|
+
});
|
|
268
|
+
// The configurable product conditional dispatches actions that
|
|
269
|
+
// each call getCartDetails. For simple items we must request
|
|
270
|
+
// details after the mutation completes. This may change when
|
|
271
|
+
// we migrate to the `cart` query for details, away from REST.
|
|
272
|
+
await dispatch(
|
|
273
|
+
getCartDetails({
|
|
274
|
+
fetchCartId,
|
|
275
|
+
fetchCartDetails
|
|
276
|
+
})
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
dispatch(actions.updateItem.receive());
|
|
281
|
+
} catch (error) {
|
|
282
|
+
dispatch(actions.updateItem.receive(error));
|
|
283
|
+
|
|
284
|
+
const shouldRetry = !error.networkError && isInvalidCart(error);
|
|
285
|
+
if (shouldRetry) {
|
|
286
|
+
// Delete the cached ID from local storage and Redux.
|
|
287
|
+
// In contrast to the save, make sure storage deletion is
|
|
288
|
+
// complete before dispatching the error--you don't want an
|
|
289
|
+
// upstream action to try and reuse the known-bad ID.
|
|
290
|
+
await dispatch(removeCart());
|
|
291
|
+
|
|
292
|
+
// then create a new one
|
|
293
|
+
try {
|
|
294
|
+
await dispatch(
|
|
295
|
+
createCart({
|
|
296
|
+
fetchCartId
|
|
297
|
+
})
|
|
298
|
+
);
|
|
299
|
+
} catch (error) {
|
|
300
|
+
// If creating a cart fails, all is not lost. Return so that the
|
|
301
|
+
// user can continue to at least browse the site.
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// and fetch details
|
|
306
|
+
await dispatch(
|
|
307
|
+
getCartDetails({
|
|
308
|
+
fetchCartId,
|
|
309
|
+
fetchCartDetails
|
|
310
|
+
})
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
if (isSignedIn) {
|
|
314
|
+
// The user is signed in and we just received their cart.
|
|
315
|
+
// Retry this operation.
|
|
316
|
+
return thunk(...arguments);
|
|
317
|
+
} else {
|
|
318
|
+
// The user is a guest and just received a brand new (empty) cart.
|
|
319
|
+
// Add the updated item to that cart.
|
|
320
|
+
await dispatch(
|
|
321
|
+
addItemToCart({
|
|
322
|
+
...payload
|
|
323
|
+
})
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
};
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
export const removeItemFromCart = payload => {
|
|
332
|
+
const { item, fetchCartDetails, fetchCartId, removeItem } = payload;
|
|
333
|
+
|
|
334
|
+
return async function thunk(dispatch, getState) {
|
|
335
|
+
dispatch(actions.removeItem.request(payload));
|
|
336
|
+
|
|
337
|
+
const { cart } = getState();
|
|
338
|
+
const { cartId } = cart;
|
|
339
|
+
|
|
340
|
+
try {
|
|
341
|
+
await removeItem({
|
|
342
|
+
variables: {
|
|
343
|
+
cartId,
|
|
344
|
+
itemId: item.uid
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
dispatch(actions.removeItem.receive());
|
|
349
|
+
} catch (error) {
|
|
350
|
+
dispatch(actions.removeItem.receive(error));
|
|
351
|
+
|
|
352
|
+
const shouldResetCart = !error.networkError && isInvalidCart(error);
|
|
353
|
+
if (shouldResetCart) {
|
|
354
|
+
// Delete the cached ID from local storage.
|
|
355
|
+
// The reducer handles clearing out the bad ID from Redux.
|
|
356
|
+
// In contrast to the save, make sure storage deletion is
|
|
357
|
+
// complete before dispatching the error--you don't want an
|
|
358
|
+
// upstream action to try and reuse the known-bad ID.
|
|
359
|
+
await dispatch(removeCart());
|
|
360
|
+
// then create a new one
|
|
361
|
+
try {
|
|
362
|
+
await dispatch(
|
|
363
|
+
createCart({
|
|
364
|
+
fetchCartId
|
|
365
|
+
})
|
|
366
|
+
);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
// If creating a cart fails, all is not lost. Return so that the
|
|
369
|
+
// user can continue to at least browse the site.
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
await dispatch(
|
|
376
|
+
getCartDetails({
|
|
377
|
+
fetchCartId,
|
|
378
|
+
fetchCartDetails
|
|
379
|
+
})
|
|
380
|
+
);
|
|
381
|
+
};
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
export const getCartDetails = payload => {
|
|
385
|
+
const { fetchCartId, fetchCartDetails, removeSplitCart = null } = payload;
|
|
386
|
+
|
|
387
|
+
return async function thunk(dispatch, getState, { apolloClient }) {
|
|
388
|
+
const { cart, user } = getState();
|
|
389
|
+
const { cartId } = cart;
|
|
390
|
+
const { isSignedIn } = user;
|
|
391
|
+
|
|
392
|
+
const isCheckoutActive = await retrieveCheckoutState();
|
|
393
|
+
console.log('isCheckoutActive')
|
|
394
|
+
console.log(isCheckoutActive)
|
|
395
|
+
|
|
396
|
+
const isSplitCartActive = await retrieveSellerCartId();
|
|
397
|
+
console.log('isSplitCartActive')
|
|
398
|
+
console.log(isSplitCartActive)
|
|
399
|
+
|
|
400
|
+
if (cartId && isSplitCartActive && !isCheckoutActive) {
|
|
401
|
+
console.log('JALANIN = removeCart')
|
|
402
|
+
|
|
403
|
+
await dispatch(removeCart());
|
|
404
|
+
|
|
405
|
+
// Clear cart data from Apollo cache
|
|
406
|
+
await apolloClient.clearCacheData(apolloClient, 'cart');
|
|
407
|
+
|
|
408
|
+
// Create a new cart
|
|
409
|
+
try {
|
|
410
|
+
console.log('JALANIN RESET SPLIT CART')
|
|
411
|
+
await dispatch(
|
|
412
|
+
resetSplitCart({
|
|
413
|
+
removeSplitCart
|
|
414
|
+
})
|
|
415
|
+
);
|
|
416
|
+
|
|
417
|
+
await clearSellerCartId();
|
|
418
|
+
|
|
419
|
+
console.log('JALANIN CREATE CART')
|
|
420
|
+
|
|
421
|
+
await dispatch(
|
|
422
|
+
createCart({
|
|
423
|
+
fetchCartId
|
|
424
|
+
})
|
|
425
|
+
);
|
|
426
|
+
} catch (error) {
|
|
427
|
+
alert(error);
|
|
428
|
+
console.log(error);
|
|
429
|
+
// If creating a cart fails, all is not lost. Return so that the
|
|
430
|
+
// user can continue to at least browse the site.
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// Retry this operation
|
|
435
|
+
return thunk(...arguments);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// if there isn't a cart, create one then retry this operation
|
|
439
|
+
if (!cartId) {
|
|
440
|
+
try {
|
|
441
|
+
await dispatch(
|
|
442
|
+
createCart({
|
|
443
|
+
fetchCartId
|
|
444
|
+
})
|
|
445
|
+
);
|
|
446
|
+
} catch (error) {
|
|
447
|
+
// If creating a cart fails, all is not lost. Return so that the
|
|
448
|
+
// user can continue to at least browse the site.
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
return thunk(...arguments);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
// Once we have the cart id indicate that we are starting to make
|
|
455
|
+
// async requests for the details.
|
|
456
|
+
dispatch(actions.getDetails.request(cartId));
|
|
457
|
+
|
|
458
|
+
try {
|
|
459
|
+
const { data } = await fetchCartDetails({
|
|
460
|
+
variables: { cartId },
|
|
461
|
+
fetchPolicy: 'network-only'
|
|
462
|
+
});
|
|
463
|
+
const { cart: details } = data;
|
|
464
|
+
|
|
465
|
+
dispatch(actions.getDetails.receive({ details }));
|
|
466
|
+
} catch (error) {
|
|
467
|
+
dispatch(actions.getDetails.receive(error));
|
|
468
|
+
|
|
469
|
+
const shouldResetCart = !error.networkError && isInvalidCart(error);
|
|
470
|
+
if (shouldResetCart) {
|
|
471
|
+
if (isSignedIn) {
|
|
472
|
+
// Since simple persistence just deletes auth token without
|
|
473
|
+
// informing Redux, we need to perform the sign out action
|
|
474
|
+
// to reset the user and cart slices back to initial state.
|
|
475
|
+
await dispatch(signOut());
|
|
476
|
+
} else {
|
|
477
|
+
// Delete the cached ID from local storage.
|
|
478
|
+
await dispatch(removeCart());
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
// Clear cart data from Apollo cache
|
|
482
|
+
await apolloClient.clearCacheData(apolloClient, 'cart');
|
|
483
|
+
|
|
484
|
+
// Create a new cart
|
|
485
|
+
try {
|
|
486
|
+
await dispatch(
|
|
487
|
+
createCart({
|
|
488
|
+
fetchCartId
|
|
489
|
+
})
|
|
490
|
+
);
|
|
491
|
+
} catch (error) {
|
|
492
|
+
// If creating a cart fails, all is not lost. Return so that the
|
|
493
|
+
// user can continue to at least browse the site.
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// Retry this operation
|
|
498
|
+
return thunk(...arguments);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
export const removeCart = () =>
|
|
505
|
+
async function thunk(dispatch) {
|
|
506
|
+
// Clear the cartId from local storage.
|
|
507
|
+
await clearCartId();
|
|
508
|
+
|
|
509
|
+
// Clear the cart info from the redux store.
|
|
510
|
+
dispatch(actions.reset());
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
/* helpers */
|
|
514
|
+
export async function retrieveSellerCartId() {
|
|
515
|
+
// return 'ok';
|
|
516
|
+
return storage.getItem('sellerCartId');
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
export async function setCheckoutState(val) {
|
|
520
|
+
return storage.setItem('checkoutState', val);
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export async function retrieveCheckoutState() {
|
|
524
|
+
return storage.getItem('checkoutState');
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
export async function saveSellerCartId(id) {
|
|
528
|
+
return storage.setItem('sellerCartId', id);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
export async function clearSellerCartId() {
|
|
532
|
+
return storage.removeItem('sellerCartId');
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
export async function retrieveCartId() {
|
|
536
|
+
return storage.getItem('cartId');
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
export async function saveCartId(id) {
|
|
540
|
+
return storage.setItem('cartId', id);
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
export async function clearCartId() {
|
|
544
|
+
return storage.removeItem('cartId');
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
async function retrieveImageCache() {
|
|
548
|
+
return storage.getItem('imagesBySku') || {};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
async function saveImageCache(cache) {
|
|
552
|
+
return storage.setItem('imagesBySku', cache);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
export async function writeImageToCache(item = {}) {
|
|
556
|
+
const { media_gallery_entries: media, sku } = item;
|
|
557
|
+
|
|
558
|
+
if (sku) {
|
|
559
|
+
const image = media && (media.find(m => m.position === 1) || media[0]);
|
|
560
|
+
|
|
561
|
+
if (image) {
|
|
562
|
+
const imageCache = await retrieveImageCache();
|
|
563
|
+
|
|
564
|
+
// if there is an image and it differs from cache
|
|
565
|
+
// write to cache and save in the background
|
|
566
|
+
if (imageCache[sku] !== image) {
|
|
567
|
+
imageCache[sku] = image;
|
|
568
|
+
saveImageCache(imageCache);
|
|
569
|
+
|
|
570
|
+
return image;
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// Returns true if the cart is invalid.
|
|
577
|
+
function isInvalidCart(error) {
|
|
578
|
+
return !!(
|
|
579
|
+
error.graphQLErrors &&
|
|
580
|
+
error.graphQLErrors.find(
|
|
581
|
+
err =>
|
|
582
|
+
err.message.includes('Could not find a cart') ||
|
|
583
|
+
err.message.includes("The cart isn't active") ||
|
|
584
|
+
err.message.includes(
|
|
585
|
+
'The current user cannot perform operations on cart'
|
|
586
|
+
)
|
|
587
|
+
)
|
|
588
|
+
);
|
|
589
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useCallback } from 'react';
|
|
2
2
|
import { useHistory, useRouteMatch } from 'react-router-dom';
|
|
3
|
+
import { setCheckoutState } from '@magento/peregrine/lib/store/actions/cart';
|
|
3
4
|
import { useCartContext } from '@magento/peregrine/lib/context/cart';
|
|
4
5
|
import mergeOperations from '@magento/peregrine/lib/util/shallowMerge';
|
|
5
6
|
import DEFAULT_OPERATIONS from './priceSummary.gql';
|
|
@@ -94,6 +95,8 @@ export const usePriceSummary = (props = {}) => {
|
|
|
94
95
|
// }
|
|
95
96
|
// });
|
|
96
97
|
|
|
98
|
+
await setCheckoutState(true);
|
|
99
|
+
|
|
97
100
|
await createSellerCart({ initCheckoutSplitCart, sellerUrl });
|
|
98
101
|
|
|
99
102
|
// console.log('initCheckoutSplitCartData')
|
|
@@ -107,7 +110,7 @@ export const usePriceSummary = (props = {}) => {
|
|
|
107
110
|
});
|
|
108
111
|
|
|
109
112
|
history.push('/checkout');
|
|
110
|
-
}, [history, createSellerCart, fetchCartId, createCart, apolloClient, removeCart, cartId, initCheckoutSplitCart, initCheckoutSplitCartData]);
|
|
113
|
+
}, [history, setCheckoutState, createSellerCart, fetchCartId, createCart, apolloClient, removeCart, cartId, initCheckoutSplitCart, initCheckoutSplitCartData]);
|
|
111
114
|
|
|
112
115
|
const handleEnterKeyPress = useCallback(() => {
|
|
113
116
|
event => {
|