merchi_cart 1.3.2 → 1.3.4

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.
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+
3
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.CustomCartProductForm = CustomCartProductForm;
8
+ exports["default"] = void 0;
9
+ var _react = _interopRequireWildcard(require("react"));
10
+ var _productFormSdkExternals = require("./product-form-sdk-externals");
11
+ var _customForm = require("../utilities/customForm");
12
+ var _LoadingTemplate = require("./LoadingTemplate");
13
+ var _jsxRuntime = require("react/jsx-runtime");
14
+ function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
15
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
16
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
17
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
18
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
19
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
20
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
21
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
22
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
23
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
24
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
25
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
26
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
+ /**
28
+ * Loads a product's published custom form for cart-item edit. On any failure
29
+ * renders `fallback` (the default merchi_product_form) so the cart stays usable.
30
+ */
31
+ function CustomCartProductForm(_ref) {
32
+ var _publishedFormBundle$, _publishedFormBundle;
33
+ var product = _ref.product,
34
+ initialJob = _ref.initialJob,
35
+ apiUrl = _ref.apiUrl,
36
+ onSave = _ref.onSave,
37
+ fallback = _ref.fallback,
38
+ onActiveChange = _ref.onActiveChange;
39
+ var _useState = (0, _react.useState)(null),
40
+ _useState2 = _slicedToArray(_useState, 2),
41
+ Loaded = _useState2[0],
42
+ setLoaded = _useState2[1];
43
+ var _useState3 = (0, _react.useState)(false),
44
+ _useState4 = _slicedToArray(_useState3, 2),
45
+ failed = _useState4[0],
46
+ setFailed = _useState4[1];
47
+ var _useState5 = (0, _react.useState)(true),
48
+ _useState6 = _slicedToArray(_useState5, 2),
49
+ loading = _useState6[0],
50
+ setLoading = _useState6[1];
51
+ var onSaveRef = (0, _react.useRef)(onSave);
52
+ onSaveRef.current = onSave;
53
+ var onActiveChangeRef = (0, _react.useRef)(onActiveChange);
54
+ onActiveChangeRef.current = onActiveChange;
55
+ var bundleKey = (_publishedFormBundle$ = (_publishedFormBundle = (0, _customForm.publishedFormBundle)(product)) === null || _publishedFormBundle === void 0 ? void 0 : _publishedFormBundle.url) !== null && _publishedFormBundle$ !== void 0 ? _publishedFormBundle$ : '';
56
+ (0, _react.useEffect)(function () {
57
+ var active = true;
58
+ setLoaded(null);
59
+ setFailed(false);
60
+ setLoading(true);
61
+ var bundle = (0, _customForm.publishedFormBundle)(product);
62
+ if (!bundle) {
63
+ setFailed(true);
64
+ setLoading(false);
65
+ return;
66
+ }
67
+ var w = window;
68
+ w.React = _react["default"];
69
+ w.MerchiProductFormSdk = _productFormSdkExternals.MerchiProductFormSdkExternals;
70
+ (0, _customForm.loadFormBundle)(bundle).then(function (Component) {
71
+ if (active) {
72
+ setLoaded(function () {
73
+ return Component;
74
+ });
75
+ setLoading(false);
76
+ }
77
+ })["catch"](function (e) {
78
+ console.error('[merchi_cart] custom form load failed', e);
79
+ if (active) {
80
+ setFailed(true);
81
+ setLoading(false);
82
+ }
83
+ });
84
+ return function () {
85
+ active = false;
86
+ };
87
+ }, [product, bundleKey]);
88
+ var props = (0, _react.useMemo)(function () {
89
+ try {
90
+ var runtime = _productFormSdkExternals.MerchiProductFormSdkExternals.createProductFormRuntime({
91
+ apiUrl: apiUrl || '',
92
+ product: product,
93
+ onAddToCart: function onAddToCart(job) {
94
+ var _onSaveRef$current;
95
+ return (_onSaveRef$current = onSaveRef.current) === null || _onSaveRef$current === void 0 ? void 0 : _onSaveRef$current.call(onSaveRef, job);
96
+ }
97
+ });
98
+ return {
99
+ product: product,
100
+ pricing: runtime.pricing,
101
+ actions: runtime.actions,
102
+ helpers: runtime.helpers
103
+ };
104
+ } catch (e) {
105
+ console.error('[merchi_cart] custom form props build failed', e);
106
+ return null;
107
+ }
108
+ }, [product, apiUrl]);
109
+ var isActive = !failed && !loading && !!Loaded && !!props;
110
+ (0, _react.useEffect)(function () {
111
+ var _onActiveChangeRef$cu;
112
+ (_onActiveChangeRef$cu = onActiveChangeRef.current) === null || _onActiveChangeRef$cu === void 0 || _onActiveChangeRef$cu.call(onActiveChangeRef, isActive);
113
+ return function () {
114
+ var _onActiveChangeRef$cu2;
115
+ return (_onActiveChangeRef$cu2 = onActiveChangeRef.current) === null || _onActiveChangeRef$cu2 === void 0 ? void 0 : _onActiveChangeRef$cu2.call(onActiveChangeRef, false);
116
+ };
117
+ }, [isActive]);
118
+ if (loading) return /*#__PURE__*/(0, _jsxRuntime.jsx)(_LoadingTemplate.LoadingTemplateSm, {});
119
+ if (failed || !Loaded || !props) return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
120
+ children: fallback
121
+ });
122
+ var Shell = _productFormSdkExternals.MerchiProductFormSdkExternals.ProductFormShell;
123
+ return /*#__PURE__*/(0, _jsxRuntime.jsx)(_jsxRuntime.Fragment, {
124
+ children: Shell ? /*#__PURE__*/(0, _jsxRuntime.jsx)(Shell, {
125
+ product: props.product,
126
+ pricing: props.pricing,
127
+ actions: props.actions,
128
+ helpers: props.helpers,
129
+ initialJob: initialJob,
130
+ actionLabels: {
131
+ addToCart: 'Save'
132
+ },
133
+ children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Loaded, _objectSpread({}, props))
134
+ }) : /*#__PURE__*/(0, _jsxRuntime.jsx)(Loaded, _objectSpread({}, props))
135
+ });
136
+ }
137
+ var _default = exports["default"] = CustomCartProductForm;
@@ -0,0 +1,42 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.MerchiProductFormSdkExternals = void 0;
7
+ var _merchi_sdk_product_form = require("merchi_sdk_product_form");
8
+ /**
9
+ * Full merchi_sdk_product_form runtime for window.MerchiProductFormSdk.
10
+ * Keep in sync with merchi_api/common/js/product_form_gate.cjs SDK_EXPORTS.
11
+ */
12
+
13
+ var MerchiProductFormSdkExternals = exports.MerchiProductFormSdkExternals = {
14
+ SDK_VERSION: _merchi_sdk_product_form.SDK_VERSION,
15
+ serializeJob: _merchi_sdk_product_form.serializeJob,
16
+ nonEmptyGroups: _merchi_sdk_product_form.nonEmptyGroups,
17
+ formatCurrency: _merchi_sdk_product_form.formatCurrency,
18
+ urlFor: _merchi_sdk_product_form.urlFor,
19
+ createPricing: _merchi_sdk_product_form.createPricing,
20
+ createProductFormRuntime: _merchi_sdk_product_form.createProductFormRuntime,
21
+ productHasGroups: _merchi_sdk_product_form.productHasGroups,
22
+ groupFieldsOf: _merchi_sdk_product_form.groupFieldsOf,
23
+ independentFieldsOf: _merchi_sdk_product_form.independentFieldsOf,
24
+ isOptionQuantityGridProduct: _merchi_sdk_product_form.isOptionQuantityGridProduct,
25
+ buildProductFormJob: _merchi_sdk_product_form.buildProductFormJob,
26
+ jobToFormState: _merchi_sdk_product_form.jobToFormState,
27
+ selectionsToVariations: _merchi_sdk_product_form.selectionsToVariations,
28
+ Section: _merchi_sdk_product_form.Section,
29
+ Stack: _merchi_sdk_product_form.Stack,
30
+ Card: _merchi_sdk_product_form.Card,
31
+ Heading: _merchi_sdk_product_form.Heading,
32
+ Text: _merchi_sdk_product_form.Text,
33
+ Divider: _merchi_sdk_product_form.Divider,
34
+ Field: _merchi_sdk_product_form.Field,
35
+ GroupRows: _merchi_sdk_product_form.GroupRows,
36
+ OptionQuantityGrid: _merchi_sdk_product_form.OptionQuantityGrid,
37
+ ProductFormProvider: _merchi_sdk_product_form.ProductFormProvider,
38
+ ProductFormShell: _merchi_sdk_product_form.ProductFormShell,
39
+ useProductForm: _merchi_sdk_product_form.useProductForm,
40
+ OPTION_FIELD_TYPES: _merchi_sdk_product_form.OPTION_FIELD_TYPES,
41
+ theme: _merchi_sdk_product_form.theme
42
+ };
@@ -18,6 +18,7 @@ var _InputError = _interopRequireDefault(require("./InputError"));
18
18
  var _user = require("../utilities/user");
19
19
  var _cart2 = require("../utilities/cart");
20
20
  var _cookie = require("../utilities/cookie");
21
+ var _local_storage = require("../utilities/local_storage");
21
22
  var _jsxRuntime = require("react/jsx-runtime");
22
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
23
24
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
@@ -38,6 +39,7 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
38
39
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
39
40
  var emailValidation = exports.emailValidation = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]/i;
40
41
  function FormCustomerNew() {
42
+ var _savedCustomer$phoneN, _savedCustomer$phoneN2;
41
43
  var _useCartContext = (0, _CartProvider.useCartContext)(),
42
44
  cart = _useCartContext.cart,
43
45
  classNameBtnPrimary = _useCartContext.classNameBtnPrimary,
@@ -59,12 +61,13 @@ function FormCustomerNew() {
59
61
  _React$useState6 = _slicedToArray(_React$useState5, 2),
60
62
  acceptConditions = _React$useState6[0],
61
63
  setAcceptConditions = _React$useState6[1];
64
+ var savedCustomer = (0, _local_storage.getSavedCheckoutCustomer)(domainId);
62
65
  function actionCreateNewCustomer(_x) {
63
66
  return _actionCreateNewCustomer.apply(this, arguments);
64
67
  }
65
68
  function _actionCreateNewCustomer() {
66
69
  _actionCreateNewCustomer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(customerJson) {
67
- var _cartJson$client, r, user, cartToken, clientEnt, cartEnt, _cart, cartJson;
70
+ var _cartJson$client, _customerJson$emailAd, r, user, cartToken, clientEnt, cartEnt, _cart, cartJson;
68
71
  return _regeneratorRuntime().wrap(function _callee$(_context) {
69
72
  while (1) switch (_context.prev = _context.next) {
70
73
  case 0:
@@ -108,37 +111,43 @@ function FormCustomerNew() {
108
111
  }
109
112
  throw new Error('Unable to attach client to cart.');
110
113
  case 21:
114
+ (0, _local_storage.saveCheckoutCustomer)(domainId, {
115
+ id: user.id,
116
+ name: customerJson.name,
117
+ emailAddress: (_customerJson$emailAd = customerJson.emailAddresses) === null || _customerJson$emailAd === void 0 || (_customerJson$emailAd = _customerJson$emailAd[0]) === null || _customerJson$emailAd === void 0 ? void 0 : _customerJson$emailAd.emailAddress,
118
+ phoneNumbers: customerJson.phoneNumbers
119
+ });
111
120
  setCartClient(cartJson.client);
112
121
  setCart(cartJson);
113
- _context.next = 28;
122
+ _context.next = 29;
114
123
  break;
115
- case 25:
116
- _context.prev = 25;
124
+ case 26:
125
+ _context.prev = 26;
117
126
  _context.t0 = _context["catch"](2);
118
127
  setError({
119
128
  message: _context.t0.errorMessage || _context.t0.message || 'Unable to attach client to cart.'
120
129
  });
121
- case 28:
122
- _context.prev = 28;
130
+ case 29:
131
+ _context.prev = 29;
123
132
  setLoading(false);
124
- return _context.finish(28);
125
- case 31:
133
+ return _context.finish(29);
134
+ case 32:
126
135
  case "end":
127
136
  return _context.stop();
128
137
  }
129
- }, _callee, null, [[2, 25, 28, 31]]);
138
+ }, _callee, null, [[2, 26, 29, 32]]);
130
139
  }));
131
140
  return _actionCreateNewCustomer.apply(this, arguments);
132
141
  }
133
142
  var _useForm = (0, _reactHookForm.useForm)({
134
143
  defaultValues: {
135
144
  emailAddresses: [{
136
- emailAddress: ''
145
+ emailAddress: (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.emailAddress) || ''
137
146
  }],
138
- name: '',
147
+ name: (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.name) || '',
139
148
  phoneNumbers: [{
140
- code: 'AU',
141
- number: ''
149
+ code: (savedCustomer === null || savedCustomer === void 0 || (_savedCustomer$phoneN = savedCustomer.phoneNumbers) === null || _savedCustomer$phoneN === void 0 || (_savedCustomer$phoneN = _savedCustomer$phoneN[0]) === null || _savedCustomer$phoneN === void 0 ? void 0 : _savedCustomer$phoneN.code) || 'AU',
150
+ number: (savedCustomer === null || savedCustomer === void 0 || (_savedCustomer$phoneN2 = savedCustomer.phoneNumbers) === null || _savedCustomer$phoneN2 === void 0 || (_savedCustomer$phoneN2 = _savedCustomer$phoneN2[0]) === null || _savedCustomer$phoneN2 === void 0 ? void 0 : _savedCustomer$phoneN2.number) || ''
142
151
  }],
143
152
  registeredAsGuest: false
144
153
  }
@@ -14,6 +14,7 @@ var _user = require("../utilities/user");
14
14
  var _cart2 = require("../utilities/cart");
15
15
  var _cookie = require("../utilities/cookie");
16
16
  var _helpers = require("../utilities/helpers");
17
+ var _local_storage = require("../utilities/local_storage");
17
18
  var _jsxRuntime = require("react/jsx-runtime");
18
19
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
19
20
  function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
@@ -52,9 +53,10 @@ function FormReturningCustomer() {
52
53
  _React$useState4 = _slicedToArray(_React$useState3, 2),
53
54
  error = _React$useState4[0],
54
55
  setError = _React$useState4[1];
56
+ var savedCustomer = (0, _local_storage.getSavedCheckoutCustomer)(domainId);
55
57
  var hookForm = (0, _reactHookForm.useForm)({
56
58
  defaultValues: {
57
- emailAddress: ''
59
+ emailAddress: (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.emailAddress) || ''
58
60
  }
59
61
  });
60
62
  var handleSubmit = hookForm.handleSubmit,
@@ -66,7 +68,7 @@ function FormReturningCustomer() {
66
68
  }
67
69
  function _submit() {
68
70
  _submit = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
69
- var _getValues, emailAddress, _user$user_id, _cartJson$client, user, userId, userJson, cartToken, clientEnt, cartEnt, _cart, cartJson;
71
+ var _getValues, emailAddress, _user$user_id, _cartJson$client, user, userId, cartToken, clientEnt, cartEnt, _cart, cartJson;
70
72
  return _regeneratorRuntime().wrap(function _callee$(_context) {
71
73
  while (1) switch (_context.prev = _context.next) {
72
74
  case 0:
@@ -86,33 +88,33 @@ function FormReturningCustomer() {
86
88
  }
87
89
  throw new Error('Unable to find returning customer.');
88
90
  case 10:
89
- userJson = {
90
- id: userId,
91
- emailAddresses: [{
92
- emailAddress: emailAddress
93
- }],
94
- name: user.name || 'Hidden for privacy'
95
- }; // patch cart with returning user
96
- _context.next = 13;
91
+ _context.next = 12;
97
92
  return (0, _cookie.getCartCookieToken)(domainId);
98
- case 13:
93
+ case 12:
99
94
  cartToken = _context.sent;
100
- clientEnt = (0, _user.makeUser)(userJson, true);
95
+ clientEnt = (0, _user.makeUser)({
96
+ id: userId
97
+ }, true);
101
98
  cartEnt = (0, _cart2.makeCart)(_objectSpread({}, cart), false, cartToken);
102
99
  cartEnt.client = clientEnt;
103
- _context.next = 19;
100
+ _context.next = 18;
104
101
  return cartEnt.save({
105
102
  embed: _helpers.cartEmbed
106
103
  });
107
- case 19:
104
+ case 18:
108
105
  _cart = _context.sent;
109
106
  cartJson = _cart.toJson();
110
107
  if (cartJson !== null && cartJson !== void 0 && (_cartJson$client = cartJson.client) !== null && _cartJson$client !== void 0 && _cartJson$client.id) {
111
- _context.next = 23;
108
+ _context.next = 22;
112
109
  break;
113
110
  }
114
111
  throw new Error('Unable to attach client to cart.');
115
- case 23:
112
+ case 22:
113
+ (0, _local_storage.saveCheckoutCustomer)(domainId, {
114
+ id: userId,
115
+ name: user.name || (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.name),
116
+ emailAddress: emailAddress
117
+ });
116
118
  setCartClient(cartJson.client);
117
119
  setCart(cartJson);
118
120
  _context.next = 31;
@@ -18,6 +18,7 @@ var _address = require("../utilities/address");
18
18
  var _cart = require("../utilities/cart");
19
19
  var _helpers = require("../utilities/helpers");
20
20
  var _tabs = require("../utilities/tabs");
21
+ var _local_storage = require("../utilities/local_storage");
21
22
  var _jsxRuntime = require("react/jsx-runtime");
22
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
23
24
  function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
@@ -114,9 +115,10 @@ function ActiveFormShipmentAddressAndNotes() {
114
115
  setCart = _useCartContext2.setCart,
115
116
  setLoadingTotals = _useCartContext2.setLoadingTotals,
116
117
  setActiveTabAndEditDisabled = _useCartContext2.setActiveTabAndEditDisabled;
117
- var _cart$receiverAddress = cart.receiverAddress,
118
- receiverAddress = _cart$receiverAddress === void 0 ? {} : _cart$receiverAddress,
119
- receiverNotes = cart.receiverNotes;
118
+ var saved = (0, _local_storage.getSavedCheckoutAddress)(domainId);
119
+ var cartAddress = cart === null || cart === void 0 ? void 0 : cart.receiverAddress;
120
+ var receiverAddress = (0, _local_storage.addressHasContent)(cartAddress) ? cartAddress : saved.address || {};
121
+ var receiverNotes = (cart === null || cart === void 0 ? void 0 : cart.receiverNotes) || saved.receiverNotes || "";
120
122
  var _useState = (0, _react.useState)(false),
121
123
  _useState2 = _slicedToArray(_useState, 2),
122
124
  addressFieldsOpen = _useState2[0],
@@ -146,27 +148,28 @@ function ActiveFormShipmentAddressAndNotes() {
146
148
  embed: _helpers.cartEmbed
147
149
  });
148
150
  case 12:
151
+ (0, _local_storage.saveCheckoutAddress)(domainId, address, receiverNotes);
149
152
  setCart(cartEnt.toJson()); // Update cart context
150
153
  setActiveTabAndEditDisabled({
151
154
  tabId: _tabs.tabIdCheckout,
152
155
  tabIndexToSet: 2,
153
156
  disabled: false
154
157
  });
155
- _context.next = 19;
158
+ _context.next = 20;
156
159
  break;
157
- case 16:
158
- _context.prev = 16;
160
+ case 17:
161
+ _context.prev = 17;
159
162
  _context.t0 = _context["catch"](9);
160
163
  alertError(_context.t0.errorMessage || _context.t0.message || "Unable to set shipment address.");
161
- case 19:
162
- _context.prev = 19;
164
+ case 20:
165
+ _context.prev = 20;
163
166
  setLoadingTotals(false);
164
- return _context.finish(19);
165
- case 22:
167
+ return _context.finish(20);
168
+ case 23:
166
169
  case "end":
167
170
  return _context.stop();
168
171
  }
169
- }, _callee, null, [[9, 16, 19, 22]]);
172
+ }, _callee, null, [[9, 17, 20, 23]]);
170
173
  }));
171
174
  return _saveCartShipmentAddressAndGoToNextTab.apply(this, arguments);
172
175
  }
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.loadFormBundle = loadFormBundle;
7
+ exports.publishedFormBundle = publishedFormBundle;
8
+ /**
9
+ * Helpers for loading a product's published custom form bundle in cart edit.
10
+ * Mirrors the product-embed loader: SRI script tag → window.MERCHI_PRODUCT_FORM_<id>.
11
+ */
12
+
13
+ /** If the product has a published custom form with a compiled bundle, return
14
+ * the info needed to load it; otherwise null (caller falls back). */
15
+ function publishedFormBundle(product) {
16
+ var form = product === null || product === void 0 ? void 0 : product.productForm;
17
+ if (!form || form.status !== 'published') return null;
18
+ var version = form.activeVersion;
19
+ if (!version || !version.compiledBundleUrl) return null;
20
+ return {
21
+ url: version.compiledBundleUrl,
22
+ integrity: version.bundleSha || undefined,
23
+ globalName: "MERCHI_PRODUCT_FORM_".concat(form.id)
24
+ };
25
+ }
26
+
27
+ /** Dynamically load a compiled form bundle and resolve its default export. */
28
+ function loadFormBundle(bundle) {
29
+ var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
30
+ var url = bundle.url,
31
+ integrity = bundle.integrity,
32
+ globalName = bundle.globalName;
33
+ var timeoutMs = opts.timeoutMs || 15000;
34
+ var w = window;
35
+ var existing = w[globalName] && w[globalName]["default"];
36
+ if (existing) return Promise.resolve(existing);
37
+ return new Promise(function (resolve, reject) {
38
+ var settled = false;
39
+ var finish = function finish(fn, arg) {
40
+ if (settled) return;
41
+ settled = true;
42
+ clearTimeout(timer);
43
+ fn(arg);
44
+ };
45
+ var timer = setTimeout(function () {
46
+ return finish(reject, new Error("form bundle ".concat(globalName, " timed out")));
47
+ }, timeoutMs);
48
+ var script = document.createElement('script');
49
+ script.src = url;
50
+ if (integrity) script.integrity = integrity;
51
+ script.crossOrigin = 'anonymous';
52
+ script.async = true;
53
+ script.onload = function () {
54
+ var mod = w[globalName];
55
+ if (mod && mod["default"]) finish(resolve, mod["default"]);else {
56
+ finish(reject, new Error("form bundle ".concat(globalName, " did not register a default export")));
57
+ }
58
+ };
59
+ script.onerror = function () {
60
+ return finish(reject, new Error("failed to load form bundle ".concat(globalName)));
61
+ };
62
+ document.head.appendChild(script);
63
+ });
64
+ }
@@ -91,6 +91,17 @@ Object.keys(_job).forEach(function (key) {
91
91
  }
92
92
  });
93
93
  });
94
+ var _local_storage = require("./local_storage");
95
+ Object.keys(_local_storage).forEach(function (key) {
96
+ if (key === "default" || key === "__esModule") return;
97
+ if (key in exports && exports[key] === _local_storage[key]) return;
98
+ Object.defineProperty(exports, key, {
99
+ enumerable: true,
100
+ get: function get() {
101
+ return _local_storage[key];
102
+ }
103
+ });
104
+ });
94
105
  var _product = require("./product");
95
106
  Object.keys(_product).forEach(function (key) {
96
107
  if (key === "default" || key === "__esModule") return;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.addressHasContent = addressHasContent;
7
+ exports.getSavedCheckoutAddress = getSavedCheckoutAddress;
8
+ exports.getSavedCheckoutCustomer = getSavedCheckoutCustomer;
9
+ exports.getSavedCheckoutDraft = getSavedCheckoutDraft;
10
+ exports.saveCheckoutAddress = saveCheckoutAddress;
11
+ exports.saveCheckoutCustomer = saveCheckoutCustomer;
12
+ var _browserOrNode = require("browser-or-node");
13
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
14
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
15
+ function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
16
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
17
+ function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
18
+ function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
19
+ function storageKey(domainId) {
20
+ return "merchi-cart-checkout-".concat(domainId);
21
+ }
22
+ function readDraft(domainId) {
23
+ if (!_browserOrNode.isBrowser || domainId === undefined || domainId === null) {
24
+ return {};
25
+ }
26
+ try {
27
+ var raw = localStorage.getItem(storageKey(domainId));
28
+ if (!raw) return {};
29
+ var parsed = JSON.parse(raw);
30
+ return parsed && _typeof(parsed) === 'object' ? parsed : {};
31
+ } catch (_unused) {
32
+ return {};
33
+ }
34
+ }
35
+ function writeDraft(domainId, draft) {
36
+ if (!_browserOrNode.isBrowser || domainId === undefined || domainId === null) {
37
+ return;
38
+ }
39
+ try {
40
+ localStorage.setItem(storageKey(domainId), JSON.stringify(draft));
41
+ } catch (_unused2) {
42
+ // Ignore quota / private-mode failures.
43
+ }
44
+ }
45
+ function getSavedCheckoutDraft(domainId) {
46
+ return readDraft(domainId);
47
+ }
48
+ function getSavedCheckoutCustomer(domainId) {
49
+ return readDraft(domainId).customer;
50
+ }
51
+ function getSavedCheckoutAddress(domainId) {
52
+ var draft = readDraft(domainId);
53
+ return {
54
+ address: draft.address,
55
+ receiverNotes: draft.receiverNotes
56
+ };
57
+ }
58
+ function saveCheckoutCustomer(domainId, customer) {
59
+ var draft = readDraft(domainId);
60
+ var nextCustomer = _objectSpread(_objectSpread({}, draft.customer), customer);
61
+ // Drop empty phone entries.
62
+ if (nextCustomer.phoneNumbers) {
63
+ nextCustomer.phoneNumbers = nextCustomer.phoneNumbers.filter(function (phone) {
64
+ return phone && (phone.number || phone.code);
65
+ });
66
+ if (!nextCustomer.phoneNumbers.length) {
67
+ delete nextCustomer.phoneNumbers;
68
+ }
69
+ }
70
+ writeDraft(domainId, _objectSpread(_objectSpread({}, draft), {}, {
71
+ customer: nextCustomer
72
+ }));
73
+ }
74
+ function saveCheckoutAddress(domainId, address, receiverNotes) {
75
+ if (!address) return;
76
+ var draft = readDraft(domainId);
77
+ var nextAddress = {
78
+ city: address.city || '',
79
+ country: address.country || '',
80
+ lineOne: address.lineOne || '',
81
+ lineTwo: address.lineTwo || '',
82
+ postcode: address.postcode || '',
83
+ state: address.state || ''
84
+ };
85
+ var next = _objectSpread(_objectSpread({}, draft), {}, {
86
+ address: nextAddress
87
+ });
88
+ if (receiverNotes !== undefined) {
89
+ next.receiverNotes = receiverNotes;
90
+ }
91
+ writeDraft(domainId, next);
92
+ }
93
+ function addressHasContent(address) {
94
+ if (!address) return false;
95
+ return Boolean(address.lineOne || address.lineTwo || address.city || address.state || address.postcode || address.country);
96
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "merchi_cart",
3
- "version": "1.3.2",
3
+ "version": "1.3.4",
4
4
  "description": "Merchi's cart",
5
5
  "source": "src/index.ts",
6
6
  "main": "lib/index.js",