ordering-components-external 13.2.19 → 13.2.20

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.
Files changed (17) hide show
  1. package/_bundles/{7.ordering-component.14952af682175cdca2c7.js → 7.ordering-component.0a164d98b183999274c5.js} +1 -1
  2. package/_bundles/ordering-component.0a164d98b183999274c5.js +2 -0
  3. package/_modules/components/ProductForm/index.js +56 -49
  4. package/_modules/components/UpsellingPage/index.js +86 -21
  5. package/package.json +1 -1
  6. package/src/components/ProductForm/index.js +21 -17
  7. package/src/components/UpsellingPage/index.js +57 -21
  8. package/_bundles/ordering-component.14952af682175cdca2c7.js +0 -2
  9. /package/_bundles/{0.ordering-component.14952af682175cdca2c7.js → 0.ordering-component.0a164d98b183999274c5.js} +0 -0
  10. /package/_bundles/{1.ordering-component.14952af682175cdca2c7.js → 1.ordering-component.0a164d98b183999274c5.js} +0 -0
  11. /package/_bundles/{2.ordering-component.14952af682175cdca2c7.js → 2.ordering-component.0a164d98b183999274c5.js} +0 -0
  12. /package/_bundles/{4.ordering-component.14952af682175cdca2c7.js → 4.ordering-component.0a164d98b183999274c5.js} +0 -0
  13. /package/_bundles/{5.ordering-component.14952af682175cdca2c7.js → 5.ordering-component.0a164d98b183999274c5.js} +0 -0
  14. /package/_bundles/{6.ordering-component.14952af682175cdca2c7.js → 6.ordering-component.0a164d98b183999274c5.js} +0 -0
  15. /package/_bundles/{7.ordering-component.14952af682175cdca2c7.js.LICENSE.txt → 7.ordering-component.0a164d98b183999274c5.js.LICENSE.txt} +0 -0
  16. /package/_bundles/{8.ordering-component.14952af682175cdca2c7.js → 8.ordering-component.0a164d98b183999274c5.js} +0 -0
  17. /package/_bundles/{ordering-component.14952af682175cdca2c7.js.LICENSE.txt → ordering-component.0a164d98b183999274c5.js.LICENSE.txt} +0 -0
@@ -770,8 +770,9 @@ var ProductForm = exports.ProductForm = function ProductForm(props) {
770
770
  if (!(product !== null && product !== void 0 && product.product)) {
771
771
  return errors;
772
772
  }
773
- (_product$product8 = product.product) === null || _product$product8 === void 0 || (_product$product8 = _product$product8.extras) === null || _product$product8 === void 0 || _product$product8.forEach(function (extra) {
774
- extra.options.map(function (option) {
773
+ product === null || product === void 0 || (_product$product8 = product.product) === null || _product$product8 === void 0 || (_product$product8 = _product$product8.extras) === null || _product$product8 === void 0 || _product$product8.forEach(function (extra) {
774
+ var _extra$options2;
775
+ extra === null || extra === void 0 || (_extra$options2 = extra.options) === null || _extra$options2 === void 0 || _extra$options2.map(function (option) {
775
776
  var _productCart$options3, _pizzaState, _Object$keys2, _option$suboptions3;
776
777
  var suboptions = (_productCart$options3 = productCart.options["id:".concat(option.id)]) === null || _productCart$options3 === void 0 ? void 0 : _productCart$options3.suboptions;
777
778
  var quantity = suboptions ? option !== null && option !== void 0 && option.with_half_option ? pizzaState === null || pizzaState === void 0 || (_pizzaState = pizzaState["option:".concat(option === null || option === void 0 ? void 0 : option.id)]) === null || _pizzaState === void 0 ? void 0 : _pizzaState.value : option.limit_suboptions_by_max ? Object.values(suboptions).reduce(function (count, suboption) {
@@ -1144,39 +1145,42 @@ var ProductForm = exports.ProductForm = function ProductForm(props) {
1144
1145
  (0, _react.useEffect)(function () {
1145
1146
  var _product$product11;
1146
1147
  if (!(product !== null && product !== void 0 && product.loading) && product !== null && product !== void 0 && product.product && ((_product$product11 = product.product) === null || _product$product11 === void 0 || (_product$product11 = _product$product11.extras) === null || _product$product11 === void 0 ? void 0 : _product$product11.length) > 0) {
1148
+ var _product$product12, _product$product13;
1147
1149
  var _selectedSuboptions = {};
1148
1150
  var _dependsSuboptions = {};
1149
1151
  var preselectedOptions = [];
1150
1152
  var preselectedSuboptions = [];
1151
- var _iterator9 = _createForOfIteratorHelper(product.product.extras),
1153
+ var _iterator9 = _createForOfIteratorHelper(product === null || product === void 0 || (_product$product12 = product.product) === null || _product$product12 === void 0 ? void 0 : _product$product12.extras),
1152
1154
  _step9;
1153
1155
  try {
1154
1156
  for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
1155
1157
  var extra = _step9.value;
1156
- var _iterator11 = _createForOfIteratorHelper(extra.options),
1157
- _step11;
1158
- try {
1159
- for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
1160
- var option = _step11.value;
1161
- var _iterator12 = _createForOfIteratorHelper(option.suboptions),
1162
- _step12;
1163
- try {
1164
- for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
1165
- var _option$suboptions6, _props$productCart12;
1166
- var suboption = _step12.value;
1167
- _selectedSuboptions["suboption:".concat(suboption.id)] = (suboption.preselected || (option === null || option === void 0 ? void 0 : option.max) === 1 && (option === null || option === void 0 ? void 0 : option.min) === 1 && (option === null || option === void 0 || (_option$suboptions6 = option.suboptions) === null || _option$suboptions6 === void 0 ? void 0 : _option$suboptions6.length) === 1) && (!editMode || !!((_props$productCart12 = props.productCart) !== null && _props$productCart12 !== void 0 && (_props$productCart12 = _props$productCart12.options["id:".concat(option === null || option === void 0 ? void 0 : option.id)]) !== null && _props$productCart12 !== void 0 && _props$productCart12.suboptions["id:".concat(suboption === null || suboption === void 0 ? void 0 : suboption.id)]));
1168
- _dependsSuboptions["suboption:".concat(suboption.id)] = option !== null && option !== void 0 && option.conditioned && (option === null || option === void 0 ? void 0 : option.respect_to) !== null ? option === null || option === void 0 ? void 0 : option.respect_to : null;
1158
+ if (extra !== null && extra !== void 0 && extra.options) {
1159
+ var _iterator11 = _createForOfIteratorHelper(extra === null || extra === void 0 ? void 0 : extra.options),
1160
+ _step11;
1161
+ try {
1162
+ for (_iterator11.s(); !(_step11 = _iterator11.n()).done;) {
1163
+ var option = _step11.value;
1164
+ var _iterator12 = _createForOfIteratorHelper(option === null || option === void 0 ? void 0 : option.suboptions),
1165
+ _step12;
1166
+ try {
1167
+ for (_iterator12.s(); !(_step12 = _iterator12.n()).done;) {
1168
+ var _option$suboptions6, _props$productCart12;
1169
+ var suboption = _step12.value;
1170
+ _selectedSuboptions["suboption:".concat(suboption.id)] = (suboption.preselected || (option === null || option === void 0 ? void 0 : option.max) === 1 && (option === null || option === void 0 ? void 0 : option.min) === 1 && (option === null || option === void 0 || (_option$suboptions6 = option.suboptions) === null || _option$suboptions6 === void 0 ? void 0 : _option$suboptions6.length) === 1) && (!editMode || !!((_props$productCart12 = props.productCart) !== null && _props$productCart12 !== void 0 && (_props$productCart12 = _props$productCart12.options["id:".concat(option === null || option === void 0 ? void 0 : option.id)]) !== null && _props$productCart12 !== void 0 && _props$productCart12.suboptions["id:".concat(suboption === null || suboption === void 0 ? void 0 : suboption.id)]));
1171
+ _dependsSuboptions["suboption:".concat(suboption.id)] = option !== null && option !== void 0 && option.conditioned && (option === null || option === void 0 ? void 0 : option.respect_to) !== null ? option === null || option === void 0 ? void 0 : option.respect_to : null;
1172
+ }
1173
+ } catch (err) {
1174
+ _iterator12.e(err);
1175
+ } finally {
1176
+ _iterator12.f();
1169
1177
  }
1170
- } catch (err) {
1171
- _iterator12.e(err);
1172
- } finally {
1173
- _iterator12.f();
1174
1178
  }
1179
+ } catch (err) {
1180
+ _iterator11.e(err);
1181
+ } finally {
1182
+ _iterator11.f();
1175
1183
  }
1176
- } catch (err) {
1177
- _iterator11.e(err);
1178
- } finally {
1179
- _iterator11.f();
1180
1184
  }
1181
1185
  }
1182
1186
  } catch (err) {
@@ -1193,36 +1197,38 @@ var ProductForm = exports.ProductForm = function ProductForm(props) {
1193
1197
  });
1194
1198
  });
1195
1199
  }
1196
- var _iterator10 = _createForOfIteratorHelper(product.product.extras),
1200
+ var _iterator10 = _createForOfIteratorHelper(product === null || product === void 0 || (_product$product13 = product.product) === null || _product$product13 === void 0 ? void 0 : _product$product13.extras),
1197
1201
  _step10;
1198
1202
  try {
1199
1203
  for (_iterator10.s(); !(_step10 = _iterator10.n()).done;) {
1200
1204
  var _extra4 = _step10.value;
1201
- var _iterator13 = _createForOfIteratorHelper(_extra4.options),
1202
- _step13;
1203
- try {
1204
- for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
1205
- var _option5 = _step13.value;
1206
- var _iterator14 = _createForOfIteratorHelper(_option5.suboptions),
1207
- _step14;
1208
- try {
1209
- for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
1210
- var _suboption3 = _step14.value;
1211
- if (_checkSuboptionsSelected(_suboption3 === null || _suboption3 === void 0 ? void 0 : _suboption3.id, _selectedSuboptions, _dependsSuboptions)) {
1212
- preselectedOptions.push(_option5);
1213
- preselectedSuboptions.push(_suboption3);
1205
+ if (_extra4 !== null && _extra4 !== void 0 && _extra4.options) {
1206
+ var _iterator13 = _createForOfIteratorHelper(_extra4 === null || _extra4 === void 0 ? void 0 : _extra4.options),
1207
+ _step13;
1208
+ try {
1209
+ for (_iterator13.s(); !(_step13 = _iterator13.n()).done;) {
1210
+ var _option5 = _step13.value;
1211
+ var _iterator14 = _createForOfIteratorHelper(_option5 === null || _option5 === void 0 ? void 0 : _option5.suboptions),
1212
+ _step14;
1213
+ try {
1214
+ for (_iterator14.s(); !(_step14 = _iterator14.n()).done;) {
1215
+ var _suboption3 = _step14.value;
1216
+ if (_checkSuboptionsSelected(_suboption3 === null || _suboption3 === void 0 ? void 0 : _suboption3.id, _selectedSuboptions, _dependsSuboptions)) {
1217
+ preselectedOptions.push(_option5);
1218
+ preselectedSuboptions.push(_suboption3);
1219
+ }
1214
1220
  }
1221
+ } catch (err) {
1222
+ _iterator14.e(err);
1223
+ } finally {
1224
+ _iterator14.f();
1215
1225
  }
1216
- } catch (err) {
1217
- _iterator14.e(err);
1218
- } finally {
1219
- _iterator14.f();
1220
1226
  }
1227
+ } catch (err) {
1228
+ _iterator13.e(err);
1229
+ } finally {
1230
+ _iterator13.f();
1221
1231
  }
1222
- } catch (err) {
1223
- _iterator13.e(err);
1224
- } finally {
1225
- _iterator13.f();
1226
1232
  }
1227
1233
  }
1228
1234
  } catch (err) {
@@ -1298,9 +1304,10 @@ var ProductForm = exports.ProductForm = function ProductForm(props) {
1298
1304
  if (isStarbucks) {
1299
1305
  (0, _react.useEffect)(function () {
1300
1306
  if (product !== null && product !== void 0 && product.product && Object.keys(product === null || product === void 0 ? void 0 : product.product).length) {
1301
- var _ref7, _ref8;
1302
- var options = (_ref7 = []).concat.apply(_ref7, _toConsumableArray(product.product.extras.map(function (extra) {
1303
- return extra.options.filter(function (option) {
1307
+ var _ref7, _product$product14, _ref8;
1308
+ var options = (_ref7 = []).concat.apply(_ref7, _toConsumableArray((_product$product14 = product.product) === null || _product$product14 === void 0 || (_product$product14 = _product$product14.extras) === null || _product$product14 === void 0 ? void 0 : _product$product14.map(function (extra) {
1309
+ var _extra$options3;
1310
+ return extra === null || extra === void 0 || (_extra$options3 = extra.options) === null || _extra$options3 === void 0 ? void 0 : _extra$options3.filter(function (option) {
1304
1311
  return option.name === 'Tamaño' && option.suboptions.filter(function (suboption) {
1305
1312
  return suboption.name === 'Grande (16oz - 437ml)';
1306
1313
  }).length === 1;
@@ -9,6 +9,7 @@ var _react = _interopRequireWildcard(require("react"));
9
9
  var _propTypes = _interopRequireDefault(require("prop-types"));
10
10
  var _ApiContext = require("../../contexts/ApiContext");
11
11
  var _OrderContext = require("../../contexts/OrderContext");
12
+ var _SessionContext = require("../../contexts/SessionContext");
12
13
  function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
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); }
14
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 && {}.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; }
@@ -30,6 +31,7 @@ function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
30
31
  var UpsellingPage = exports.UpsellingPage = function UpsellingPage(props) {
31
32
  var _Object$values$find$b, _Object$values$find;
32
33
  var UIComponent = props.UIComponent,
34
+ useSuggestiveUpselling = props.useSuggestiveUpselling,
33
35
  products = props.products,
34
36
  cartProducts = props.cartProducts,
35
37
  onSave = props.onSave;
@@ -51,6 +53,10 @@ var UpsellingPage = exports.UpsellingPage = function UpsellingPage(props) {
51
53
  var _useOrder = (0, _OrderContext.useOrder)(),
52
54
  _useOrder2 = _slicedToArray(_useOrder, 1),
53
55
  orderState = _useOrder2[0];
56
+ var _useSession = (0, _SessionContext.useSession)(),
57
+ _useSession2 = _slicedToArray(_useSession, 1),
58
+ token = _useSession2[0].token;
59
+ var currentCart = orderState.carts["businessId:".concat(props.businessId)];
54
60
  var businessId = props.uuid ? (_Object$values$find$b = (_Object$values$find = Object.values(orderState.carts).find(function (_cart) {
55
61
  return (_cart === null || _cart === void 0 ? void 0 : _cart.uuid) === props.uuid;
56
62
  })) === null || _Object$values$find === void 0 ? void 0 : _Object$values$find.business_id) !== null && _Object$values$find$b !== void 0 ? _Object$values$find$b : {} : props.businessId;
@@ -59,7 +65,13 @@ var UpsellingPage = exports.UpsellingPage = function UpsellingPage(props) {
59
65
  if (products !== null && products !== void 0 && products.length && !props.uuid) {
60
66
  getUpsellingProducts(products);
61
67
  } else {
62
- getProducts();
68
+ if (useSuggestiveUpselling) {
69
+ setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
70
+ loading: false
71
+ }));
72
+ } else {
73
+ getProducts();
74
+ }
63
75
  }
64
76
  } else {
65
77
  setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
@@ -69,10 +81,15 @@ var UpsellingPage = exports.UpsellingPage = function UpsellingPage(props) {
69
81
  }
70
82
  }, [businessId]);
71
83
  (0, _react.useEffect)(function () {
72
- if (!upsellingProducts.loading) {
84
+ if (useSuggestiveUpselling) return;
85
+ if (!upsellingProducts.loading && !orderState.loading) {
73
86
  getUpsellingProducts(businessProducts);
74
87
  }
75
- }, [orderState.loading]);
88
+ }, [orderState.loading, upsellingProducts.loading]);
89
+ (0, _react.useEffect)(function () {
90
+ if (!(cartProducts !== null && cartProducts !== void 0 && cartProducts.length) || !useSuggestiveUpselling || upsellingProducts.loading || orderState.loading) return;
91
+ getSuggestiveProducts();
92
+ }, [cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.length, orderState.loading, upsellingProducts.loading]);
76
93
 
77
94
  /**
78
95
  * getting products if array of product is not defined
@@ -113,35 +130,82 @@ var UpsellingPage = exports.UpsellingPage = function UpsellingPage(props) {
113
130
  return _ref.apply(this, arguments);
114
131
  };
115
132
  }();
133
+
134
+ /**
135
+ * getting suggestive products if useSuggestiveUpselling is true
136
+ */
137
+ var getSuggestiveProducts = /*#__PURE__*/function () {
138
+ var _ref2 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime().mark(function _callee2() {
139
+ var _yield$ordering$setAc, _yield$ordering$setAc2, error, result;
140
+ return _regeneratorRuntime().wrap(function _callee2$(_context2) {
141
+ while (1) switch (_context2.prev = _context2.next) {
142
+ case 0:
143
+ if (currentCart !== null && currentCart !== void 0 && currentCart.uuid) {
144
+ _context2.next = 2;
145
+ break;
146
+ }
147
+ return _context2.abrupt("return");
148
+ case 2:
149
+ _context2.prev = 2;
150
+ _context2.next = 5;
151
+ return ordering.setAccessToken(token).carts(currentCart === null || currentCart === void 0 ? void 0 : currentCart.uuid).getUpselling();
152
+ case 5:
153
+ _yield$ordering$setAc = _context2.sent;
154
+ _yield$ordering$setAc2 = _yield$ordering$setAc.content;
155
+ error = _yield$ordering$setAc2.error;
156
+ result = _yield$ordering$setAc2.result;
157
+ if (!error) {
158
+ setBusinessProducts(result);
159
+ getUpsellingProducts(result, true);
160
+ } else {
161
+ setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
162
+ loading: false,
163
+ error: error
164
+ }));
165
+ }
166
+ _context2.next = 15;
167
+ break;
168
+ case 12:
169
+ _context2.prev = 12;
170
+ _context2.t0 = _context2["catch"](2);
171
+ setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
172
+ loading: false,
173
+ error: _context2.t0
174
+ }));
175
+ case 15:
176
+ case "end":
177
+ return _context2.stop();
178
+ }
179
+ }, _callee2, null, [[2, 12]]);
180
+ }));
181
+ return function getSuggestiveProducts() {
182
+ return _ref2.apply(this, arguments);
183
+ };
184
+ }();
185
+
116
186
  /**
117
187
  *
118
188
  * filt products if they are already in the cart
119
189
  * @param {array} cartProducts
120
190
  */
121
191
  var getUpsellingProducts = function getUpsellingProducts(result) {
192
+ var allowAll = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
122
193
  var upsellingProductsfiltered = result.filter(function (product) {
123
- return product.upselling;
194
+ return product.upselling || allowAll;
124
195
  });
125
- var repeatProducts = cartProducts && (cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.filter(function (cartProduct) {
196
+ var repeatProducts = cartProducts !== null && cartProducts !== void 0 && cartProducts.length ? cartProducts === null || cartProducts === void 0 ? void 0 : cartProducts.filter(function (cartProduct) {
126
197
  return upsellingProductsfiltered.find(function (product) {
127
198
  return product.id === cartProduct.id;
128
199
  });
200
+ }) : [];
201
+ setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
202
+ loading: false,
203
+ products: repeatProducts !== null && repeatProducts !== void 0 && repeatProducts.length ? upsellingProductsfiltered === null || upsellingProductsfiltered === void 0 ? void 0 : upsellingProductsfiltered.filter(function (product) {
204
+ return !product.inventoried && !repeatProducts.find(function (repeatProduct) {
205
+ return repeatProduct.id === product.id;
206
+ });
207
+ }) : upsellingProductsfiltered
129
208
  }));
130
- if (repeatProducts.length) {
131
- setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
132
- loading: false,
133
- products: upsellingProductsfiltered.filter(function (product) {
134
- return !product.inventoried && !repeatProducts.find(function (repeatProduct) {
135
- return repeatProduct.id === product.id;
136
- });
137
- })
138
- }));
139
- } else {
140
- setUpsellingProducts(_objectSpread(_objectSpread({}, upsellingProducts), {}, {
141
- loading: false,
142
- products: upsellingProductsfiltered
143
- }));
144
- }
145
209
  };
146
210
 
147
211
  /**
@@ -153,7 +217,8 @@ var UpsellingPage = exports.UpsellingPage = function UpsellingPage(props) {
153
217
  };
154
218
  return /*#__PURE__*/_react.default.createElement(UIComponent, _extends({}, props, {
155
219
  upsellingProducts: upsellingProducts,
156
- handleFormProduct: handleFormProduct
220
+ handleFormProduct: handleFormProduct,
221
+ businessId: businessId
157
222
  }));
158
223
  };
159
224
  UpsellingPage.propTypes = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ordering-components-external",
3
- "version": "13.2.19",
3
+ "version": "13.2.20",
4
4
  "description": "Ordering Internal Dashboard",
5
5
  "main": "./_modules/index.js",
6
6
  "scripts": {
@@ -533,8 +533,8 @@ export const ProductForm = (props) => {
533
533
  if (!product?.product) {
534
534
  return errors
535
535
  }
536
- product.product?.extras?.forEach(extra => {
537
- extra.options.map(option => {
536
+ product?.product?.extras?.forEach(extra => {
537
+ extra?.options?.map(option => {
538
538
  const suboptions = productCart.options[`id:${option.id}`]?.suboptions
539
539
  const quantity = suboptions
540
540
  ? option?.with_half_option
@@ -844,14 +844,16 @@ export const ProductForm = (props) => {
844
844
  const _dependsSuboptions = {}
845
845
  const preselectedOptions = []
846
846
  const preselectedSuboptions = []
847
- for (const extra of product.product.extras) {
848
- for (const option of extra.options) {
849
- for (const suboption of option.suboptions) {
850
- _selectedSuboptions[`suboption:${suboption.id}`] =
851
- (suboption.preselected ||
852
- (option?.max === 1 && option?.min === 1 && option?.suboptions?.length === 1)) &&
853
- (!editMode || !!props.productCart?.options[`id:${option?.id}`]?.suboptions[`id:${suboption?.id}`])
854
- _dependsSuboptions[`suboption:${suboption.id}`] = option?.conditioned && option?.respect_to !== null ? option?.respect_to : null
847
+ for (const extra of product?.product?.extras) {
848
+ if (extra?.options) {
849
+ for (const option of extra?.options) {
850
+ for (const suboption of option?.suboptions) {
851
+ _selectedSuboptions[`suboption:${suboption.id}`] =
852
+ (suboption.preselected ||
853
+ (option?.max === 1 && option?.min === 1 && option?.suboptions?.length === 1)) &&
854
+ (!editMode || !!props.productCart?.options[`id:${option?.id}`]?.suboptions[`id:${suboption?.id}`])
855
+ _dependsSuboptions[`suboption:${suboption.id}`] = option?.conditioned && option?.respect_to !== null ? option?.respect_to : null
856
+ }
855
857
  }
856
858
  }
857
859
  }
@@ -862,12 +864,14 @@ export const ProductForm = (props) => {
862
864
  }))
863
865
  }
864
866
 
865
- for (const extra of product.product.extras) {
866
- for (const option of extra.options) {
867
- for (const suboption of option.suboptions) {
868
- if (checkSuboptionsSelected(suboption?.id, _selectedSuboptions, _dependsSuboptions)) {
869
- preselectedOptions.push(option)
870
- preselectedSuboptions.push(suboption)
867
+ for (const extra of product?.product?.extras) {
868
+ if (extra?.options) {
869
+ for (const option of extra?.options) {
870
+ for (const suboption of option?.suboptions) {
871
+ if (checkSuboptionsSelected(suboption?.id, _selectedSuboptions, _dependsSuboptions)) {
872
+ preselectedOptions.push(option)
873
+ preselectedSuboptions.push(suboption)
874
+ }
871
875
  }
872
876
  }
873
877
  }
@@ -948,7 +952,7 @@ export const ProductForm = (props) => {
948
952
  if (isStarbucks) {
949
953
  useEffect(() => {
950
954
  if (product?.product && Object.keys(product?.product).length) {
951
- const options = [].concat(...product.product.extras.map(extra => extra.options.filter(
955
+ const options = [].concat(...product.product?.extras?.map(extra => extra?.options?.filter(
952
956
  option => (
953
957
  option.name === 'Tamaño' &&
954
958
  option.suboptions.filter(suboption => suboption.name === 'Grande (16oz - 437ml)').length === 1
@@ -3,14 +3,16 @@ import PropTypes from 'prop-types'
3
3
 
4
4
  import { useApi } from '../../contexts/ApiContext'
5
5
  import { useOrder } from '../../contexts/OrderContext'
6
+ import { useSession } from '../../contexts/SessionContext'
6
7
 
7
8
  export const UpsellingPage = (props) => {
8
- const { UIComponent, products, cartProducts, onSave } = props
9
-
9
+ const { UIComponent, useSuggestiveUpselling, products, cartProducts, onSave } = props
10
10
  const [upsellingProducts, setUpsellingProducts] = useState({ products: [], loading: true, error: false })
11
11
  const [businessProducts, setBusinessProducts] = useState([])
12
12
  const [ordering] = useApi()
13
13
  const [orderState] = useOrder()
14
+ const [{ token }] = useSession()
15
+ const currentCart = orderState.carts[`businessId:${props.businessId}`]
14
16
 
15
17
  const businessId = props.uuid
16
18
  ? Object.values(orderState.carts).find(_cart => _cart?.uuid === props.uuid)?.business_id ?? {}
@@ -21,7 +23,14 @@ export const UpsellingPage = (props) => {
21
23
  if (products?.length && !props.uuid) {
22
24
  getUpsellingProducts(products)
23
25
  } else {
24
- getProducts()
26
+ if (useSuggestiveUpselling) {
27
+ setUpsellingProducts({
28
+ ...upsellingProducts,
29
+ loading: false
30
+ })
31
+ } else {
32
+ getProducts()
33
+ }
25
34
  }
26
35
  } else {
27
36
  setUpsellingProducts({
@@ -33,10 +42,16 @@ export const UpsellingPage = (props) => {
33
42
  }, [businessId])
34
43
 
35
44
  useEffect(() => {
36
- if (!upsellingProducts.loading) {
45
+ if (useSuggestiveUpselling) return
46
+ if (!upsellingProducts.loading && !orderState.loading) {
37
47
  getUpsellingProducts(businessProducts)
38
48
  }
39
- }, [orderState.loading])
49
+ }, [orderState.loading, upsellingProducts.loading])
50
+
51
+ useEffect(() => {
52
+ if (!cartProducts?.length || !useSuggestiveUpselling || upsellingProducts.loading || orderState.loading) return
53
+ getSuggestiveProducts()
54
+ }, [cartProducts?.length, orderState.loading, upsellingProducts.loading])
40
55
 
41
56
  /**
42
57
  * getting products if array of product is not defined
@@ -58,31 +73,51 @@ export const UpsellingPage = (props) => {
58
73
  })
59
74
  }
60
75
  }
76
+
61
77
  /**
62
- *
63
- * filt products if they are already in the cart
64
- * @param {array} cartProducts
78
+ * getting suggestive products if useSuggestiveUpselling is true
65
79
  */
66
- const getUpsellingProducts = (result) => {
67
- const upsellingProductsfiltered = result.filter(product => product.upselling)
68
-
69
- const repeatProducts = cartProducts && cartProducts?.filter(cartProduct => upsellingProductsfiltered.find(product => product.id === cartProduct.id))
70
-
71
- if (repeatProducts.length) {
72
- setUpsellingProducts({
73
- ...upsellingProducts,
74
- loading: false,
75
- products: upsellingProductsfiltered.filter(product => !product.inventoried && !repeatProducts.find(repeatProduct => repeatProduct.id === product.id))
76
- })
77
- } else {
80
+ const getSuggestiveProducts = async () => {
81
+ if (!currentCart?.uuid) return
82
+ try {
83
+ const { content: { error, result } } = await ordering.setAccessToken(token).carts(currentCart?.uuid).getUpselling()
84
+ if (!error) {
85
+ setBusinessProducts(result)
86
+ getUpsellingProducts(result, true)
87
+ } else {
88
+ setUpsellingProducts({
89
+ ...upsellingProducts,
90
+ loading: false,
91
+ error
92
+ })
93
+ }
94
+ } catch (error) {
78
95
  setUpsellingProducts({
79
96
  ...upsellingProducts,
80
97
  loading: false,
81
- products: upsellingProductsfiltered
98
+ error
82
99
  })
83
100
  }
84
101
  }
85
102
 
103
+ /**
104
+ *
105
+ * filt products if they are already in the cart
106
+ * @param {array} cartProducts
107
+ */
108
+ const getUpsellingProducts = (result, allowAll = false) => {
109
+ const upsellingProductsfiltered = result.filter(product => product.upselling || allowAll)
110
+ const repeatProducts = cartProducts?.length ? cartProducts?.filter(cartProduct => upsellingProductsfiltered.find(product => product.id === cartProduct.id)) : []
111
+
112
+ setUpsellingProducts({
113
+ ...upsellingProducts,
114
+ loading: false,
115
+ products: repeatProducts?.length
116
+ ? upsellingProductsfiltered?.filter(product => !product.inventoried && !repeatProducts.find(repeatProduct => repeatProduct.id === product.id))
117
+ : upsellingProductsfiltered
118
+ })
119
+ }
120
+
86
121
  /**
87
122
  * Function for confirm that the productForm now can be displayed
88
123
  * @param {product} product
@@ -96,6 +131,7 @@ export const UpsellingPage = (props) => {
96
131
  {...props}
97
132
  upsellingProducts={upsellingProducts}
98
133
  handleFormProduct={handleFormProduct}
134
+ businessId={businessId}
99
135
  />
100
136
  )
101
137
  }