@merchi/cart 1.4.74 → 1.4.76
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/esm/CartProvider.js +30 -8
- package/esm/CartProvider.load.test.js +113 -62
- package/lib/CartProvider.js +30 -8
- package/package.json +2 -2
- package/src/CartProvider.load.test.tsx +11 -0
- package/src/CartProvider.tsx +22 -9
package/esm/CartProvider.js
CHANGED
|
@@ -409,13 +409,24 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
409
409
|
type: 'success'
|
|
410
410
|
});
|
|
411
411
|
}
|
|
412
|
-
function
|
|
412
|
+
function showCartError(message, fromInitialization) {
|
|
413
413
|
setAlert({
|
|
414
414
|
icon: faExclamationTriangle,
|
|
415
415
|
message: message,
|
|
416
416
|
show: true,
|
|
417
417
|
title: 'Error!',
|
|
418
|
-
type: 'danger'
|
|
418
|
+
type: 'danger',
|
|
419
|
+
fromInitialization: fromInitialization
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
function alertError(message) {
|
|
423
|
+
showCartError(message, false);
|
|
424
|
+
}
|
|
425
|
+
function dismissInitializationError() {
|
|
426
|
+
setAlert(function (previous) {
|
|
427
|
+
return previous.fromInitialization && previous.show ? _objectSpread(_objectSpread({}, previous), {}, {
|
|
428
|
+
show: false
|
|
429
|
+
}) : previous;
|
|
419
430
|
});
|
|
420
431
|
}
|
|
421
432
|
function closeClearCart() {
|
|
@@ -505,10 +516,19 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
505
516
|
}
|
|
506
517
|
function _createCartAndCookie() {
|
|
507
518
|
_createCartAndCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(callback) {
|
|
508
|
-
var
|
|
519
|
+
var initializing,
|
|
520
|
+
isTest,
|
|
521
|
+
jobSource,
|
|
522
|
+
_cart,
|
|
523
|
+
query,
|
|
524
|
+
created,
|
|
525
|
+
cartJson,
|
|
526
|
+
_args4 = arguments,
|
|
527
|
+
_t5;
|
|
509
528
|
return _regenerator().w(function (_context4) {
|
|
510
529
|
while (1) switch (_context4.p = _context4.n) {
|
|
511
530
|
case 0:
|
|
531
|
+
initializing = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : false;
|
|
512
532
|
setFetchingCart(true);
|
|
513
533
|
_context4.p = 1;
|
|
514
534
|
isTest = readTestCheckoutFlag();
|
|
@@ -540,13 +560,14 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
540
560
|
case 4:
|
|
541
561
|
cartJson = _context4.v;
|
|
542
562
|
setCart(_objectSpread({}, cartJson));
|
|
563
|
+
dismissInitializationError();
|
|
543
564
|
if (callback) callback();
|
|
544
565
|
return _context4.a(2, cartJson);
|
|
545
566
|
case 5:
|
|
546
567
|
_context4.p = 5;
|
|
547
568
|
_t5 = _context4.v;
|
|
548
569
|
reportCartFailure(_t5, CART_FAILURE_SOURCES.create);
|
|
549
|
-
|
|
570
|
+
showCartError((_t5 === null || _t5 === void 0 ? void 0 : _t5.errorMessage) || (_t5 === null || _t5 === void 0 ? void 0 : _t5.message) || 'Unable to fetch cart.', initializing);
|
|
550
571
|
case 6:
|
|
551
572
|
_context4.p = 6;
|
|
552
573
|
setFetchingCart(false);
|
|
@@ -605,7 +626,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
605
626
|
break;
|
|
606
627
|
}
|
|
607
628
|
_context6.n = 4;
|
|
608
|
-
return createCartAndCookie();
|
|
629
|
+
return createCartAndCookie(undefined, true);
|
|
609
630
|
case 4:
|
|
610
631
|
return _context6.a(2, _context6.v);
|
|
611
632
|
case 5:
|
|
@@ -634,6 +655,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
634
655
|
case 8:
|
|
635
656
|
cartJson = _context6.v;
|
|
636
657
|
setCart(_objectSpread({}, cartJson));
|
|
658
|
+
dismissInitializationError();
|
|
637
659
|
return _context6.a(2, cartJson);
|
|
638
660
|
case 9:
|
|
639
661
|
console.error("MErhci cart error: Stripe payment " + "options have not been correctly set up. " + "Check the company profile payment options tab " + "to set and edit stripe payment options.");
|
|
@@ -649,14 +671,14 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
649
671
|
break;
|
|
650
672
|
}
|
|
651
673
|
_context6.n = 12;
|
|
652
|
-
return createCartAndCookie();
|
|
674
|
+
return createCartAndCookie(undefined, true);
|
|
653
675
|
case 12:
|
|
654
676
|
return _context6.a(2, _context6.v);
|
|
655
677
|
case 13:
|
|
656
678
|
// A network/server/auth failure does not mean the saved cart is gone.
|
|
657
679
|
// Preserve its cookie and contents so the shopper can retry the read.
|
|
658
680
|
reportCartFailure(_t6, CART_FAILURE_SOURCES.load);
|
|
659
|
-
|
|
681
|
+
showCartError((_t6 === null || _t6 === void 0 ? void 0 : _t6.errorMessage) || (_t6 === null || _t6 === void 0 ? void 0 : _t6.message) || 'Unable to load cart. Please try again.', true);
|
|
660
682
|
case 14:
|
|
661
683
|
_context6.p = 14;
|
|
662
684
|
setFetchingCart(false);
|
|
@@ -746,7 +768,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
746
768
|
break;
|
|
747
769
|
}
|
|
748
770
|
_context.n = 5;
|
|
749
|
-
return createCartAndCookie();
|
|
771
|
+
return createCartAndCookie(undefined, true);
|
|
750
772
|
case 5:
|
|
751
773
|
_t2 = _context.v;
|
|
752
774
|
return _context.a(2, {
|
|
@@ -196,15 +196,15 @@ describe('cart load and replacement failures', function () {
|
|
|
196
196
|
return _mount.apply(this, arguments);
|
|
197
197
|
}
|
|
198
198
|
function _mount() {
|
|
199
|
-
_mount = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
200
|
-
return _regenerator().w(function (
|
|
201
|
-
while (1) switch (
|
|
199
|
+
_mount = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee84(error) {
|
|
200
|
+
return _regenerator().w(function (_context84) {
|
|
201
|
+
while (1) switch (_context84.n) {
|
|
202
202
|
case 0:
|
|
203
203
|
mockGet.mockRejectedValue(error);
|
|
204
|
-
|
|
205
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
206
|
-
return _regenerator().w(function (
|
|
207
|
-
while (1) switch (
|
|
204
|
+
_context84.n = 1;
|
|
205
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee83() {
|
|
206
|
+
return _regenerator().w(function (_context83) {
|
|
207
|
+
while (1) switch (_context83.n) {
|
|
208
208
|
case 0:
|
|
209
209
|
root.render(/*#__PURE__*/_jsx(CartProvider, {
|
|
210
210
|
domainId: 5,
|
|
@@ -218,14 +218,14 @@ describe('cart load and replacement failures', function () {
|
|
|
218
218
|
children: /*#__PURE__*/_jsx(Probe, {})
|
|
219
219
|
}));
|
|
220
220
|
case 1:
|
|
221
|
-
return
|
|
221
|
+
return _context83.a(2);
|
|
222
222
|
}
|
|
223
|
-
},
|
|
223
|
+
}, _callee83);
|
|
224
224
|
})));
|
|
225
225
|
case 1:
|
|
226
|
-
return
|
|
226
|
+
return _context84.a(2);
|
|
227
227
|
}
|
|
228
|
-
},
|
|
228
|
+
}, _callee84);
|
|
229
229
|
}));
|
|
230
230
|
return _mount.apply(this, arguments);
|
|
231
231
|
}
|
|
@@ -233,9 +233,9 @@ describe('cart load and replacement failures', function () {
|
|
|
233
233
|
return _mountSavedCart.apply(this, arguments);
|
|
234
234
|
}
|
|
235
235
|
function _mountSavedCart() {
|
|
236
|
-
_mountSavedCart = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
237
|
-
return _regenerator().w(function (
|
|
238
|
-
while (1) switch (
|
|
236
|
+
_mountSavedCart = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee87() {
|
|
237
|
+
return _regenerator().w(function (_context87) {
|
|
238
|
+
while (1) switch (_context87.n) {
|
|
239
239
|
case 0:
|
|
240
240
|
mockGet.mockResolvedValue({
|
|
241
241
|
toJson: function toJson() {
|
|
@@ -248,10 +248,10 @@ describe('cart load and replacement failures', function () {
|
|
|
248
248
|
};
|
|
249
249
|
}
|
|
250
250
|
});
|
|
251
|
-
|
|
252
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
253
|
-
return _regenerator().w(function (
|
|
254
|
-
while (1) switch (
|
|
251
|
+
_context87.n = 1;
|
|
252
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee85() {
|
|
253
|
+
return _regenerator().w(function (_context85) {
|
|
254
|
+
while (1) switch (_context85.n) {
|
|
255
255
|
case 0:
|
|
256
256
|
root.render(/*#__PURE__*/_jsx(CartProvider, {
|
|
257
257
|
domainId: 5,
|
|
@@ -259,24 +259,24 @@ describe('cart load and replacement failures', function () {
|
|
|
259
259
|
children: /*#__PURE__*/_jsx(Probe, {})
|
|
260
260
|
}));
|
|
261
261
|
case 1:
|
|
262
|
-
return
|
|
262
|
+
return _context85.a(2);
|
|
263
263
|
}
|
|
264
|
-
},
|
|
264
|
+
}, _callee85);
|
|
265
265
|
})));
|
|
266
266
|
case 1:
|
|
267
|
-
|
|
268
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
269
|
-
return _regenerator().w(function (
|
|
270
|
-
while (1) switch (
|
|
267
|
+
_context87.n = 2;
|
|
268
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee86() {
|
|
269
|
+
return _regenerator().w(function (_context86) {
|
|
270
|
+
while (1) switch (_context86.n) {
|
|
271
271
|
case 0:
|
|
272
|
-
return
|
|
272
|
+
return _context86.a(2, context.setActiveTabIndex(tabIdClearCart));
|
|
273
273
|
}
|
|
274
|
-
},
|
|
274
|
+
}, _callee86);
|
|
275
275
|
})));
|
|
276
276
|
case 2:
|
|
277
|
-
return
|
|
277
|
+
return _context87.a(2);
|
|
278
278
|
}
|
|
279
|
-
},
|
|
279
|
+
}, _callee87);
|
|
280
280
|
}));
|
|
281
281
|
return _mountSavedCart.apply(this, arguments);
|
|
282
282
|
}
|
|
@@ -906,6 +906,7 @@ describe('cart load and replacement failures', function () {
|
|
|
906
906
|
case 2:
|
|
907
907
|
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
908
908
|
expect(context.fetchingCart).toBe(false);
|
|
909
|
+
expect(context.alert.show).toBe(true);
|
|
909
910
|
_context38.n = 3;
|
|
910
911
|
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee37() {
|
|
911
912
|
return _regenerator().w(function (_context37) {
|
|
@@ -921,6 +922,7 @@ describe('cart load and replacement failures', function () {
|
|
|
921
922
|
case 3:
|
|
922
923
|
expect(mockPost).toHaveBeenCalledTimes(2);
|
|
923
924
|
expect(context.cart.id).toBe(99);
|
|
925
|
+
expect(context.alert.show).toBe(false);
|
|
924
926
|
case 4:
|
|
925
927
|
return _context38.a(2);
|
|
926
928
|
}
|
|
@@ -1566,34 +1568,83 @@ describe('cart load and replacement failures', function () {
|
|
|
1566
1568
|
expect(context.cart.id).toBe(42);
|
|
1567
1569
|
expect(context.cart.cartItems).toHaveLength(1);
|
|
1568
1570
|
expect(mockPost).not.toHaveBeenCalled();
|
|
1571
|
+
expect(context.alert.show).toBe(false);
|
|
1569
1572
|
case 3:
|
|
1570
1573
|
return _context74.a(2);
|
|
1571
1574
|
}
|
|
1572
1575
|
}, _callee74);
|
|
1573
1576
|
})));
|
|
1577
|
+
it('preserves an unrelated alert when a cart read succeeds', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee77() {
|
|
1578
|
+
return _regenerator().w(function (_context77) {
|
|
1579
|
+
while (1) switch (_context77.n) {
|
|
1580
|
+
case 0:
|
|
1581
|
+
_context77.n = 1;
|
|
1582
|
+
return mount(new TypeError('Load failed'));
|
|
1583
|
+
case 1:
|
|
1584
|
+
_context77.n = 2;
|
|
1585
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee75() {
|
|
1586
|
+
return _regenerator().w(function (_context75) {
|
|
1587
|
+
while (1) switch (_context75.n) {
|
|
1588
|
+
case 0:
|
|
1589
|
+
return _context75.a(2, context.alertError('The discount could not be applied.'));
|
|
1590
|
+
}
|
|
1591
|
+
}, _callee75);
|
|
1592
|
+
})));
|
|
1593
|
+
case 2:
|
|
1594
|
+
mockGet.mockResolvedValue({
|
|
1595
|
+
toJson: function toJson() {
|
|
1596
|
+
return {
|
|
1597
|
+
id: 42,
|
|
1598
|
+
isTest: false,
|
|
1599
|
+
cartItems: [{
|
|
1600
|
+
id: 7
|
|
1601
|
+
}]
|
|
1602
|
+
};
|
|
1603
|
+
}
|
|
1604
|
+
});
|
|
1605
|
+
_context77.n = 3;
|
|
1606
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee76() {
|
|
1607
|
+
return _regenerator().w(function (_context76) {
|
|
1608
|
+
while (1) switch (_context76.n) {
|
|
1609
|
+
case 0:
|
|
1610
|
+
_context76.n = 1;
|
|
1611
|
+
return window.getCart();
|
|
1612
|
+
case 1:
|
|
1613
|
+
return _context76.a(2);
|
|
1614
|
+
}
|
|
1615
|
+
}, _callee76);
|
|
1616
|
+
})));
|
|
1617
|
+
case 3:
|
|
1618
|
+
expect(context.alert.show).toBe(true);
|
|
1619
|
+
expect(context.alert.message).toBe('The discount could not be applied.');
|
|
1620
|
+
case 4:
|
|
1621
|
+
return _context77.a(2);
|
|
1622
|
+
}
|
|
1623
|
+
}, _callee77);
|
|
1624
|
+
})));
|
|
1574
1625
|
it.each([new TypeError('Failed to fetch'), {
|
|
1575
1626
|
statusCode: 503,
|
|
1576
1627
|
message: 'Service unavailable'
|
|
1577
1628
|
}, null, undefined])('keeps the clear-cart panel and saved cart when replacement fails with %s', /*#__PURE__*/function () {
|
|
1578
|
-
var
|
|
1579
|
-
return _regenerator().w(function (
|
|
1580
|
-
while (1) switch (
|
|
1629
|
+
var _ref78 = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee79(error) {
|
|
1630
|
+
return _regenerator().w(function (_context79) {
|
|
1631
|
+
while (1) switch (_context79.n) {
|
|
1581
1632
|
case 0:
|
|
1582
|
-
|
|
1633
|
+
_context79.n = 1;
|
|
1583
1634
|
return mountSavedCart();
|
|
1584
1635
|
case 1:
|
|
1585
1636
|
mockPost.mockRejectedValueOnce(error);
|
|
1586
|
-
|
|
1587
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
1588
|
-
return _regenerator().w(function (
|
|
1589
|
-
while (1) switch (
|
|
1637
|
+
_context79.n = 2;
|
|
1638
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee78() {
|
|
1639
|
+
return _regenerator().w(function (_context78) {
|
|
1640
|
+
while (1) switch (_context78.n) {
|
|
1590
1641
|
case 0:
|
|
1591
|
-
|
|
1642
|
+
_context78.n = 1;
|
|
1592
1643
|
return context.clearCart();
|
|
1593
1644
|
case 1:
|
|
1594
|
-
return
|
|
1645
|
+
return _context78.a(2);
|
|
1595
1646
|
}
|
|
1596
|
-
},
|
|
1647
|
+
}, _callee78);
|
|
1597
1648
|
})));
|
|
1598
1649
|
case 2:
|
|
1599
1650
|
expect(context.activeTabIndex).toBe(tabIdClearCart);
|
|
@@ -1611,47 +1662,47 @@ describe('cart load and replacement failures', function () {
|
|
|
1611
1662
|
expect(context.alert.message).toBe((error === null || error === void 0 ? void 0 : error.message) || 'Unable to fetch cart.');
|
|
1612
1663
|
expect(context.fetchingCart).toBe(false);
|
|
1613
1664
|
case 3:
|
|
1614
|
-
return
|
|
1665
|
+
return _context79.a(2);
|
|
1615
1666
|
}
|
|
1616
|
-
},
|
|
1667
|
+
}, _callee79);
|
|
1617
1668
|
}));
|
|
1618
1669
|
return function (_x5) {
|
|
1619
|
-
return
|
|
1670
|
+
return _ref78.apply(this, arguments);
|
|
1620
1671
|
};
|
|
1621
1672
|
}());
|
|
1622
|
-
it('finishes clearing only after an explicit retry successfully creates the replacement', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
1623
|
-
return _regenerator().w(function (
|
|
1624
|
-
while (1) switch (
|
|
1673
|
+
it('finishes clearing only after an explicit retry successfully creates the replacement', /*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee82() {
|
|
1674
|
+
return _regenerator().w(function (_context82) {
|
|
1675
|
+
while (1) switch (_context82.n) {
|
|
1625
1676
|
case 0:
|
|
1626
|
-
|
|
1677
|
+
_context82.n = 1;
|
|
1627
1678
|
return mountSavedCart();
|
|
1628
1679
|
case 1:
|
|
1629
1680
|
mockPost.mockRejectedValueOnce(new TypeError('Failed to fetch'));
|
|
1630
|
-
|
|
1631
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
1632
|
-
return _regenerator().w(function (
|
|
1633
|
-
while (1) switch (
|
|
1681
|
+
_context82.n = 2;
|
|
1682
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee80() {
|
|
1683
|
+
return _regenerator().w(function (_context80) {
|
|
1684
|
+
while (1) switch (_context80.n) {
|
|
1634
1685
|
case 0:
|
|
1635
|
-
|
|
1686
|
+
_context80.n = 1;
|
|
1636
1687
|
return context.clearCart();
|
|
1637
1688
|
case 1:
|
|
1638
|
-
return
|
|
1689
|
+
return _context80.a(2);
|
|
1639
1690
|
}
|
|
1640
|
-
},
|
|
1691
|
+
}, _callee80);
|
|
1641
1692
|
})));
|
|
1642
1693
|
case 2:
|
|
1643
1694
|
expect(context.activeTabIndex).toBe(tabIdClearCart);
|
|
1644
|
-
|
|
1645
|
-
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function
|
|
1646
|
-
return _regenerator().w(function (
|
|
1647
|
-
while (1) switch (
|
|
1695
|
+
_context82.n = 3;
|
|
1696
|
+
return act(/*#__PURE__*/_asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee81() {
|
|
1697
|
+
return _regenerator().w(function (_context81) {
|
|
1698
|
+
while (1) switch (_context81.n) {
|
|
1648
1699
|
case 0:
|
|
1649
|
-
|
|
1700
|
+
_context81.n = 1;
|
|
1650
1701
|
return context.clearCart();
|
|
1651
1702
|
case 1:
|
|
1652
|
-
return
|
|
1703
|
+
return _context81.a(2);
|
|
1653
1704
|
}
|
|
1654
|
-
},
|
|
1705
|
+
}, _callee81);
|
|
1655
1706
|
})));
|
|
1656
1707
|
case 3:
|
|
1657
1708
|
expect(mockPost).toHaveBeenCalledTimes(2);
|
|
@@ -1661,8 +1712,8 @@ describe('cart load and replacement failures', function () {
|
|
|
1661
1712
|
expect(context.alert.show).toBe(false);
|
|
1662
1713
|
expect(context.fetchingCart).toBe(false);
|
|
1663
1714
|
case 4:
|
|
1664
|
-
return
|
|
1715
|
+
return _context82.a(2);
|
|
1665
1716
|
}
|
|
1666
|
-
},
|
|
1717
|
+
}, _callee82);
|
|
1667
1718
|
})));
|
|
1668
1719
|
});
|
package/lib/CartProvider.js
CHANGED
|
@@ -415,13 +415,24 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
415
415
|
type: 'success'
|
|
416
416
|
});
|
|
417
417
|
}
|
|
418
|
-
function
|
|
418
|
+
function showCartError(message, fromInitialization) {
|
|
419
419
|
setAlert({
|
|
420
420
|
icon: _freeSolidSvgIcons.faExclamationTriangle,
|
|
421
421
|
message: message,
|
|
422
422
|
show: true,
|
|
423
423
|
title: 'Error!',
|
|
424
|
-
type: 'danger'
|
|
424
|
+
type: 'danger',
|
|
425
|
+
fromInitialization: fromInitialization
|
|
426
|
+
});
|
|
427
|
+
}
|
|
428
|
+
function alertError(message) {
|
|
429
|
+
showCartError(message, false);
|
|
430
|
+
}
|
|
431
|
+
function dismissInitializationError() {
|
|
432
|
+
setAlert(function (previous) {
|
|
433
|
+
return previous.fromInitialization && previous.show ? _objectSpread(_objectSpread({}, previous), {}, {
|
|
434
|
+
show: false
|
|
435
|
+
}) : previous;
|
|
425
436
|
});
|
|
426
437
|
}
|
|
427
438
|
function closeClearCart() {
|
|
@@ -511,10 +522,19 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
511
522
|
}
|
|
512
523
|
function _createCartAndCookie() {
|
|
513
524
|
_createCartAndCookie = _asyncToGenerator(/*#__PURE__*/_regenerator().m(function _callee4(callback) {
|
|
514
|
-
var
|
|
525
|
+
var initializing,
|
|
526
|
+
isTest,
|
|
527
|
+
jobSource,
|
|
528
|
+
_cart,
|
|
529
|
+
query,
|
|
530
|
+
created,
|
|
531
|
+
cartJson,
|
|
532
|
+
_args4 = arguments,
|
|
533
|
+
_t5;
|
|
515
534
|
return _regenerator().w(function (_context4) {
|
|
516
535
|
while (1) switch (_context4.p = _context4.n) {
|
|
517
536
|
case 0:
|
|
537
|
+
initializing = _args4.length > 1 && _args4[1] !== undefined ? _args4[1] : false;
|
|
518
538
|
setFetchingCart(true);
|
|
519
539
|
_context4.p = 1;
|
|
520
540
|
isTest = (0, _test_checkout.readTestCheckoutFlag)();
|
|
@@ -546,13 +566,14 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
546
566
|
case 4:
|
|
547
567
|
cartJson = _context4.v;
|
|
548
568
|
setCart(_objectSpread({}, cartJson));
|
|
569
|
+
dismissInitializationError();
|
|
549
570
|
if (callback) callback();
|
|
550
571
|
return _context4.a(2, cartJson);
|
|
551
572
|
case 5:
|
|
552
573
|
_context4.p = 5;
|
|
553
574
|
_t5 = _context4.v;
|
|
554
575
|
(0, _reportFailure.reportCartFailure)(_t5, _reportFailure.CART_FAILURE_SOURCES.create);
|
|
555
|
-
|
|
576
|
+
showCartError((_t5 === null || _t5 === void 0 ? void 0 : _t5.errorMessage) || (_t5 === null || _t5 === void 0 ? void 0 : _t5.message) || 'Unable to fetch cart.', initializing);
|
|
556
577
|
case 6:
|
|
557
578
|
_context4.p = 6;
|
|
558
579
|
setFetchingCart(false);
|
|
@@ -611,7 +632,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
611
632
|
break;
|
|
612
633
|
}
|
|
613
634
|
_context6.n = 4;
|
|
614
|
-
return createCartAndCookie();
|
|
635
|
+
return createCartAndCookie(undefined, true);
|
|
615
636
|
case 4:
|
|
616
637
|
return _context6.a(2, _context6.v);
|
|
617
638
|
case 5:
|
|
@@ -640,6 +661,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
640
661
|
case 8:
|
|
641
662
|
cartJson = _context6.v;
|
|
642
663
|
setCart(_objectSpread({}, cartJson));
|
|
664
|
+
dismissInitializationError();
|
|
643
665
|
return _context6.a(2, cartJson);
|
|
644
666
|
case 9:
|
|
645
667
|
console.error("MErhci cart error: Stripe payment " + "options have not been correctly set up. " + "Check the company profile payment options tab " + "to set and edit stripe payment options.");
|
|
@@ -655,14 +677,14 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
655
677
|
break;
|
|
656
678
|
}
|
|
657
679
|
_context6.n = 12;
|
|
658
|
-
return createCartAndCookie();
|
|
680
|
+
return createCartAndCookie(undefined, true);
|
|
659
681
|
case 12:
|
|
660
682
|
return _context6.a(2, _context6.v);
|
|
661
683
|
case 13:
|
|
662
684
|
// A network/server/auth failure does not mean the saved cart is gone.
|
|
663
685
|
// Preserve its cookie and contents so the shopper can retry the read.
|
|
664
686
|
(0, _reportFailure.reportCartFailure)(_t6, _reportFailure.CART_FAILURE_SOURCES.load);
|
|
665
|
-
|
|
687
|
+
showCartError((_t6 === null || _t6 === void 0 ? void 0 : _t6.errorMessage) || (_t6 === null || _t6 === void 0 ? void 0 : _t6.message) || 'Unable to load cart. Please try again.', true);
|
|
666
688
|
case 14:
|
|
667
689
|
_context6.p = 14;
|
|
668
690
|
setFetchingCart(false);
|
|
@@ -752,7 +774,7 @@ var CartProvider = function CartProvider(_ref) {
|
|
|
752
774
|
break;
|
|
753
775
|
}
|
|
754
776
|
_context.n = 5;
|
|
755
|
-
return createCartAndCookie();
|
|
777
|
+
return createCartAndCookie(undefined, true);
|
|
756
778
|
case 5:
|
|
757
779
|
_t2 = _context.v;
|
|
758
780
|
return _context.a(2, {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@merchi/cart",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.76",
|
|
4
4
|
"description": "Merchi's cart",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -95,7 +95,7 @@
|
|
|
95
95
|
"@fortawesome/react-fontawesome": "^0.2.0",
|
|
96
96
|
"@merchi/error-catch": "^0.1.12",
|
|
97
97
|
"@merchi/invoice": "^1.3.19",
|
|
98
|
-
"@merchi/product-form": "^1.19.
|
|
98
|
+
"@merchi/product-form": "^1.19.60",
|
|
99
99
|
"@merchi/product-form-sdk": "^1.0.39",
|
|
100
100
|
"@merchi/sdk": "^1.42.28",
|
|
101
101
|
"@stripe/react-stripe-js": "^2.3.0",
|
|
@@ -282,9 +282,11 @@ describe('cart load and replacement failures', () => {
|
|
|
282
282
|
await act(async () => { await (window as any).getCart(); });
|
|
283
283
|
expect(mockPost).toHaveBeenCalledTimes(1);
|
|
284
284
|
expect(context.fetchingCart).toBe(false);
|
|
285
|
+
expect(context.alert.show).toBe(true);
|
|
285
286
|
await act(async () => { await (window as any).getCart(); });
|
|
286
287
|
expect(mockPost).toHaveBeenCalledTimes(2);
|
|
287
288
|
expect(context.cart.id).toBe(99);
|
|
289
|
+
expect(context.alert.show).toBe(false);
|
|
288
290
|
});
|
|
289
291
|
it('initializes a cart for a direct item operation without a storefront readiness helper', async () => {
|
|
290
292
|
setUserAgent(headlessUserAgent);
|
|
@@ -459,6 +461,15 @@ describe('cart load and replacement failures', () => {
|
|
|
459
461
|
expect(context.cart.id).toBe(42);
|
|
460
462
|
expect(context.cart.cartItems).toHaveLength(1);
|
|
461
463
|
expect(mockPost).not.toHaveBeenCalled();
|
|
464
|
+
expect(context.alert.show).toBe(false);
|
|
465
|
+
});
|
|
466
|
+
it('preserves an unrelated alert when a cart read succeeds', async () => {
|
|
467
|
+
await mount(new TypeError('Load failed'));
|
|
468
|
+
await act(async () => context.alertError('The discount could not be applied.'));
|
|
469
|
+
mockGet.mockResolvedValue({ toJson: () => ({ id: 42, isTest: false, cartItems: [{ id: 7 }] }) });
|
|
470
|
+
await act(async () => { await (window as any).getCart(); });
|
|
471
|
+
expect(context.alert.show).toBe(true);
|
|
472
|
+
expect(context.alert.message).toBe('The discount could not be applied.');
|
|
462
473
|
});
|
|
463
474
|
it.each([
|
|
464
475
|
new TypeError('Failed to fetch'),
|
package/src/CartProvider.tsx
CHANGED
|
@@ -564,13 +564,13 @@ const CartProvider = ({
|
|
|
564
564
|
|
|
565
565
|
const [cartItem, setCartItem] = useState(({} as any));
|
|
566
566
|
|
|
567
|
-
const [alert, setAlert] = useState(
|
|
567
|
+
const [alert, setAlert] = useState<Alert & { fromInitialization?: boolean }>({
|
|
568
568
|
icon: faInfoCircle,
|
|
569
569
|
message: '',
|
|
570
570
|
show: false,
|
|
571
571
|
title: '',
|
|
572
572
|
type: 'info',
|
|
573
|
-
}
|
|
573
|
+
});
|
|
574
574
|
|
|
575
575
|
function alertSuccess(message: string) {
|
|
576
576
|
setAlert({
|
|
@@ -582,16 +582,27 @@ const CartProvider = ({
|
|
|
582
582
|
});
|
|
583
583
|
}
|
|
584
584
|
|
|
585
|
-
function
|
|
585
|
+
function showCartError(message: string, fromInitialization: boolean) {
|
|
586
586
|
setAlert({
|
|
587
587
|
icon: faExclamationTriangle,
|
|
588
588
|
message,
|
|
589
589
|
show: true,
|
|
590
590
|
title: 'Error!',
|
|
591
591
|
type: 'danger',
|
|
592
|
+
fromInitialization,
|
|
592
593
|
});
|
|
593
594
|
}
|
|
594
595
|
|
|
596
|
+
function alertError(message: string) {
|
|
597
|
+
showCartError(message, false);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
function dismissInitializationError() {
|
|
601
|
+
setAlert(previous => previous.fromInitialization && previous.show
|
|
602
|
+
? { ...previous, show: false }
|
|
603
|
+
: previous);
|
|
604
|
+
}
|
|
605
|
+
|
|
595
606
|
function closeClearCart() {
|
|
596
607
|
setActiveTabIndex(tabIdItems);
|
|
597
608
|
}
|
|
@@ -633,7 +644,7 @@ const CartProvider = ({
|
|
|
633
644
|
}
|
|
634
645
|
|
|
635
646
|
// Create cart and save cart cookie
|
|
636
|
-
async function createCartAndCookie(callback?: () => void) {
|
|
647
|
+
async function createCartAndCookie(callback?: () => void, initializing = false) {
|
|
637
648
|
setFetchingCart(true);
|
|
638
649
|
try {
|
|
639
650
|
const isTest = readTestCheckoutFlag();
|
|
@@ -654,11 +665,12 @@ const CartProvider = ({
|
|
|
654
665
|
if (domainId) setCartCookie(Number(domainId), cartJson, undefined, isTest);
|
|
655
666
|
cartJson = await attachSavedClientIfNeeded(cartJson);
|
|
656
667
|
setCart({...cartJson});
|
|
668
|
+
dismissInitializationError();
|
|
657
669
|
if (callback) callback();
|
|
658
670
|
return cartJson;
|
|
659
671
|
} catch (e: any) {
|
|
660
672
|
reportCartFailure(e, CART_FAILURE_SOURCES.create);
|
|
661
|
-
|
|
673
|
+
showCartError(e?.errorMessage || e?.message || 'Unable to fetch cart.', initializing);
|
|
662
674
|
} finally {
|
|
663
675
|
setFetchingCart(false);
|
|
664
676
|
}
|
|
@@ -680,7 +692,7 @@ const CartProvider = ({
|
|
|
680
692
|
let cartJson = await cart.toJson();
|
|
681
693
|
const isTest = readTestCheckoutFlag();
|
|
682
694
|
if (Boolean(cartJson.isTest) !== isTest) {
|
|
683
|
-
return await createCartAndCookie();
|
|
695
|
+
return await createCartAndCookie(undefined, true);
|
|
684
696
|
}
|
|
685
697
|
const jobSource = jobSourceFieldsForApi(captureAndReadJobSource());
|
|
686
698
|
if (cartNeedsSourceUpdate(cartJson, jobSource)) {
|
|
@@ -698,6 +710,7 @@ const CartProvider = ({
|
|
|
698
710
|
if (stripeIsValidAndActive(cartJson) || isTest) {
|
|
699
711
|
cartJson = await attachSavedClientIfNeeded(cartJson);
|
|
700
712
|
setCart({...cartJson});
|
|
713
|
+
dismissInitializationError();
|
|
701
714
|
return cartJson;
|
|
702
715
|
} else {
|
|
703
716
|
console.error(
|
|
@@ -710,12 +723,12 @@ const CartProvider = ({
|
|
|
710
723
|
}
|
|
711
724
|
} catch(e: any){
|
|
712
725
|
if (canReplaceMissingCart(e)) {
|
|
713
|
-
return await createCartAndCookie();
|
|
726
|
+
return await createCartAndCookie(undefined, true);
|
|
714
727
|
} else {
|
|
715
728
|
// A network/server/auth failure does not mean the saved cart is gone.
|
|
716
729
|
// Preserve its cookie and contents so the shopper can retry the read.
|
|
717
730
|
reportCartFailure(e, CART_FAILURE_SOURCES.load);
|
|
718
|
-
|
|
731
|
+
showCartError(e?.errorMessage || e?.message || 'Unable to load cart. Please try again.', true);
|
|
719
732
|
}
|
|
720
733
|
} finally {
|
|
721
734
|
setFetchingCart(false);
|
|
@@ -746,7 +759,7 @@ const CartProvider = ({
|
|
|
746
759
|
if (cartIdAndToken) {
|
|
747
760
|
return { skippedCreation: false, cart: await getCart(cartIdAndToken) };
|
|
748
761
|
} else if (allowCreation) {
|
|
749
|
-
return { skippedCreation: false, cart: await createCartAndCookie() };
|
|
762
|
+
return { skippedCreation: false, cart: await createCartAndCookie(undefined, true) };
|
|
750
763
|
}
|
|
751
764
|
return { skippedCreation: true };
|
|
752
765
|
}).finally(() => {
|