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.
- package/esm/CartProvider.js +239 -154
- package/esm/components/CustomCartProductForm.js +129 -0
- package/esm/components/product-form-sdk-externals.js +35 -0
- package/esm/forms/FormNewCustomer.js +22 -13
- package/esm/forms/FormReturningCustomer.js +18 -16
- package/esm/forms/FormShipmentAddressAndNotes.js +14 -11
- package/esm/utilities/customForm.js +57 -0
- package/esm/utilities/index.js +1 -0
- package/esm/utilities/local_storage.js +85 -0
- package/lib/CartProvider.js +239 -154
- package/lib/components/CustomCartProductForm.js +137 -0
- package/lib/components/product-form-sdk-externals.js +42 -0
- package/lib/forms/FormNewCustomer.js +22 -13
- package/lib/forms/FormReturningCustomer.js +18 -16
- package/lib/forms/FormShipmentAddressAndNotes.js +14 -11
- package/lib/utilities/customForm.js +64 -0
- package/lib/utilities/index.js +11 -0
- package/lib/utilities/local_storage.js +96 -0
- package/package.json +1 -1
- package/src/CartProvider.tsx +39 -3
- package/src/forms/FormNewCustomer.tsx +19 -3
- package/src/forms/FormReturningCustomer.tsx +15 -8
- package/src/forms/FormShipmentAddressAndNotes.tsx +12 -1
- package/src/utilities/index.ts +1 -0
- package/src/utilities/local_storage.ts +130 -0
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
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); }
|
|
2
|
+
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; }
|
|
3
|
+
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; }
|
|
4
|
+
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; }
|
|
5
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
6
|
+
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); }
|
|
7
|
+
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
|
8
|
+
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."); }
|
|
9
|
+
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); }
|
|
10
|
+
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; }
|
|
11
|
+
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; } }
|
|
12
|
+
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
13
|
+
import React, { useEffect, useMemo, useRef, useState } from 'react';
|
|
14
|
+
import { MerchiProductFormSdkExternals } from './product-form-sdk-externals';
|
|
15
|
+
import { loadFormBundle, publishedFormBundle } from '../utilities/customForm';
|
|
16
|
+
import { LoadingTemplateSm } from './LoadingTemplate';
|
|
17
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
18
|
+
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
19
|
+
/**
|
|
20
|
+
* Loads a product's published custom form for cart-item edit. On any failure
|
|
21
|
+
* renders `fallback` (the default merchi_product_form) so the cart stays usable.
|
|
22
|
+
*/
|
|
23
|
+
export function CustomCartProductForm(_ref) {
|
|
24
|
+
var _publishedFormBundle$, _publishedFormBundle;
|
|
25
|
+
var product = _ref.product,
|
|
26
|
+
initialJob = _ref.initialJob,
|
|
27
|
+
apiUrl = _ref.apiUrl,
|
|
28
|
+
onSave = _ref.onSave,
|
|
29
|
+
fallback = _ref.fallback,
|
|
30
|
+
onActiveChange = _ref.onActiveChange;
|
|
31
|
+
var _useState = useState(null),
|
|
32
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
33
|
+
Loaded = _useState2[0],
|
|
34
|
+
setLoaded = _useState2[1];
|
|
35
|
+
var _useState3 = useState(false),
|
|
36
|
+
_useState4 = _slicedToArray(_useState3, 2),
|
|
37
|
+
failed = _useState4[0],
|
|
38
|
+
setFailed = _useState4[1];
|
|
39
|
+
var _useState5 = useState(true),
|
|
40
|
+
_useState6 = _slicedToArray(_useState5, 2),
|
|
41
|
+
loading = _useState6[0],
|
|
42
|
+
setLoading = _useState6[1];
|
|
43
|
+
var onSaveRef = useRef(onSave);
|
|
44
|
+
onSaveRef.current = onSave;
|
|
45
|
+
var onActiveChangeRef = useRef(onActiveChange);
|
|
46
|
+
onActiveChangeRef.current = onActiveChange;
|
|
47
|
+
var bundleKey = (_publishedFormBundle$ = (_publishedFormBundle = publishedFormBundle(product)) === null || _publishedFormBundle === void 0 ? void 0 : _publishedFormBundle.url) !== null && _publishedFormBundle$ !== void 0 ? _publishedFormBundle$ : '';
|
|
48
|
+
useEffect(function () {
|
|
49
|
+
var active = true;
|
|
50
|
+
setLoaded(null);
|
|
51
|
+
setFailed(false);
|
|
52
|
+
setLoading(true);
|
|
53
|
+
var bundle = publishedFormBundle(product);
|
|
54
|
+
if (!bundle) {
|
|
55
|
+
setFailed(true);
|
|
56
|
+
setLoading(false);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
var w = window;
|
|
60
|
+
w.React = React;
|
|
61
|
+
w.MerchiProductFormSdk = MerchiProductFormSdkExternals;
|
|
62
|
+
loadFormBundle(bundle).then(function (Component) {
|
|
63
|
+
if (active) {
|
|
64
|
+
setLoaded(function () {
|
|
65
|
+
return Component;
|
|
66
|
+
});
|
|
67
|
+
setLoading(false);
|
|
68
|
+
}
|
|
69
|
+
})["catch"](function (e) {
|
|
70
|
+
console.error('[merchi_cart] custom form load failed', e);
|
|
71
|
+
if (active) {
|
|
72
|
+
setFailed(true);
|
|
73
|
+
setLoading(false);
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
return function () {
|
|
77
|
+
active = false;
|
|
78
|
+
};
|
|
79
|
+
}, [product, bundleKey]);
|
|
80
|
+
var props = useMemo(function () {
|
|
81
|
+
try {
|
|
82
|
+
var runtime = MerchiProductFormSdkExternals.createProductFormRuntime({
|
|
83
|
+
apiUrl: apiUrl || '',
|
|
84
|
+
product: product,
|
|
85
|
+
onAddToCart: function onAddToCart(job) {
|
|
86
|
+
var _onSaveRef$current;
|
|
87
|
+
return (_onSaveRef$current = onSaveRef.current) === null || _onSaveRef$current === void 0 ? void 0 : _onSaveRef$current.call(onSaveRef, job);
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
return {
|
|
91
|
+
product: product,
|
|
92
|
+
pricing: runtime.pricing,
|
|
93
|
+
actions: runtime.actions,
|
|
94
|
+
helpers: runtime.helpers
|
|
95
|
+
};
|
|
96
|
+
} catch (e) {
|
|
97
|
+
console.error('[merchi_cart] custom form props build failed', e);
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
}, [product, apiUrl]);
|
|
101
|
+
var isActive = !failed && !loading && !!Loaded && !!props;
|
|
102
|
+
useEffect(function () {
|
|
103
|
+
var _onActiveChangeRef$cu;
|
|
104
|
+
(_onActiveChangeRef$cu = onActiveChangeRef.current) === null || _onActiveChangeRef$cu === void 0 || _onActiveChangeRef$cu.call(onActiveChangeRef, isActive);
|
|
105
|
+
return function () {
|
|
106
|
+
var _onActiveChangeRef$cu2;
|
|
107
|
+
return (_onActiveChangeRef$cu2 = onActiveChangeRef.current) === null || _onActiveChangeRef$cu2 === void 0 ? void 0 : _onActiveChangeRef$cu2.call(onActiveChangeRef, false);
|
|
108
|
+
};
|
|
109
|
+
}, [isActive]);
|
|
110
|
+
if (loading) return /*#__PURE__*/_jsx(LoadingTemplateSm, {});
|
|
111
|
+
if (failed || !Loaded || !props) return /*#__PURE__*/_jsx(_Fragment, {
|
|
112
|
+
children: fallback
|
|
113
|
+
});
|
|
114
|
+
var Shell = MerchiProductFormSdkExternals.ProductFormShell;
|
|
115
|
+
return /*#__PURE__*/_jsx(_Fragment, {
|
|
116
|
+
children: Shell ? /*#__PURE__*/_jsx(Shell, {
|
|
117
|
+
product: props.product,
|
|
118
|
+
pricing: props.pricing,
|
|
119
|
+
actions: props.actions,
|
|
120
|
+
helpers: props.helpers,
|
|
121
|
+
initialJob: initialJob,
|
|
122
|
+
actionLabels: {
|
|
123
|
+
addToCart: 'Save'
|
|
124
|
+
},
|
|
125
|
+
children: /*#__PURE__*/_jsx(Loaded, _objectSpread({}, props))
|
|
126
|
+
}) : /*#__PURE__*/_jsx(Loaded, _objectSpread({}, props))
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
export default CustomCartProductForm;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Full merchi_sdk_product_form runtime for window.MerchiProductFormSdk.
|
|
3
|
+
* Keep in sync with merchi_api/common/js/product_form_gate.cjs SDK_EXPORTS.
|
|
4
|
+
*/
|
|
5
|
+
import { SDK_VERSION, serializeJob, nonEmptyGroups, formatCurrency, urlFor, createPricing, createProductFormRuntime, productHasGroups, groupFieldsOf, independentFieldsOf, isOptionQuantityGridProduct, buildProductFormJob, jobToFormState, selectionsToVariations, Section, Stack, Card, Heading, Text, Divider, Field, GroupRows, OptionQuantityGrid, ProductFormProvider, ProductFormShell, useProductForm, OPTION_FIELD_TYPES, theme } from 'merchi_sdk_product_form';
|
|
6
|
+
export var MerchiProductFormSdkExternals = {
|
|
7
|
+
SDK_VERSION: SDK_VERSION,
|
|
8
|
+
serializeJob: serializeJob,
|
|
9
|
+
nonEmptyGroups: nonEmptyGroups,
|
|
10
|
+
formatCurrency: formatCurrency,
|
|
11
|
+
urlFor: urlFor,
|
|
12
|
+
createPricing: createPricing,
|
|
13
|
+
createProductFormRuntime: createProductFormRuntime,
|
|
14
|
+
productHasGroups: productHasGroups,
|
|
15
|
+
groupFieldsOf: groupFieldsOf,
|
|
16
|
+
independentFieldsOf: independentFieldsOf,
|
|
17
|
+
isOptionQuantityGridProduct: isOptionQuantityGridProduct,
|
|
18
|
+
buildProductFormJob: buildProductFormJob,
|
|
19
|
+
jobToFormState: jobToFormState,
|
|
20
|
+
selectionsToVariations: selectionsToVariations,
|
|
21
|
+
Section: Section,
|
|
22
|
+
Stack: Stack,
|
|
23
|
+
Card: Card,
|
|
24
|
+
Heading: Heading,
|
|
25
|
+
Text: Text,
|
|
26
|
+
Divider: Divider,
|
|
27
|
+
Field: Field,
|
|
28
|
+
GroupRows: GroupRows,
|
|
29
|
+
OptionQuantityGrid: OptionQuantityGrid,
|
|
30
|
+
ProductFormProvider: ProductFormProvider,
|
|
31
|
+
ProductFormShell: ProductFormShell,
|
|
32
|
+
useProductForm: useProductForm,
|
|
33
|
+
OPTION_FIELD_TYPES: OPTION_FIELD_TYPES,
|
|
34
|
+
theme: theme
|
|
35
|
+
};
|
|
@@ -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 =
|
|
113
|
+
_context.next = 29;
|
|
105
114
|
break;
|
|
106
|
-
case
|
|
107
|
-
_context.prev =
|
|
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
|
|
113
|
-
_context.prev =
|
|
121
|
+
case 29:
|
|
122
|
+
_context.prev = 29;
|
|
114
123
|
setLoading(false);
|
|
115
|
-
return _context.finish(
|
|
116
|
-
case
|
|
124
|
+
return _context.finish(29);
|
|
125
|
+
case 32:
|
|
117
126
|
case "end":
|
|
118
127
|
return _context.stop();
|
|
119
128
|
}
|
|
120
|
-
}, _callee, null, [[2,
|
|
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,
|
|
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
|
-
|
|
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
|
|
85
|
+
case 12:
|
|
91
86
|
cartToken = _context.sent;
|
|
92
|
-
clientEnt = makeUser(
|
|
87
|
+
clientEnt = makeUser({
|
|
88
|
+
id: userId
|
|
89
|
+
}, true);
|
|
93
90
|
cartEnt = makeCart(_objectSpread({}, cart), false, cartToken);
|
|
94
91
|
cartEnt.client = clientEnt;
|
|
95
|
-
_context.next =
|
|
92
|
+
_context.next = 18;
|
|
96
93
|
return cartEnt.save({
|
|
97
94
|
embed: cartEmbed
|
|
98
95
|
});
|
|
99
|
-
case
|
|
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 =
|
|
100
|
+
_context.next = 22;
|
|
104
101
|
break;
|
|
105
102
|
}
|
|
106
103
|
throw new Error('Unable to attach client to cart.');
|
|
107
|
-
case
|
|
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
|
|
111
|
-
|
|
112
|
-
|
|
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 =
|
|
151
|
+
_context.next = 20;
|
|
149
152
|
break;
|
|
150
|
-
case
|
|
151
|
-
_context.prev =
|
|
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
|
|
155
|
-
_context.prev =
|
|
157
|
+
case 20:
|
|
158
|
+
_context.prev = 20;
|
|
156
159
|
setLoadingTotals(false);
|
|
157
|
-
return _context.finish(
|
|
158
|
-
case
|
|
160
|
+
return _context.finish(20);
|
|
161
|
+
case 23:
|
|
159
162
|
case "end":
|
|
160
163
|
return _context.stop();
|
|
161
164
|
}
|
|
162
|
-
}, _callee, null, [[9,
|
|
165
|
+
}, _callee, null, [[9, 17, 20, 23]]);
|
|
163
166
|
}));
|
|
164
167
|
return _saveCartShipmentAddressAndGoToNextTab.apply(this, arguments);
|
|
165
168
|
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Helpers for loading a product's published custom form bundle in cart edit.
|
|
3
|
+
* Mirrors the product-embed loader: SRI script tag → window.MERCHI_PRODUCT_FORM_<id>.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** If the product has a published custom form with a compiled bundle, return
|
|
7
|
+
* the info needed to load it; otherwise null (caller falls back). */
|
|
8
|
+
export function publishedFormBundle(product) {
|
|
9
|
+
var form = product === null || product === void 0 ? void 0 : product.productForm;
|
|
10
|
+
if (!form || form.status !== 'published') return null;
|
|
11
|
+
var version = form.activeVersion;
|
|
12
|
+
if (!version || !version.compiledBundleUrl) return null;
|
|
13
|
+
return {
|
|
14
|
+
url: version.compiledBundleUrl,
|
|
15
|
+
integrity: version.bundleSha || undefined,
|
|
16
|
+
globalName: "MERCHI_PRODUCT_FORM_".concat(form.id)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Dynamically load a compiled form bundle and resolve its default export. */
|
|
21
|
+
export function loadFormBundle(bundle) {
|
|
22
|
+
var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
23
|
+
var url = bundle.url,
|
|
24
|
+
integrity = bundle.integrity,
|
|
25
|
+
globalName = bundle.globalName;
|
|
26
|
+
var timeoutMs = opts.timeoutMs || 15000;
|
|
27
|
+
var w = window;
|
|
28
|
+
var existing = w[globalName] && w[globalName]["default"];
|
|
29
|
+
if (existing) return Promise.resolve(existing);
|
|
30
|
+
return new Promise(function (resolve, reject) {
|
|
31
|
+
var settled = false;
|
|
32
|
+
var finish = function finish(fn, arg) {
|
|
33
|
+
if (settled) return;
|
|
34
|
+
settled = true;
|
|
35
|
+
clearTimeout(timer);
|
|
36
|
+
fn(arg);
|
|
37
|
+
};
|
|
38
|
+
var timer = setTimeout(function () {
|
|
39
|
+
return finish(reject, new Error("form bundle ".concat(globalName, " timed out")));
|
|
40
|
+
}, timeoutMs);
|
|
41
|
+
var script = document.createElement('script');
|
|
42
|
+
script.src = url;
|
|
43
|
+
if (integrity) script.integrity = integrity;
|
|
44
|
+
script.crossOrigin = 'anonymous';
|
|
45
|
+
script.async = true;
|
|
46
|
+
script.onload = function () {
|
|
47
|
+
var mod = w[globalName];
|
|
48
|
+
if (mod && mod["default"]) finish(resolve, mod["default"]);else {
|
|
49
|
+
finish(reject, new Error("form bundle ".concat(globalName, " did not register a default export")));
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
script.onerror = function () {
|
|
53
|
+
return finish(reject, new Error("failed to load form bundle ".concat(globalName)));
|
|
54
|
+
};
|
|
55
|
+
document.head.appendChild(script);
|
|
56
|
+
});
|
|
57
|
+
}
|
package/esm/utilities/index.js
CHANGED
|
@@ -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
|
+
}
|