merchi_cart 1.3.3 → 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.
@@ -25,10 +25,12 @@ import InputError from './InputError';
25
25
  import { createNewCustomer, makeUser } from '../utilities/user';
26
26
  import { makeCart } from '../utilities/cart';
27
27
  import { getCartCookieToken } from '../utilities/cookie';
28
+ import { getSavedCheckoutCustomer, saveCheckoutCustomer } from '../utilities/local_storage';
28
29
  import { jsx as _jsx } from "react/jsx-runtime";
29
30
  import { jsxs as _jsxs } from "react/jsx-runtime";
30
31
  export var emailValidation = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]/i;
31
32
  export function FormCustomerNew() {
33
+ var _savedCustomer$phoneN, _savedCustomer$phoneN2;
32
34
  var _useCartContext = useCartContext(),
33
35
  cart = _useCartContext.cart,
34
36
  classNameBtnPrimary = _useCartContext.classNameBtnPrimary,
@@ -50,12 +52,13 @@ export function FormCustomerNew() {
50
52
  _React$useState6 = _slicedToArray(_React$useState5, 2),
51
53
  acceptConditions = _React$useState6[0],
52
54
  setAcceptConditions = _React$useState6[1];
55
+ var savedCustomer = getSavedCheckoutCustomer(domainId);
53
56
  function actionCreateNewCustomer(_x) {
54
57
  return _actionCreateNewCustomer.apply(this, arguments);
55
58
  }
56
59
  function _actionCreateNewCustomer() {
57
60
  _actionCreateNewCustomer = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(customerJson) {
58
- var _cartJson$client, r, user, cartToken, clientEnt, cartEnt, _cart, cartJson;
61
+ var _cartJson$client, _customerJson$emailAd, r, user, cartToken, clientEnt, cartEnt, _cart, cartJson;
59
62
  return _regeneratorRuntime().wrap(function _callee$(_context) {
60
63
  while (1) switch (_context.prev = _context.next) {
61
64
  case 0:
@@ -99,37 +102,43 @@ export function FormCustomerNew() {
99
102
  }
100
103
  throw new Error('Unable to attach client to cart.');
101
104
  case 21:
105
+ saveCheckoutCustomer(domainId, {
106
+ id: user.id,
107
+ name: customerJson.name,
108
+ 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,
109
+ phoneNumbers: customerJson.phoneNumbers
110
+ });
102
111
  setCartClient(cartJson.client);
103
112
  setCart(cartJson);
104
- _context.next = 28;
113
+ _context.next = 29;
105
114
  break;
106
- case 25:
107
- _context.prev = 25;
115
+ case 26:
116
+ _context.prev = 26;
108
117
  _context.t0 = _context["catch"](2);
109
118
  setError({
110
119
  message: _context.t0.errorMessage || _context.t0.message || 'Unable to attach client to cart.'
111
120
  });
112
- case 28:
113
- _context.prev = 28;
121
+ case 29:
122
+ _context.prev = 29;
114
123
  setLoading(false);
115
- return _context.finish(28);
116
- case 31:
124
+ return _context.finish(29);
125
+ case 32:
117
126
  case "end":
118
127
  return _context.stop();
119
128
  }
120
- }, _callee, null, [[2, 25, 28, 31]]);
129
+ }, _callee, null, [[2, 26, 29, 32]]);
121
130
  }));
122
131
  return _actionCreateNewCustomer.apply(this, arguments);
123
132
  }
124
133
  var _useForm = useForm({
125
134
  defaultValues: {
126
135
  emailAddresses: [{
127
- emailAddress: ''
136
+ emailAddress: (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.emailAddress) || ''
128
137
  }],
129
- name: '',
138
+ name: (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.name) || '',
130
139
  phoneNumbers: [{
131
- code: 'AU',
132
- number: ''
140
+ 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',
141
+ 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) || ''
133
142
  }],
134
143
  registeredAsGuest: false
135
144
  }
@@ -22,6 +22,7 @@ import { makeUser, tryReturningCustomerEmail } from '../utilities/user';
22
22
  import { makeCart } from '../utilities/cart';
23
23
  import { getCartCookieToken } from '../utilities/cookie';
24
24
  import { cartEmbed } from '../utilities/helpers';
25
+ import { getSavedCheckoutCustomer, saveCheckoutCustomer } from '../utilities/local_storage';
25
26
  import { jsx as _jsx } from "react/jsx-runtime";
26
27
  import { jsxs as _jsxs } from "react/jsx-runtime";
27
28
  var emailValidation = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]/i;
@@ -44,9 +45,10 @@ function FormReturningCustomer() {
44
45
  _React$useState4 = _slicedToArray(_React$useState3, 2),
45
46
  error = _React$useState4[0],
46
47
  setError = _React$useState4[1];
48
+ var savedCustomer = getSavedCheckoutCustomer(domainId);
47
49
  var hookForm = useForm({
48
50
  defaultValues: {
49
- emailAddress: ''
51
+ emailAddress: (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.emailAddress) || ''
50
52
  }
51
53
  });
52
54
  var handleSubmit = hookForm.handleSubmit,
@@ -58,7 +60,7 @@ function FormReturningCustomer() {
58
60
  }
59
61
  function _submit() {
60
62
  _submit = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
61
- var _getValues, emailAddress, _user$user_id, _cartJson$client, user, userId, userJson, cartToken, clientEnt, cartEnt, _cart, cartJson;
63
+ var _getValues, emailAddress, _user$user_id, _cartJson$client, user, userId, cartToken, clientEnt, cartEnt, _cart, cartJson;
62
64
  return _regeneratorRuntime().wrap(function _callee$(_context) {
63
65
  while (1) switch (_context.prev = _context.next) {
64
66
  case 0:
@@ -78,33 +80,33 @@ function FormReturningCustomer() {
78
80
  }
79
81
  throw new Error('Unable to find returning customer.');
80
82
  case 10:
81
- userJson = {
82
- id: userId,
83
- emailAddresses: [{
84
- emailAddress: emailAddress
85
- }],
86
- name: user.name || 'Hidden for privacy'
87
- }; // patch cart with returning user
88
- _context.next = 13;
83
+ _context.next = 12;
89
84
  return getCartCookieToken(domainId);
90
- case 13:
85
+ case 12:
91
86
  cartToken = _context.sent;
92
- clientEnt = makeUser(userJson, true);
87
+ clientEnt = makeUser({
88
+ id: userId
89
+ }, true);
93
90
  cartEnt = makeCart(_objectSpread({}, cart), false, cartToken);
94
91
  cartEnt.client = clientEnt;
95
- _context.next = 19;
92
+ _context.next = 18;
96
93
  return cartEnt.save({
97
94
  embed: cartEmbed
98
95
  });
99
- case 19:
96
+ case 18:
100
97
  _cart = _context.sent;
101
98
  cartJson = _cart.toJson();
102
99
  if (cartJson !== null && cartJson !== void 0 && (_cartJson$client = cartJson.client) !== null && _cartJson$client !== void 0 && _cartJson$client.id) {
103
- _context.next = 23;
100
+ _context.next = 22;
104
101
  break;
105
102
  }
106
103
  throw new Error('Unable to attach client to cart.');
107
- case 23:
104
+ case 22:
105
+ saveCheckoutCustomer(domainId, {
106
+ id: userId,
107
+ name: user.name || (savedCustomer === null || savedCustomer === void 0 ? void 0 : savedCustomer.name),
108
+ emailAddress: emailAddress
109
+ });
108
110
  setCartClient(cartJson.client);
109
111
  setCart(cartJson);
110
112
  _context.next = 31;
@@ -26,6 +26,7 @@ import { makeAddress } from "../utilities/address";
26
26
  import { makeCart } from "../utilities/cart";
27
27
  import { cartEmbed } from "../utilities/helpers";
28
28
  import { tabIdCheckout } from "../utilities/tabs";
29
+ import { addressHasContent, getSavedCheckoutAddress, saveCheckoutAddress } from "../utilities/local_storage";
29
30
  import { jsx as _jsx } from "react/jsx-runtime";
30
31
  import { jsxs as _jsxs } from "react/jsx-runtime";
31
32
  var AddressGeoSuggestInput = function AddressGeoSuggestInput(props) {
@@ -107,9 +108,10 @@ export function ActiveFormShipmentAddressAndNotes() {
107
108
  setCart = _useCartContext2.setCart,
108
109
  setLoadingTotals = _useCartContext2.setLoadingTotals,
109
110
  setActiveTabAndEditDisabled = _useCartContext2.setActiveTabAndEditDisabled;
110
- var _cart$receiverAddress = cart.receiverAddress,
111
- receiverAddress = _cart$receiverAddress === void 0 ? {} : _cart$receiverAddress,
112
- receiverNotes = cart.receiverNotes;
111
+ var saved = getSavedCheckoutAddress(domainId);
112
+ var cartAddress = cart === null || cart === void 0 ? void 0 : cart.receiverAddress;
113
+ var receiverAddress = addressHasContent(cartAddress) ? cartAddress : saved.address || {};
114
+ var receiverNotes = (cart === null || cart === void 0 ? void 0 : cart.receiverNotes) || saved.receiverNotes || "";
113
115
  var _useState = useState(false),
114
116
  _useState2 = _slicedToArray(_useState, 2),
115
117
  addressFieldsOpen = _useState2[0],
@@ -139,27 +141,28 @@ export function ActiveFormShipmentAddressAndNotes() {
139
141
  embed: cartEmbed
140
142
  });
141
143
  case 12:
144
+ saveCheckoutAddress(domainId, address, receiverNotes);
142
145
  setCart(cartEnt.toJson()); // Update cart context
143
146
  setActiveTabAndEditDisabled({
144
147
  tabId: tabIdCheckout,
145
148
  tabIndexToSet: 2,
146
149
  disabled: false
147
150
  });
148
- _context.next = 19;
151
+ _context.next = 20;
149
152
  break;
150
- case 16:
151
- _context.prev = 16;
153
+ case 17:
154
+ _context.prev = 17;
152
155
  _context.t0 = _context["catch"](9);
153
156
  alertError(_context.t0.errorMessage || _context.t0.message || "Unable to set shipment address.");
154
- case 19:
155
- _context.prev = 19;
157
+ case 20:
158
+ _context.prev = 20;
156
159
  setLoadingTotals(false);
157
- return _context.finish(19);
158
- case 22:
160
+ return _context.finish(20);
161
+ case 23:
159
162
  case "end":
160
163
  return _context.stop();
161
164
  }
162
- }, _callee, null, [[9, 16, 19, 22]]);
165
+ }, _callee, null, [[9, 17, 20, 23]]);
163
166
  }));
164
167
  return _saveCartShipmentAddressAndGoToNextTab.apply(this, arguments);
165
168
  }
@@ -23,8 +23,6 @@ import { LoadingTemplateSm } from '../components/LoadingTemplate';
23
23
  import { useCartContext } from '../CartProvider';
24
24
  import { makeCartItem } from '../utilities/cart';
25
25
  import { getCartCookieToken } from '../utilities/cookie';
26
- import { publishedFormBundle } from '../utilities/customForm';
27
- import CustomCartProductForm from '../components/CustomCartProductForm';
28
26
  import { jsx as _jsx } from "react/jsx-runtime";
29
27
  import { jsxs as _jsxs } from "react/jsx-runtime";
30
28
  var MerchiProductForm = /*#__PURE__*/lazy(function () {
@@ -78,10 +76,6 @@ function PanelEditCartItem(_ref) {
78
76
  _useState2 = _slicedToArray(_useState, 2),
79
77
  loading = _useState2[0],
80
78
  setLoading = _useState2[1];
81
- var _useState3 = useState(false),
82
- _useState4 = _slicedToArray(_useState3, 2),
83
- customFormActive = _useState4[0],
84
- setCustomFormActive = _useState4[1];
85
79
  var formId = 'edit-cart-item-form';
86
80
 
87
81
  // This action patches the cart item
@@ -155,36 +149,27 @@ function PanelEditCartItem(_ref) {
155
149
  id: undefined
156
150
  });
157
151
  var showForm = activeTabIndex === tabIdItem && !!(cartItem !== null && cartItem !== void 0 && cartItem.id) && !!(cartItem !== null && cartItem !== void 0 && (_cartItem$product = cartItem.product) !== null && _cartItem$product !== void 0 && _cartItem$product.id);
158
- var hasPublishedCustomForm = !!publishedFormBundle(cartItem === null || cartItem === void 0 ? void 0 : cartItem.product);
159
- var defaultForm = /*#__PURE__*/_jsx(Suspense, {
160
- fallback: /*#__PURE__*/_jsx(LoadingTemplateSm, {}),
161
- children: /*#__PURE__*/_jsx(MerchiProductForm, _objectSpread({
162
- apiUrl: apiUrl,
163
- isCartItem: true,
164
- initJob: _objectSpread({}, cleanCartItem),
165
- initProduct: cartItem.product,
166
- onSubmit: onSubmit,
167
- productFormId: formId,
168
- hideRequestQuotationButton: true,
169
- hidePaymentUpfrontButton: true
170
- }, productFormClassNames))
171
- });
172
152
  return /*#__PURE__*/_jsxs(CartTabPanel, {
173
153
  tabId: tabIdItem,
174
154
  children: [/*#__PURE__*/_jsx(CartBody, {
175
155
  style: {
176
156
  padding: '2rem'
177
157
  },
178
- children: showForm && (hasPublishedCustomForm ? /*#__PURE__*/_jsx(CustomCartProductForm, {
179
- product: cartItem.product,
180
- initialJob: cleanCartItem,
181
- apiUrl: apiUrl,
182
- onSave: onSubmit,
183
- fallback: defaultForm,
184
- onActiveChange: setCustomFormActive
185
- }) : defaultForm)
158
+ children: showForm && /*#__PURE__*/_jsx(Suspense, {
159
+ fallback: /*#__PURE__*/_jsx(LoadingTemplateSm, {}),
160
+ children: /*#__PURE__*/_jsx(MerchiProductForm, _objectSpread({
161
+ apiUrl: apiUrl,
162
+ isCartItem: true,
163
+ initJob: _objectSpread({}, cleanCartItem),
164
+ initProduct: cartItem.product,
165
+ onSubmit: onSubmit,
166
+ productFormId: formId,
167
+ hideRequestQuotationButton: true,
168
+ hidePaymentUpfrontButton: true
169
+ }, productFormClassNames))
170
+ })
186
171
  }), /*#__PURE__*/_jsxs(CartFooter, {
187
- children: [/*#__PURE__*/_jsx(ButtonBack, {}), !customFormActive && /*#__PURE__*/_jsxs(Button, {
172
+ children: [/*#__PURE__*/_jsx(ButtonBack, {}), /*#__PURE__*/_jsxs(Button, {
188
173
  className: classNameBtnEditCartItem,
189
174
  disabled: loading,
190
175
  form: formId,
@@ -184,10 +184,6 @@ var productWithImagesEmbed = {
184
184
  linkedFile: {}
185
185
  }
186
186
  },
187
- // Published custom form pointer for cart-item edit (same shape as product embed).
188
- productForm: {
189
- activeVersion: {}
190
- },
191
187
  taxType: {}
192
188
  };
193
189
  var cartShipmentQuote = {
@@ -6,6 +6,7 @@ export * from './currency';
6
6
  export * from './helpers';
7
7
  export * from './invoice';
8
8
  export * from './job';
9
+ export * from './local_storage';
9
10
  export * from './product';
10
11
  export * from './shipment';
11
12
  export * from './tabs';
@@ -0,0 +1,85 @@
1
+ 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; }
2
+ 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; }
3
+ 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; }
4
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
5
+ 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); }
6
+ 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); }
7
+ import { isBrowser } from 'browser-or-node';
8
+ function storageKey(domainId) {
9
+ return "merchi-cart-checkout-".concat(domainId);
10
+ }
11
+ function readDraft(domainId) {
12
+ if (!isBrowser || domainId === undefined || domainId === null) {
13
+ return {};
14
+ }
15
+ try {
16
+ var raw = localStorage.getItem(storageKey(domainId));
17
+ if (!raw) return {};
18
+ var parsed = JSON.parse(raw);
19
+ return parsed && _typeof(parsed) === 'object' ? parsed : {};
20
+ } catch (_unused) {
21
+ return {};
22
+ }
23
+ }
24
+ function writeDraft(domainId, draft) {
25
+ if (!isBrowser || domainId === undefined || domainId === null) {
26
+ return;
27
+ }
28
+ try {
29
+ localStorage.setItem(storageKey(domainId), JSON.stringify(draft));
30
+ } catch (_unused2) {
31
+ // Ignore quota / private-mode failures.
32
+ }
33
+ }
34
+ export function getSavedCheckoutDraft(domainId) {
35
+ return readDraft(domainId);
36
+ }
37
+ export function getSavedCheckoutCustomer(domainId) {
38
+ return readDraft(domainId).customer;
39
+ }
40
+ export function getSavedCheckoutAddress(domainId) {
41
+ var draft = readDraft(domainId);
42
+ return {
43
+ address: draft.address,
44
+ receiverNotes: draft.receiverNotes
45
+ };
46
+ }
47
+ export function saveCheckoutCustomer(domainId, customer) {
48
+ var draft = readDraft(domainId);
49
+ var nextCustomer = _objectSpread(_objectSpread({}, draft.customer), customer);
50
+ // Drop empty phone entries.
51
+ if (nextCustomer.phoneNumbers) {
52
+ nextCustomer.phoneNumbers = nextCustomer.phoneNumbers.filter(function (phone) {
53
+ return phone && (phone.number || phone.code);
54
+ });
55
+ if (!nextCustomer.phoneNumbers.length) {
56
+ delete nextCustomer.phoneNumbers;
57
+ }
58
+ }
59
+ writeDraft(domainId, _objectSpread(_objectSpread({}, draft), {}, {
60
+ customer: nextCustomer
61
+ }));
62
+ }
63
+ export function saveCheckoutAddress(domainId, address, receiverNotes) {
64
+ if (!address) return;
65
+ var draft = readDraft(domainId);
66
+ var nextAddress = {
67
+ city: address.city || '',
68
+ country: address.country || '',
69
+ lineOne: address.lineOne || '',
70
+ lineTwo: address.lineTwo || '',
71
+ postcode: address.postcode || '',
72
+ state: address.state || ''
73
+ };
74
+ var next = _objectSpread(_objectSpread({}, draft), {}, {
75
+ address: nextAddress
76
+ });
77
+ if (receiverNotes !== undefined) {
78
+ next.receiverNotes = receiverNotes;
79
+ }
80
+ writeDraft(domainId, next);
81
+ }
82
+ export function addressHasContent(address) {
83
+ if (!address) return false;
84
+ return Boolean(address.lineOne || address.lineTwo || address.city || address.state || address.postcode || address.country);
85
+ }