@sentecacommerce-theme/lib 0.12.103 → 0.13.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.
Files changed (44) hide show
  1. package/dist/cjs/api/myCartApi/index.js +4 -1
  2. package/dist/cjs/components/AnalyticsScripts/index.js +3 -2
  3. package/dist/cjs/hooks/useCheckoutSubmit/index.js +4 -1
  4. package/dist/cjs/hooks/useLineItem/LineItemContext.js +86 -0
  5. package/dist/cjs/hooks/useLineItem/index.js +34 -12
  6. package/dist/cjs/hooks/useProduct/index.js +17 -276
  7. package/dist/cjs/hooks/useProduct/utils/index.js +44 -18
  8. package/dist/cjs/hooks/useProductVariants/index.js +334 -0
  9. package/dist/cjs/hooks/useProductVariants/types.js +3 -0
  10. package/dist/cjs/index.js +16 -4
  11. package/dist/cjs/listing/api/queries/useListingQuery.js +1 -0
  12. package/dist/cjs/listing/hooks/useListingBreadcrumbs.js +20 -8
  13. package/dist/cjs/listing/hooks/useListingCategories.js +2 -2
  14. package/dist/cjs/listing/hooks/useListingMeta.js +12 -10
  15. package/dist/cjs/listing/utils/buildCategoryQuery.js +22 -1
  16. package/dist/cjs/seo/components/TranslationsMeta/index.js +1 -0
  17. package/dist/cjs/utils/localStorage.js +7 -2
  18. package/dist/esm/api/myCartApi/index.js +4 -1
  19. package/dist/esm/components/AnalyticsScripts/index.js +3 -2
  20. package/dist/esm/hooks/useCheckoutSubmit/index.js +4 -1
  21. package/dist/esm/hooks/useLineItem/LineItemContext.js +75 -0
  22. package/dist/esm/hooks/useLineItem/index.js +16 -12
  23. package/dist/esm/hooks/useProduct/index.js +18 -277
  24. package/dist/esm/hooks/useProduct/utils/index.js +44 -18
  25. package/dist/esm/hooks/useProductVariants/index.js +293 -0
  26. package/dist/esm/hooks/useProductVariants/types.js +1 -0
  27. package/dist/esm/index.js +1 -0
  28. package/dist/esm/listing/api/queries/useListingQuery.js +1 -0
  29. package/dist/esm/listing/hooks/useListingBreadcrumbs.js +15 -3
  30. package/dist/esm/listing/hooks/useListingCategories.js +2 -2
  31. package/dist/esm/listing/hooks/useListingMeta.js +12 -12
  32. package/dist/esm/listing/utils/buildCategoryQuery.js +22 -1
  33. package/dist/esm/seo/components/TranslationsMeta/index.js +1 -0
  34. package/dist/esm/utils/localStorage.js +7 -2
  35. package/dist/types/hooks/useLineItem/LineItemContext.d.ts +22 -0
  36. package/dist/types/hooks/useLineItem/index.d.ts +2 -2
  37. package/dist/types/hooks/useProduct/index.d.ts +2 -76
  38. package/dist/types/hooks/useProduct/utils/index.d.ts +6 -4
  39. package/dist/types/hooks/useProductVariants/index.d.ts +34 -0
  40. package/dist/types/hooks/useProductVariants/types.d.ts +79 -0
  41. package/dist/types/index.d.ts +1 -0
  42. package/dist/types/listing/hooks/useListingMeta.d.ts +4 -0
  43. package/dist/types/listing/types.d.ts +1 -0
  44. package/package.json +5 -5
@@ -64,7 +64,7 @@ var buildCategoryQuery = function(param) {
64
64
  filter += (ref = config === null || config === void 0 ? void 0 : config.filterExtension) !== null && ref !== void 0 ? ref : '';
65
65
  }
66
66
  if (postFilter) {
67
- if (router.query.cat) filter += '&';
67
+ if (filter) filter += '&';
68
68
  filter += postFilter;
69
69
  }
70
70
  break;
@@ -81,6 +81,14 @@ var buildCategoryQuery = function(param) {
81
81
  filter += "text;eq;\"".concat(searchQuery, "\"");
82
82
  if (postFilter) filter += "&".concat(postFilter);
83
83
  break;
84
+ case _index.ListingTypesEnum.View:
85
+ filter += applyViewsPostFilter({
86
+ postFilter: postFilter
87
+ });
88
+ if (config === null || config === void 0 ? void 0 : config.filterExtension) {
89
+ filter += filter ? "&".concat(config === null || config === void 0 ? void 0 : config.filterExtension) : config === null || config === void 0 ? void 0 : config.filterExtension;
90
+ }
91
+ break;
84
92
  default:
85
93
  return "".concat((config === null || config === void 0 ? void 0 : config.filterExtension) + "".concat(postFilter !== null && postFilter !== void 0 ? postFilter : ''));
86
94
  }
@@ -113,3 +121,16 @@ var applyBrandsPostFilter = function(param) {
113
121
  }
114
122
  return '';
115
123
  };
124
+ var applyViewsPostFilter = function(param) {
125
+ var postFilter = param.postFilter;
126
+ if (postFilter) {
127
+ var filtersArr = postFilter.split('&');
128
+ var filters = filtersArr.reduce(function(res, val, i) {
129
+ if (val.includes('category')) return res;
130
+ if (!res) return res + "".concat(val);
131
+ return res += "&".concat(val);
132
+ }, '');
133
+ return "".concat(filters);
134
+ }
135
+ return '';
136
+ };
@@ -31,6 +31,7 @@ var TranslationsMeta = function() {
31
31
  var defaultTranslation = translationsMap[defaultLocale];
32
32
  var isError = router.route === '/404' || router.route === '/_error';
33
33
  if (isError) return null;
34
+ if ((allLocales === null || allLocales === void 0 ? void 0 : allLocales.length) < 2) return null;
34
35
  var domain = defaultTranslation === null || defaultTranslation === void 0 ? void 0 : defaultTranslation.domain;
35
36
  return(/*#__PURE__*/ _react.default.createElement(_head.default, null, allLocales === null || allLocales === void 0 ? void 0 : allLocales.map(function(locale) {
36
37
  var translation = translationsMap === null || translationsMap === void 0 ? void 0 : translationsMap[locale];
@@ -4,12 +4,17 @@ Object.defineProperty(exports, "__esModule", {
4
4
  exports.writeToLocalStorage = exports.readFromLocalStorage = void 0;
5
5
  var readFromLocalStorage = function(key) {
6
6
  if (typeof window !== 'undefined') {
7
- return JSON.parse(localStorage.getItem(key));
7
+ try {
8
+ return JSON.parse(localStorage.getItem(key));
9
+ } catch (e) {
10
+ console.log(e);
11
+ return null;
12
+ }
8
13
  }
9
14
  };
10
15
  exports.readFromLocalStorage = readFromLocalStorage;
11
16
  var writeToLocalStorage = function(key, data) {
12
- if (typeof window !== 'undefined') {
17
+ if (typeof window !== 'undefined' && data !== undefined) {
13
18
  localStorage.setItem(key, JSON.stringify(data));
14
19
  }
15
20
  };
@@ -250,7 +250,9 @@ export var MyCartApiFactory = function() {
250
250
  var product = data.lineItems.find(function(p) {
251
251
  return p.productId === params.productId;
252
252
  });
253
- product && addToCart(product.variant.sku, params.quantity, product.offer.price || product.offer.originalPrice);
253
+ if (hasAnalytics) {
254
+ product && addToCart(product.variant.sku, params.quantity, product.offer.price || product.offer.originalPrice);
255
+ }
254
256
  queryCache.setQueryData([
255
257
  myCartCacheKeys.useIsExisting,
256
258
  params.productId,
@@ -543,6 +545,7 @@ export var MyCartApiFactory = function() {
543
545
  });
544
546
  };
545
547
  var instance;
548
+ var hasAnalytics = !process.env.NO_ANALYTICS;
546
549
  return {
547
550
  getInstance: function getInstance() {
548
551
  if (!instance) {
@@ -50,6 +50,7 @@ function _unsupportedIterableToArray(o, minLen) {
50
50
  if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
51
51
  }
52
52
  var isProduction = process.env.NODE_ENV === 'production';
53
+ var hasAnalytics = !process.env.NO_ANALYTICS;
53
54
  export function AnalyticsScripts() {
54
55
  var ref11, ref1, ref2, ref3, ref4;
55
56
  var timeout = useRef(null);
@@ -87,7 +88,7 @@ export function AnalyticsScripts() {
87
88
  }
88
89
  };
89
90
  useEffect(function() {
90
- if (!isProduction) return;
91
+ if (!isProduction && !hasAnalytics) return;
91
92
  if (document) {
92
93
  timeout.current = setTimeout(initAnalytics, 3500);
93
94
  document.addEventListener('scroll', initAnalyticsOnEvent);
@@ -99,7 +100,7 @@ export function AnalyticsScripts() {
99
100
  ]);
100
101
  var pageView = getPageView(config);
101
102
  useEffect(function() {
102
- if (!isProduction) return;
103
+ if (!isProduction && !hasAnalytics) return;
103
104
  var handleRouteChange = function(url) {
104
105
  if ('browser' in process) {
105
106
  setTimeout(function() {
@@ -82,6 +82,7 @@ export var useCheckoutSubmit = function(param) {
82
82
  var onSuccess = param.onSuccess, onBeforeBoricaRedirect = param.onBeforeBoricaRedirect, onBeforePaypalRedirect = param.onBeforePaypalRedirect, onBeforeUniCreditRedirect = param.onBeforeUniCreditRedirect, onError = param.onError, withMeta = param.withMeta, disablePaymentRedirects = param.disablePaymentRedirects;
83
83
  var Config = useConfigState();
84
84
  var districtMeta = useDistrictMeta();
85
+ var hasAnalytics = !process.env.NO_ANALYTICS;
85
86
  var ref = _slicedToArray(myOrdersApi.useCreate(withMeta ? districtMeta : {}, function() {
86
87
  var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(result) {
87
88
  var paypalPayment, boricaPayment, uniCreditPayment, boricaResult, paypalResponse, uniCreditStartSessionResponse;
@@ -89,7 +90,9 @@ export var useCheckoutSubmit = function(param) {
89
90
  while(1)switch(_ctx.prev = _ctx.next){
90
91
  case 0:
91
92
  // Analytics
92
- purchase(result, Config);
93
+ if (hasAnalytics) {
94
+ purchase(result, Config);
95
+ }
93
96
  paypalPayment = result.payments.find(function(payment) {
94
97
  return payment.type === PaymentDTOTypeEnum.PaymentServiceProvider && payment.moduleKey === 'Paypal';
95
98
  });
@@ -0,0 +1,75 @@
1
+ import React from 'react';
2
+ import { useProductVariants } from '../useProductVariants';
3
+ import { getTranslatableField } from '../..';
4
+ function _defineProperty(obj, key, value) {
5
+ if (key in obj) {
6
+ Object.defineProperty(obj, key, {
7
+ value: value,
8
+ enumerable: true,
9
+ configurable: true,
10
+ writable: true
11
+ });
12
+ } else {
13
+ obj[key] = value;
14
+ }
15
+ return obj;
16
+ }
17
+ function _objectSpread(target) {
18
+ for(var i = 1; i < arguments.length; i++){
19
+ var source = arguments[i] != null ? arguments[i] : {};
20
+ var ownKeys = Object.keys(source);
21
+ if (typeof Object.getOwnPropertySymbols === "function") {
22
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
23
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
24
+ }));
25
+ }
26
+ ownKeys.forEach(function(key) {
27
+ _defineProperty(target, key, source[key]);
28
+ });
29
+ }
30
+ return target;
31
+ }
32
+ var LineItemStateContext = /*#__PURE__*/ React.createContext(undefined);
33
+ var CountDispatchContext = /*#__PURE__*/ React.createContext(undefined);
34
+ export function LineItemStateProvider(param) {
35
+ var product = param.product, children = param.children;
36
+ var ref = useProductVariants({
37
+ product: product,
38
+ defaultVariantId: product.variantId,
39
+ isLineItem: true
40
+ }), variants = ref.variants, selectedVariant = ref.selectedVariant, getVariantById = ref.getVariantById, getVariantByAttribute = ref.getVariantByAttribute, getVariantByCombination = ref.getVariantByCombination, selectVariantById = ref.selectVariantById, selectVariantByAttribute = ref.selectVariantByAttribute, selectVariantByCombination = ref.selectVariantByCombination;
41
+ var state = {
42
+ variants: variants,
43
+ selectedVariant: _objectSpread({}, selectedVariant, {
44
+ name: getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.name),
45
+ slug: getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.slug)
46
+ })
47
+ };
48
+ var functions = {
49
+ getVariantById: getVariantById,
50
+ getVariantByAttribute: getVariantByAttribute,
51
+ getVariantByCombination: getVariantByCombination,
52
+ selectVariantById: selectVariantById,
53
+ selectVariantByAttribute: selectVariantByAttribute,
54
+ selectVariantByCombination: selectVariantByCombination
55
+ };
56
+ return(/*#__PURE__*/ React.createElement(LineItemStateContext.Provider, {
57
+ value: state
58
+ }, /*#__PURE__*/ React.createElement(CountDispatchContext.Provider, {
59
+ value: functions
60
+ }, children)));
61
+ }
62
+ export function useLineItemState() {
63
+ var context = React.useContext(LineItemStateContext);
64
+ if (context === undefined) {
65
+ throw new Error('useLineItemState must be used within a LineItemStateProvider');
66
+ }
67
+ return context;
68
+ }
69
+ export function useLineItemFunctions() {
70
+ var context = React.useContext(CountDispatchContext);
71
+ if (context === undefined) {
72
+ throw new Error('useLineItemFunctions must be used within a LineItemStateProvider');
73
+ }
74
+ return context;
75
+ }
@@ -29,11 +29,12 @@ function _objectSpread(target) {
29
29
  }
30
30
  return target;
31
31
  }
32
+ export { useLineItemFunctions, useLineItemState, LineItemStateProvider } from './LineItemContext';
32
33
  export var useLineItem = function(props) {
33
- var ref16 = usePrefetchProduct(getTranslatableField(props === null || props === void 0 ? void 0 : props.slug)), onMouseEnter = ref16.onMouseEnter, onMouseLeave = ref16.onMouseLeave, onMouseDown = ref16.onMouseDown;
34
+ var ref17 = usePrefetchProduct(getTranslatableField(props === null || props === void 0 ? void 0 : props.slug)), onMouseEnter = ref17.onMouseEnter, onMouseLeave = ref17.onMouseLeave, onMouseDown = ref17.onMouseDown;
34
35
  var data = React.useMemo(function() {
35
- var ref17, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, ref10, ref11, ref12;
36
- var mappedAttributes = (ref17 = Object.keys(props === null || props === void 0 ? void 0 : props.attributes)) === null || ref17 === void 0 ? void 0 : ref17.reduce(function(map, key) {
36
+ var ref18, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, ref10, ref11, ref12, ref13;
37
+ var mappedAttributes = (ref18 = Object.keys(props === null || props === void 0 ? void 0 : props.attributes)) === null || ref18 === void 0 ? void 0 : ref18.reduce(function(map, key) {
37
38
  var current = props === null || props === void 0 ? void 0 : props.attributes[key];
38
39
  var ref;
39
40
  map[key] = {
@@ -54,7 +55,7 @@ export var useLineItem = function(props) {
54
55
  }
55
56
  return map;
56
57
  }, {});
57
- var ref13, ref14, ref15;
58
+ var ref14, ref15, ref16;
58
59
  return {
59
60
  _id: props === null || props === void 0 ? void 0 : props._id,
60
61
  name: getTranslatableField(props === null || props === void 0 ? void 0 : props.name),
@@ -62,18 +63,21 @@ export var useLineItem = function(props) {
62
63
  name: getTranslatableField(props === null || props === void 0 ? void 0 : (ref2 = props.brands) === null || ref2 === void 0 ? void 0 : (ref3 = ref2[0]) === null || ref3 === void 0 ? void 0 : ref3.name),
63
64
  slug: getTranslatableField(props === null || props === void 0 ? void 0 : (ref4 = props.brands) === null || ref4 === void 0 ? void 0 : (ref5 = ref4[0]) === null || ref5 === void 0 ? void 0 : ref5.slug)
64
65
  },
66
+ attributes: props === null || props === void 0 ? void 0 : props.attributes,
67
+ variantAttributes: (ref6 = props) === null || ref6 === void 0 ? void 0 : ref6.variantAttributes,
68
+ variants: props === null || props === void 0 ? void 0 : props.variants,
65
69
  slug: getTranslatableField(props === null || props === void 0 ? void 0 : props.slug),
66
70
  collapseKey: props === null || props === void 0 ? void 0 : props.collapseKey,
67
71
  variantId: props === null || props === void 0 ? void 0 : props.variantId,
68
- offerId: props === null || props === void 0 ? void 0 : (ref6 = props.offer) === null || ref6 === void 0 ? void 0 : ref6._id,
72
+ offerId: props === null || props === void 0 ? void 0 : (ref7 = props.offer) === null || ref7 === void 0 ? void 0 : ref7._id,
69
73
  offer: props === null || props === void 0 ? void 0 : props.offer,
70
74
  sku: props === null || props === void 0 ? void 0 : props.sku,
71
75
  images: (props === null || props === void 0 ? void 0 : props.images) || {},
72
76
  ratings: props === null || props === void 0 ? void 0 : props.reviewRatingStatistics,
73
- price: props === null || props === void 0 ? void 0 : (ref7 = props.offer) === null || ref7 === void 0 ? void 0 : ref7.originalPrice,
74
- discountedPrice: props === null || props === void 0 ? void 0 : (ref8 = props.offer) === null || ref8 === void 0 ? void 0 : ref8.discountedPrice,
75
- variantOptions: (ref13 = props === null || props === void 0 ? void 0 : props.variantOptions) !== null && ref13 !== void 0 ? ref13 : 0,
76
- variantsCount: (ref14 = props === null || props === void 0 ? void 0 : props.variantsCount) !== null && ref14 !== void 0 ? ref14 : 0,
77
+ price: props === null || props === void 0 ? void 0 : (ref8 = props.offer) === null || ref8 === void 0 ? void 0 : ref8.originalPrice,
78
+ discountedPrice: props === null || props === void 0 ? void 0 : (ref9 = props.offer) === null || ref9 === void 0 ? void 0 : ref9.discountedPrice,
79
+ variantOptions: (ref14 = props === null || props === void 0 ? void 0 : props.variantOptions) !== null && ref14 !== void 0 ? ref14 : 0,
80
+ variantsCount: (ref15 = props === null || props === void 0 ? void 0 : props.variantsCount) !== null && ref15 !== void 0 ? ref15 : 0,
77
81
  categories: props === null || props === void 0 ? void 0 : props.categories.map(function(category) {
78
82
  return {
79
83
  name: getTranslatableField(category.name),
@@ -81,9 +85,9 @@ export var useLineItem = function(props) {
81
85
  };
82
86
  }),
83
87
  inventory: {
84
- hasAvailableQuantity: (props === null || props === void 0 ? void 0 : (ref9 = props.inventorySnapshot) === null || ref9 === void 0 ? void 0 : ref9.canOrder) ? true : false,
85
- availableQuantity: (props === null || props === void 0 ? void 0 : (ref10 = props.inventorySnapshot) === null || ref10 === void 0 ? void 0 : ref10.canOrder) ? (ref15 = props === null || props === void 0 ? void 0 : (ref11 = props.inventorySnapshot) === null || ref11 === void 0 ? void 0 : ref11.availableQuantity) !== null && ref15 !== void 0 ? ref15 : 0 : 0,
86
- maxQuantity: props === null || props === void 0 ? void 0 : (ref12 = props.offer) === null || ref12 === void 0 ? void 0 : ref12.maxQuantity
88
+ hasAvailableQuantity: (props === null || props === void 0 ? void 0 : (ref10 = props.inventorySnapshot) === null || ref10 === void 0 ? void 0 : ref10.canOrder) ? true : false,
89
+ availableQuantity: (props === null || props === void 0 ? void 0 : (ref11 = props.inventorySnapshot) === null || ref11 === void 0 ? void 0 : ref11.canOrder) ? (ref16 = props === null || props === void 0 ? void 0 : (ref12 = props.inventorySnapshot) === null || ref12 === void 0 ? void 0 : ref12.availableQuantity) !== null && ref16 !== void 0 ? ref16 : 0 : 0,
90
+ maxQuantity: props === null || props === void 0 ? void 0 : (ref13 = props.offer) === null || ref13 === void 0 ? void 0 : ref13.maxQuantity
87
91
  },
88
92
  labels: mapLabelData(props === null || props === void 0 ? void 0 : props.labels),
89
93
  getCustomFieldByKey: function(key) {
@@ -1,281 +1,46 @@
1
1
  import React from 'react';
2
2
  import { getTranslatableField } from '../../';
3
- import { assignCombinationsToAttributeRules, createCombinationsMap, createCustomFieldsMap, createProductContent, createAttributeRulesMap, createMappedAttributes, processAvailabilityPerStore, getProductVideos, createMappedOptions, createSelectedOption2, formatCategories, createLabelsMap, createCollapseKeyImagesMap } from './utils';
3
+ import { createProductContent, getProductVideos, formatCategories, createCollapseKeyImagesMap } from './utils';
4
4
  import { productsApi } from '../../api/productsApi';
5
5
  import { assetsFieldsToMap } from '../../utils/assetsToMap';
6
- function _arrayLikeToArray(arr, len) {
7
- if (len == null || len > arr.length) len = arr.length;
8
- for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
9
- return arr2;
10
- }
11
- function _arrayWithHoles(arr) {
12
- if (Array.isArray(arr)) return arr;
13
- }
14
- function _arrayWithoutHoles(arr) {
15
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
16
- }
17
- function _defineProperty(obj, key, value) {
18
- if (key in obj) {
19
- Object.defineProperty(obj, key, {
20
- value: value,
21
- enumerable: true,
22
- configurable: true,
23
- writable: true
24
- });
25
- } else {
26
- obj[key] = value;
27
- }
28
- return obj;
29
- }
30
- function _iterableToArray(iter) {
31
- if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
32
- }
33
- function _iterableToArrayLimit(arr, i) {
34
- var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
35
- if (_i == null) return;
36
- var _arr = [];
37
- var _n = true;
38
- var _d = false;
39
- var _s, _e;
40
- try {
41
- for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
42
- _arr.push(_s.value);
43
- if (i && _arr.length === i) break;
44
- }
45
- } catch (err) {
46
- _d = true;
47
- _e = err;
48
- } finally{
49
- try {
50
- if (!_n && _i["return"] != null) _i["return"]();
51
- } finally{
52
- if (_d) throw _e;
53
- }
54
- }
55
- return _arr;
56
- }
57
- function _nonIterableRest() {
58
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
59
- }
60
- function _nonIterableSpread() {
61
- throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
62
- }
63
- function _objectSpread(target) {
64
- for(var i = 1; i < arguments.length; i++){
65
- var source = arguments[i] != null ? arguments[i] : {};
66
- var ownKeys = Object.keys(source);
67
- if (typeof Object.getOwnPropertySymbols === "function") {
68
- ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
69
- return Object.getOwnPropertyDescriptor(source, sym).enumerable;
70
- }));
71
- }
72
- ownKeys.forEach(function(key) {
73
- _defineProperty(target, key, source[key]);
74
- });
75
- }
76
- return target;
77
- }
78
- function _slicedToArray(arr, i) {
79
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
80
- }
81
- function _toConsumableArray(arr) {
82
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
83
- }
84
- function _unsupportedIterableToArray(o, minLen) {
85
- if (!o) return;
86
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
87
- var n = Object.prototype.toString.call(o).slice(8, -1);
88
- if (n === "Object" && o.constructor) n = o.constructor.name;
89
- if (n === "Map" || n === "Set") return Array.from(n);
90
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
91
- }
6
+ import { useProductVariants } from '../useProductVariants';
92
7
  var ProductStateContext = /*#__PURE__*/ React.createContext(undefined);
93
8
  var CountDispatchContext = /*#__PURE__*/ React.createContext(undefined);
94
9
  export function ProductProvider(param) {
95
10
  var product = param.product, children = param.children, defaultVariantId = param.defaultVariantId, _includeBrandInName = param.includeBrandInName, includeBrandInName = _includeBrandInName === void 0 ? false : _includeBrandInName;
96
- var getVariantsWithInventory = function getVariantsWithInventory() {
97
- var ref9;
98
- var variantsWithInventory = product === null || product === void 0 ? void 0 : (ref9 = product.variants) === null || ref9 === void 0 ? void 0 : ref9.reduce(function(map, _, index, arr) {
99
- var ref10, ref6, ref7, ref8;
100
- var currentVariant = arr[index];
101
- var mappedAttributes = createMappedAttributes(currentVariant);
102
- var mappedOptions = createMappedOptions(currentVariant === null || currentVariant === void 0 ? void 0 : currentVariant.availableOptions) || [];
103
- var isAvailable = currentVariant === null || currentVariant === void 0 ? void 0 : (ref10 = currentVariant.inventorySnapshot) === null || ref10 === void 0 ? void 0 : ref10.canOrder;
104
- var updatedVariant = _objectSpread({}, currentVariant, {
105
- inventory: {
106
- hasAvailableQuantity: (ref6 = currentVariant.inventorySnapshot) === null || ref6 === void 0 ? void 0 : ref6.canOrder,
107
- availableQuantity: (ref7 = currentVariant.inventorySnapshot) === null || ref7 === void 0 ? void 0 : ref7.availableQuantity
108
- },
109
- isAvailable: isAvailable ? true : false,
110
- quantity: isAvailable ? currentVariant === null || currentVariant === void 0 ? void 0 : (ref8 = currentVariant.inventorySnapshot) === null || ref8 === void 0 ? void 0 : ref8.availableQuantity : 0,
111
- attributes: mappedAttributes,
112
- options: mappedOptions,
113
- selectedOptions: [],
114
- labels: createLabelsMap(currentVariant.labels),
115
- getAttribute: function(rule) {
116
- return getSelectedAttribute(rule, mappedAttributes);
117
- },
118
- getAttributeByKey: function(key) {
119
- var ref;
120
- return (ref = mappedAttributes.find(function(a) {
121
- return a.name === key;
122
- })) === null || ref === void 0 ? void 0 : ref.value;
123
- },
124
- getAvailabilityByStore: function() {
125
- var ref;
126
- return processAvailabilityPerStore(currentVariant === null || currentVariant === void 0 ? void 0 : (ref = currentVariant.inventorySnapshot) === null || ref === void 0 ? void 0 : ref.availabilities);
127
- }
128
- });
129
- map[currentVariant._id] = updatedVariant;
130
- return map;
131
- }, {});
132
- return variantsWithInventory || {};
133
- };
134
- var getCombinationsAndAttributeRules = function getCombinationsAndAttributeRules() {
135
- var mappedAttributeRules = createAttributeRulesMap(product);
136
- var attributesWithId = Object.keys(variants || {}).map(function(key) {
137
- return {
138
- variantId: variants === null || variants === void 0 ? void 0 : variants[key]._id,
139
- attributes: variants === null || variants === void 0 ? void 0 : variants[key].attributes
140
- };
141
- });
142
- var combinationsMap = createCombinationsMap(Object.keys(mappedAttributeRules), attributesWithId);
143
- assignCombinationsToAttributeRules(mappedAttributeRules, combinationsMap);
144
- return {
145
- combinations: combinationsMap,
146
- attributeRules: mappedAttributeRules
147
- };
148
- };
149
- var getDefaultVariant = function getDefaultVariant() {
150
- if (!defaultVariantId) return null;
151
- return variants[defaultVariantId];
152
- };
153
- var selectVariantById = function selectVariantById(id) {
154
- setSelectedVariant(variants[id]);
155
- };
156
- var selectVariantByAttribute = function selectVariantByAttribute(key) {
157
- var ref;
158
- // Get by collapseKey for SD
159
- var split = key.split(':');
160
- if (split[0] === 'collapseKey') {
161
- var id = Object.keys(variants).find(function(el) {
162
- return variants[el].collapseKey === split[1];
163
- });
164
- return setSelectedVariant(variants[id]);
165
- }
166
- var id1 = (ref = combinations[key]) === null || ref === void 0 ? void 0 : ref.value;
167
- setSelectedVariant(variants[id1]);
168
- };
169
- var selectVariantByCombination = function selectVariantByCombination(key, value) {
170
- // Take collapseKey for SD
171
- var split = value.split(':');
172
- if (split[0] === 'collapseKey') {
173
- var id = Object.keys(variants).find(function(el) {
174
- var ref;
175
- return variants[el].collapseKey === split[1] && ((ref = variants[el].attributes.find(function(attr) {
176
- return attr.name === 'size';
177
- })) === null || ref === void 0 ? void 0 : ref.value) === key;
178
- });
179
- return setSelectedVariant(variants[id]);
180
- }
181
- var id2 = combinations[key].params[value];
182
- setSelectedVariant(variants[id2]);
183
- };
184
- var selectVariantOptions = function selectVariantOptions(definitionKey, optionKey) {
185
- if (selectedVariant) {
186
- var newSelectedVariant = _objectSpread({}, selectedVariant);
187
- newSelectedVariant.selectedOptions = newSelectedVariant.selectedOptions.filter(function(opt) {
188
- return opt.definitionKey !== definitionKey;
189
- });
190
- newSelectedVariant.selectedOptions = _toConsumableArray(newSelectedVariant.selectedOptions).concat([
191
- {
192
- definitionKey: definitionKey,
193
- optionKey: optionKey
194
- },
195
- ]);
196
- newSelectedVariant.mappedSelectedOptions = newSelectedVariant.selectedOptions.map(function(option) {
197
- return createSelectedOption2(newSelectedVariant.options, option);
198
- });
199
- setSelectedVariant(newSelectedVariant);
200
- }
201
- };
202
- var getSelectedAttribute = function getSelectedAttribute(rule, attributes) {
203
- var ref;
204
- var attributeRule = (ref = attributeRules[rule]) === null || ref === void 0 ? void 0 : ref.values;
205
- var result;
206
- attributes.forEach(function(currentKey) {
207
- var found = attributeRule === null || attributeRule === void 0 ? void 0 : attributeRule[currentKey.value];
208
- if (found) result = _objectSpread({
209
- id: currentKey.value,
210
- name: rule
211
- }, found);
212
- });
213
- return result;
214
- };
215
- var getVariantById = function getVariantById(id) {
216
- return variants === null || variants === void 0 ? void 0 : variants[id];
217
- };
218
- var getVariantByAttribute = function getVariantByAttribute(key) {
219
- var ref;
220
- var id = (ref = combinations[key]) === null || ref === void 0 ? void 0 : ref.value;
221
- return variants === null || variants === void 0 ? void 0 : variants[id];
222
- };
223
- var getVariantByCombination = function getVariantByCombination(key, value) {
224
- var ref11;
225
- var split = value === null || value === void 0 ? void 0 : value.split(':');
226
- if ((split === null || split === void 0 ? void 0 : split[0]) === 'collapseKey') {
227
- var id = Object.keys(variants).find(function(el) {
228
- var ref;
229
- return variants[el].collapseKey === (split === null || split === void 0 ? void 0 : split[1]) && ((ref = variants[el].attributes.find(function(attr) {
230
- return attr.name === 'size';
231
- })) === null || ref === void 0 ? void 0 : ref.value) === key;
232
- });
233
- return variants[id];
234
- }
235
- var id3 = (ref11 = combinations[key]) === null || ref11 === void 0 ? void 0 : ref11.params[value];
236
- return variants === null || variants === void 0 ? void 0 : variants[id3];
237
- };
238
- var getCustomFieldValue = function getCustomFieldValue(field) {
239
- return customFields === null || customFields === void 0 ? void 0 : customFields[field];
240
- };
241
- var getCombinationsForAttributeRule = function getCombinationsForAttributeRule(key, id) {
242
- var ref, ref12;
243
- var result = attributeRules === null || attributeRules === void 0 ? void 0 : (ref = attributeRules[key]) === null || ref === void 0 ? void 0 : (ref12 = ref.values) === null || ref12 === void 0 ? void 0 : ref12[id];
244
- return result;
245
- };
246
- var ref5 = _slicedToArray(React.useState(false), 2), mounted = ref5[0], setMounted = ref5[1];
247
- var ref1 = _slicedToArray(React.useState(getCombinationsAndAttributeRules), 2), ref2 = ref1[0], combinations = ref2.combinations, attributeRules = ref2.attributeRules, setCombinationsAndAttributeRules = ref1[1];
248
- var ref3 = _slicedToArray(React.useState(getVariantsWithInventory), 2), variants = ref3[0], setVariants = ref3[1];
249
- var ref4 = _slicedToArray(React.useState(getDefaultVariant), 2), selectedVariant = ref4[0], setSelectedVariant = ref4[1];
11
+ var ref38 = useProductVariants({
12
+ product: product,
13
+ defaultVariantId: defaultVariantId
14
+ }), selectedVariant = ref38.selectedVariant, attributeRules = ref38.attributeRules, variants = ref38.variants, getVariantById = ref38.getVariantById, getVariantByAttribute = ref38.getVariantByAttribute, getVariantByCombination = ref38.getVariantByCombination, getCustomFieldValue = ref38.getCustomFieldValue, getCombinationsForAttributeRule = ref38.getCombinationsForAttributeRule, selectVariantById = ref38.selectVariantById, selectVariantByAttribute = ref38.selectVariantByAttribute, selectVariantByCombination = ref38.selectVariantByCombination, selectVariantOptions = ref38.selectVariantOptions;
250
15
  var metaData = React.useMemo(function() {
251
- var ref, ref13, ref14, ref15, ref16, ref17, ref18, ref19, ref20, ref21, ref22, ref23, ref24, ref25, ref26, ref27, ref28, ref29, ref30, ref31, ref32, ref33, ref34, ref35, ref36, ref37, ref38, ref39, ref40, ref41, ref42, ref43, ref44, ref45, ref46, ref47, ref48, ref49;
16
+ var ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9, ref10, ref11, ref12, ref13, ref14, ref15, ref16, ref17, ref18, ref19, ref20, ref21, ref22, ref23, ref24, ref25, ref26, ref27, ref28, ref29, ref30, ref31, ref32, ref33, ref34, ref35, ref36, ref37;
252
17
  var rootCategory = product === null || product === void 0 ? void 0 : (ref = product.categories) === null || ref === void 0 ? void 0 : ref[0];
253
18
  var rootCategoryId = rootCategory === null || rootCategory === void 0 ? void 0 : rootCategory.resourceId;
254
19
  var metaObj = {
255
20
  _id: product === null || product === void 0 ? void 0 : product._id,
256
- name: includeBrandInName && (product === null || product === void 0 ? void 0 : (ref13 = product.brands) === null || ref13 === void 0 ? void 0 : (ref14 = ref13[0]) === null || ref14 === void 0 ? void 0 : (ref15 = ref14.resource) === null || ref15 === void 0 ? void 0 : ref15.name) ? "".concat(getTranslatableField(product === null || product === void 0 ? void 0 : (ref16 = product.brands) === null || ref16 === void 0 ? void 0 : (ref17 = ref16[0]) === null || ref17 === void 0 ? void 0 : (ref18 = ref17.resource) === null || ref18 === void 0 ? void 0 : ref18.name), " ").concat(getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.name) || getTranslatableField(product === null || product === void 0 ? void 0 : product.name)) : getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.name) || getTranslatableField(product === null || product === void 0 ? void 0 : product.name),
21
+ name: includeBrandInName && (product === null || product === void 0 ? void 0 : (ref1 = product.brands) === null || ref1 === void 0 ? void 0 : (ref2 = ref1[0]) === null || ref2 === void 0 ? void 0 : (ref3 = ref2.resource) === null || ref3 === void 0 ? void 0 : ref3.name) ? "".concat(getTranslatableField(product === null || product === void 0 ? void 0 : (ref4 = product.brands) === null || ref4 === void 0 ? void 0 : (ref5 = ref4[0]) === null || ref5 === void 0 ? void 0 : (ref6 = ref5.resource) === null || ref6 === void 0 ? void 0 : ref6.name), " ").concat(getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.name) || getTranslatableField(product === null || product === void 0 ? void 0 : product.name)) : getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.name) || getTranslatableField(product === null || product === void 0 ? void 0 : product.name),
257
22
  slug: getTranslatableField(product === null || product === void 0 ? void 0 : product.slug),
258
23
  description: getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.description) || getTranslatableField(product === null || product === void 0 ? void 0 : product.description),
259
24
  shortDescription: getTranslatableField(selectedVariant === null || selectedVariant === void 0 ? void 0 : selectedVariant.metaDescription) || getTranslatableField(product === null || product === void 0 ? void 0 : product.metaDescription),
260
25
  brand: {
261
- _id: product === null || product === void 0 ? void 0 : (ref19 = product.brands) === null || ref19 === void 0 ? void 0 : (ref20 = ref19[0]) === null || ref20 === void 0 ? void 0 : ref20.resourceId,
262
- name: getTranslatableField(product === null || product === void 0 ? void 0 : (ref21 = product.brands) === null || ref21 === void 0 ? void 0 : (ref22 = ref21[0]) === null || ref22 === void 0 ? void 0 : (ref23 = ref22.resource) === null || ref23 === void 0 ? void 0 : ref23.name),
263
- slug: getTranslatableField(product === null || product === void 0 ? void 0 : (ref24 = product.brands) === null || ref24 === void 0 ? void 0 : (ref25 = ref24[0]) === null || ref25 === void 0 ? void 0 : (ref26 = ref25.resource) === null || ref26 === void 0 ? void 0 : ref26.slug),
264
- logo: product === null || product === void 0 ? void 0 : (ref27 = product.brands) === null || ref27 === void 0 ? void 0 : (ref28 = ref27[0]) === null || ref28 === void 0 ? void 0 : (ref29 = ref28.resource) === null || ref29 === void 0 ? void 0 : (ref30 = ref29.assets) === null || ref30 === void 0 ? void 0 : (ref31 = ref30[0]) === null || ref31 === void 0 ? void 0 : (ref32 = ref31.sources) === null || ref32 === void 0 ? void 0 : (ref33 = ref32[0]) === null || ref33 === void 0 ? void 0 : ref33.url
26
+ _id: product === null || product === void 0 ? void 0 : (ref7 = product.brands) === null || ref7 === void 0 ? void 0 : (ref8 = ref7[0]) === null || ref8 === void 0 ? void 0 : ref8.resourceId,
27
+ name: getTranslatableField(product === null || product === void 0 ? void 0 : (ref9 = product.brands) === null || ref9 === void 0 ? void 0 : (ref10 = ref9[0]) === null || ref10 === void 0 ? void 0 : (ref11 = ref10.resource) === null || ref11 === void 0 ? void 0 : ref11.name),
28
+ slug: getTranslatableField(product === null || product === void 0 ? void 0 : (ref12 = product.brands) === null || ref12 === void 0 ? void 0 : (ref13 = ref12[0]) === null || ref13 === void 0 ? void 0 : (ref14 = ref13.resource) === null || ref14 === void 0 ? void 0 : ref14.slug),
29
+ logo: product === null || product === void 0 ? void 0 : (ref15 = product.brands) === null || ref15 === void 0 ? void 0 : (ref16 = ref15[0]) === null || ref16 === void 0 ? void 0 : (ref17 = ref16.resource) === null || ref17 === void 0 ? void 0 : (ref18 = ref17.assets) === null || ref18 === void 0 ? void 0 : (ref19 = ref18[0]) === null || ref19 === void 0 ? void 0 : (ref20 = ref19.sources) === null || ref20 === void 0 ? void 0 : (ref21 = ref20[0]) === null || ref21 === void 0 ? void 0 : ref21.url
265
30
  },
266
31
  category: {
267
- _id: product === null || product === void 0 ? void 0 : (ref35 = product.categories) === null || ref35 === void 0 ? void 0 : (ref36 = ref35[(product === null || product === void 0 ? void 0 : (ref34 = product.categories) === null || ref34 === void 0 ? void 0 : ref34.length) - 1]) === null || ref36 === void 0 ? void 0 : ref36.resourceId,
268
- name: getTranslatableField(product === null || product === void 0 ? void 0 : (ref38 = product.categories) === null || ref38 === void 0 ? void 0 : (ref39 = ref38[(product === null || product === void 0 ? void 0 : (ref37 = product.categories) === null || ref37 === void 0 ? void 0 : ref37.length) - 1]) === null || ref39 === void 0 ? void 0 : (ref40 = ref39.resource) === null || ref40 === void 0 ? void 0 : ref40.name),
269
- slug: getTranslatableField(product === null || product === void 0 ? void 0 : (ref42 = product.categories) === null || ref42 === void 0 ? void 0 : (ref43 = ref42[(product === null || product === void 0 ? void 0 : (ref41 = product.categories) === null || ref41 === void 0 ? void 0 : ref41.length) - 1]) === null || ref43 === void 0 ? void 0 : (ref44 = ref43.resource) === null || ref44 === void 0 ? void 0 : ref44.slug)
32
+ _id: product === null || product === void 0 ? void 0 : (ref23 = product.categories) === null || ref23 === void 0 ? void 0 : (ref24 = ref23[(product === null || product === void 0 ? void 0 : (ref22 = product.categories) === null || ref22 === void 0 ? void 0 : ref22.length) - 1]) === null || ref24 === void 0 ? void 0 : ref24.resourceId,
33
+ name: getTranslatableField(product === null || product === void 0 ? void 0 : (ref26 = product.categories) === null || ref26 === void 0 ? void 0 : (ref27 = ref26[(product === null || product === void 0 ? void 0 : (ref25 = product.categories) === null || ref25 === void 0 ? void 0 : ref25.length) - 1]) === null || ref27 === void 0 ? void 0 : (ref28 = ref27.resource) === null || ref28 === void 0 ? void 0 : ref28.name),
34
+ slug: getTranslatableField(product === null || product === void 0 ? void 0 : (ref30 = product.categories) === null || ref30 === void 0 ? void 0 : (ref31 = ref30[(product === null || product === void 0 ? void 0 : (ref29 = product.categories) === null || ref29 === void 0 ? void 0 : ref29.length) - 1]) === null || ref31 === void 0 ? void 0 : (ref32 = ref31.resource) === null || ref32 === void 0 ? void 0 : ref32.slug)
270
35
  },
271
36
  rootCategory: {
272
37
  _id: rootCategoryId,
273
- name: getTranslatableField(rootCategory === null || rootCategory === void 0 ? void 0 : (ref45 = rootCategory.resource) === null || ref45 === void 0 ? void 0 : ref45.name),
274
- slug: getTranslatableField(rootCategory === null || rootCategory === void 0 ? void 0 : (ref46 = rootCategory.resource) === null || ref46 === void 0 ? void 0 : ref46.slug)
38
+ name: getTranslatableField(rootCategory === null || rootCategory === void 0 ? void 0 : (ref33 = rootCategory.resource) === null || ref33 === void 0 ? void 0 : ref33.name),
39
+ slug: getTranslatableField(rootCategory === null || rootCategory === void 0 ? void 0 : (ref34 = rootCategory.resource) === null || ref34 === void 0 ? void 0 : ref34.slug)
275
40
  },
276
41
  categories: formatCategories(product === null || product === void 0 ? void 0 : product.categories, rootCategoryId),
277
42
  reviewRatingStatistics: product === null || product === void 0 ? void 0 : product.reviewRatingStatistics,
278
- content: Boolean(selectedVariant === null || selectedVariant === void 0 ? void 0 : (ref47 = selectedVariant.content) === null || ref47 === void 0 ? void 0 : ref47.map(createProductContent).length) ? selectedVariant === null || selectedVariant === void 0 ? void 0 : (ref48 = selectedVariant.content) === null || ref48 === void 0 ? void 0 : ref48.map(createProductContent) : product === null || product === void 0 ? void 0 : (ref49 = product.content) === null || ref49 === void 0 ? void 0 : ref49.map(createProductContent),
43
+ content: Boolean(selectedVariant === null || selectedVariant === void 0 ? void 0 : (ref35 = selectedVariant.content) === null || ref35 === void 0 ? void 0 : ref35.map(createProductContent).length) ? selectedVariant === null || selectedVariant === void 0 ? void 0 : (ref36 = selectedVariant.content) === null || ref36 === void 0 ? void 0 : ref36.map(createProductContent) : product === null || product === void 0 ? void 0 : (ref37 = product.content) === null || ref37 === void 0 ? void 0 : ref37.map(createProductContent),
279
44
  assets: assetsFieldsToMap(product === null || product === void 0 ? void 0 : product.assets)
280
45
  };
281
46
  return metaObj;
@@ -303,30 +68,6 @@ export function ProductProvider(param) {
303
68
  product === null || product === void 0 ? void 0 : product.metaTitle,
304
69
  selectedVariant,
305
70
  ]);
306
- var customFields = React.useMemo(function() {
307
- var mappedFields = createCustomFieldsMap(product === null || product === void 0 ? void 0 : product.custom);
308
- return mappedFields;
309
- }, [
310
- product === null || product === void 0 ? void 0 : product.custom
311
- ]);
312
- React.useEffect(function() {
313
- // prevent unnecessary calculations
314
- if (mounted) {
315
- setVariants(getVariantsWithInventory());
316
- }
317
- }, [
318
- product
319
- ]);
320
- React.useMemo(function() {
321
- // prevent unnecessary calculations
322
- if (!mounted) {
323
- return setMounted(true);
324
- }
325
- var data = getCombinationsAndAttributeRules();
326
- setCombinationsAndAttributeRules(data);
327
- }, [
328
- variants
329
- ]);
330
71
  var collapsedImages = createCollapseKeyImagesMap(product);
331
72
  var state = {
332
73
  metaData: metaData,