@sentecacommerce-theme/lib 0.12.102 → 0.13.3

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 (51) hide show
  1. package/dist/cjs/basket/api/mutations/index.js +14 -0
  2. package/dist/cjs/basket/api/mutations/useChangeGiftVariant.js +116 -0
  3. package/dist/cjs/basket/hooks/index.js +12 -0
  4. package/dist/cjs/basket/hooks/useChangeGiftVariant.js +16 -0
  5. package/dist/cjs/hooks/useLineItem/LineItemContext.js +86 -0
  6. package/dist/cjs/hooks/useLineItem/index.js +34 -12
  7. package/dist/cjs/hooks/useProduct/index.js +17 -276
  8. package/dist/cjs/hooks/useProduct/utils/index.js +44 -18
  9. package/dist/cjs/hooks/useProductVariants/index.js +334 -0
  10. package/dist/cjs/hooks/useProductVariants/types.js +3 -0
  11. package/dist/cjs/index.js +16 -4
  12. package/dist/cjs/listing/api/queries/useListingQuery.js +1 -0
  13. package/dist/cjs/listing/hooks/useListingBreadcrumbs.js +20 -8
  14. package/dist/cjs/listing/hooks/useListingCategories.js +2 -2
  15. package/dist/cjs/listing/hooks/useListingMeta.js +12 -10
  16. package/dist/cjs/listing/utils/buildCategoryQuery.js +22 -1
  17. package/dist/cjs/seo/components/TranslationsMeta/index.js +1 -0
  18. package/dist/cjs/utils/localStorage.js +7 -2
  19. package/dist/esm/basket/api/mutations/index.js +1 -0
  20. package/dist/esm/basket/api/mutations/useChangeGiftVariant.js +106 -0
  21. package/dist/esm/basket/hooks/index.js +1 -0
  22. package/dist/esm/basket/hooks/useChangeGiftVariant.js +3 -0
  23. package/dist/esm/hooks/useLineItem/LineItemContext.js +75 -0
  24. package/dist/esm/hooks/useLineItem/index.js +16 -12
  25. package/dist/esm/hooks/useProduct/index.js +18 -277
  26. package/dist/esm/hooks/useProduct/utils/index.js +44 -18
  27. package/dist/esm/hooks/useProductVariants/index.js +293 -0
  28. package/dist/esm/hooks/useProductVariants/types.js +1 -0
  29. package/dist/esm/index.js +1 -0
  30. package/dist/esm/listing/api/queries/useListingQuery.js +1 -0
  31. package/dist/esm/listing/hooks/useListingBreadcrumbs.js +15 -3
  32. package/dist/esm/listing/hooks/useListingCategories.js +2 -2
  33. package/dist/esm/listing/hooks/useListingMeta.js +12 -12
  34. package/dist/esm/listing/utils/buildCategoryQuery.js +22 -1
  35. package/dist/esm/seo/components/TranslationsMeta/index.js +1 -0
  36. package/dist/esm/utils/localStorage.js +7 -2
  37. package/dist/types/basket/api/mutations/index.d.ts +1 -0
  38. package/dist/types/basket/api/mutations/useChangeGiftVariant.d.ts +2 -0
  39. package/dist/types/basket/hooks/index.d.ts +1 -0
  40. package/dist/types/basket/hooks/useChangeGiftVariant.d.ts +2 -0
  41. package/dist/types/hooks/basket-hooks/useBasketLineItem/index.d.ts +1 -2
  42. package/dist/types/hooks/useLineItem/LineItemContext.d.ts +22 -0
  43. package/dist/types/hooks/useLineItem/index.d.ts +2 -2
  44. package/dist/types/hooks/useProduct/index.d.ts +2 -76
  45. package/dist/types/hooks/useProduct/utils/index.d.ts +6 -4
  46. package/dist/types/hooks/useProductVariants/index.d.ts +34 -0
  47. package/dist/types/hooks/useProductVariants/types.d.ts +79 -0
  48. package/dist/types/index.d.ts +1 -0
  49. package/dist/types/listing/hooks/useListingMeta.d.ts +4 -0
  50. package/dist/types/listing/types.d.ts +1 -0
  51. package/package.json +5 -5
@@ -133,8 +133,9 @@ export var createCollapseKeyImagesMap = function(product) {
133
133
  return mappedKeys;
134
134
  };
135
135
  export var createAttributeRulesMap = function(product) {
136
+ var isLineItem = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : false;
136
137
  var ref, ref1;
137
- var uniqueRules = product === null || product === void 0 ? void 0 : (ref = product.attributeRules) === null || ref === void 0 ? void 0 : ref.filter(function(x) {
138
+ var uniqueRules = isLineItem ? product === null || product === void 0 ? void 0 : product.attributeRules : product === null || product === void 0 ? void 0 : (ref = product.attributeRules) === null || ref === void 0 ? void 0 : ref.filter(function(x) {
138
139
  return x.constraint === AttributeRuleDTOConstraintEnum.CombinationUnique;
139
140
  });
140
141
  var reduceVariantsToAttributeRuleValues = function(map, attribute) {
@@ -158,7 +159,7 @@ export var createAttributeRulesMap = function(product) {
158
159
  return map;
159
160
  };
160
161
  var mappedAttributeRules = product === null || product === void 0 ? void 0 : (ref1 = product.variants) === null || ref1 === void 0 ? void 0 : ref1.map(function(v) {
161
- return v.attributes;
162
+ return isLineItem ? remapLineItemAttributes(v.attributes) : v.attributes;
162
163
  }).flat().reduce(reduceVariantsToAttributeRuleValues, {});
163
164
  return mappedAttributeRules;
164
165
  };
@@ -214,12 +215,13 @@ export var createSelectedOption = function(options, selectedOption) {
214
215
  };
215
216
  };
216
217
  export var createMappedAttributes = function(currentVariant) {
217
- var ref;
218
- var mappedAttributes = ((ref = currentVariant.attributes) === null || ref === void 0 ? void 0 : ref.reduce(function(attributeArray, attribute) {
218
+ var isLineItem = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : false;
219
+ var ref, ref2;
220
+ var mappedAttributes = ((ref2 = isLineItem ? (ref = currentVariant) === null || ref === void 0 ? void 0 : ref.variantAttributes : currentVariant === null || currentVariant === void 0 ? void 0 : currentVariant.attributes) === null || ref2 === void 0 ? void 0 : ref2.reduce(function(attributeArray, attribute) {
219
221
  attributeArray.push({
220
222
  name: attribute.name,
221
223
  value: attribute.value,
222
- valueData: attribute.valueData
224
+ valueData: isLineItem ? attribute : attribute.valueData
223
225
  });
224
226
  return attributeArray;
225
227
  }, [])) || [];
@@ -257,15 +259,15 @@ export var getProductVideos = function(assets) {
257
259
  return [];
258
260
  };
259
261
  export var formatCategories = function(categories, rootCategoryId) {
260
- var ref5;
262
+ var ref6;
261
263
  var results;
262
264
  if (categories === null || categories === void 0 ? void 0 : categories.some(function(cat) {
263
- var ref, ref4;
264
- return (ref = cat.resource) === null || ref === void 0 ? void 0 : (ref4 = ref.custom) === null || ref4 === void 0 ? void 0 : ref4.some(function(customField) {
265
+ var ref, ref5;
266
+ return (ref = cat.resource) === null || ref === void 0 ? void 0 : (ref5 = ref.custom) === null || ref5 === void 0 ? void 0 : ref5.some(function(customField) {
265
267
  return customField.field === 'isTemporary';
266
268
  });
267
269
  })) {
268
- var ref2;
270
+ var ref3;
269
271
  var cats = categories.reduce(function(result, category) {
270
272
  result[category.resourceId] = _objectSpread({}, category.resource, {
271
273
  hasChildCategories: false
@@ -285,8 +287,8 @@ export var formatCategories = function(categories, rootCategoryId) {
285
287
  ]);
286
288
  return result;
287
289
  }, {});
288
- var ref3;
289
- var mainPaths = (ref2 = Object.values(cats).filter(function(category) {
290
+ var ref4;
291
+ var mainPaths = (ref3 = Object.values(cats).filter(function(category) {
290
292
  return category.hasChildCategories === false;
291
293
  }).map(function(category) {
292
294
  return paths1 === null || paths1 === void 0 ? void 0 : paths1[category._id];
@@ -296,22 +298,22 @@ export var formatCategories = function(categories, rootCategoryId) {
296
298
  });
297
299
  }).filter(function(paths) {
298
300
  return !paths.some(function(path) {
299
- var ref, ref6;
300
- return (ref3 = (ref6 = path === null || path === void 0 ? void 0 : (ref = path.custom) === null || ref === void 0 ? void 0 : ref.find(function(custom) {
301
+ var ref, ref7;
302
+ return (ref4 = (ref7 = path === null || path === void 0 ? void 0 : (ref = path.custom) === null || ref === void 0 ? void 0 : ref.find(function(custom) {
301
303
  return custom.field === 'isTemporary';
302
- })) === null || ref6 === void 0 ? void 0 : ref6.value) !== null && ref3 !== void 0 ? ref3 : false;
304
+ })) === null || ref7 === void 0 ? void 0 : ref7.value) !== null && ref4 !== void 0 ? ref4 : false;
303
305
  });
304
- })) === null || ref2 === void 0 ? void 0 : ref2[0].flat();
306
+ })) === null || ref3 === void 0 ? void 0 : ref3[0].flat();
305
307
  results = mainPaths;
306
308
  } else {
307
309
  results = categories === null || categories === void 0 ? void 0 : categories.filter(function(cat) {
308
- var ref, ref7;
309
- return (cat === null || cat === void 0 ? void 0 : cat.resourceId) === rootCategoryId || (cat === null || cat === void 0 ? void 0 : (ref = cat.resource) === null || ref === void 0 ? void 0 : (ref7 = ref.ancestors) === null || ref7 === void 0 ? void 0 : ref7.some(function(ancestor) {
310
+ var ref, ref8;
311
+ return (cat === null || cat === void 0 ? void 0 : cat.resourceId) === rootCategoryId || (cat === null || cat === void 0 ? void 0 : (ref = cat.resource) === null || ref === void 0 ? void 0 : (ref8 = ref.ancestors) === null || ref8 === void 0 ? void 0 : ref8.some(function(ancestor) {
310
312
  return ancestor.resource === rootCategoryId;
311
313
  }));
312
314
  });
313
315
  }
314
- var formattedResults = (ref5 = results) === null || ref5 === void 0 ? void 0 : ref5.map(function(category) {
316
+ var formattedResults = (ref6 = results) === null || ref6 === void 0 ? void 0 : ref6.map(function(category) {
315
317
  if (category.resource) {
316
318
  var resource = category.resource;
317
319
  return {
@@ -343,3 +345,27 @@ export var createLabelsMap = function(data) {
343
345
  return el.key;
344
346
  });
345
347
  };
348
+ // object of objects to array of objects remapping for attributes of line items from catalog search api
349
+ export function remapLineItemAttributes(attributes) {
350
+ var arr = [];
351
+ Object.keys(attributes).forEach(function(key) {
352
+ // handles the remapping for lineItem.variantAttributes or lineItem[index].variantAttributes
353
+ if (Array.isArray(attributes[key])) {
354
+ attributes[key].forEach(function(attr, index) {
355
+ arr.push(_objectSpread({
356
+ attributeName: key,
357
+ name: key,
358
+ data: attributes[key][index],
359
+ valueData: attributes[key][index]
360
+ }, attributes[key][index]));
361
+ });
362
+ } else {
363
+ arr.push({
364
+ name: key,
365
+ data: attributes[key][0],
366
+ value: attributes[key][0]
367
+ });
368
+ }
369
+ });
370
+ return arr;
371
+ }
@@ -0,0 +1,293 @@
1
+ import React from 'react';
2
+ import { assignCombinationsToAttributeRules, createCombinationsMap, createAttributeRulesMap, createMappedAttributes, processAvailabilityPerStore, createMappedOptions, createSelectedOption2, createLabelsMap, createCustomFieldsMap, remapLineItemAttributes } from '../useProduct/utils';
3
+ function _arrayLikeToArray(arr, len) {
4
+ if (len == null || len > arr.length) len = arr.length;
5
+ for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
6
+ return arr2;
7
+ }
8
+ function _arrayWithHoles(arr) {
9
+ if (Array.isArray(arr)) return arr;
10
+ }
11
+ function _arrayWithoutHoles(arr) {
12
+ if (Array.isArray(arr)) return _arrayLikeToArray(arr);
13
+ }
14
+ function _defineProperty(obj, key, value) {
15
+ if (key in obj) {
16
+ Object.defineProperty(obj, key, {
17
+ value: value,
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true
21
+ });
22
+ } else {
23
+ obj[key] = value;
24
+ }
25
+ return obj;
26
+ }
27
+ function _iterableToArray(iter) {
28
+ if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
29
+ }
30
+ function _iterableToArrayLimit(arr, i) {
31
+ var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
32
+ if (_i == null) return;
33
+ var _arr = [];
34
+ var _n = true;
35
+ var _d = false;
36
+ var _s, _e;
37
+ try {
38
+ for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
39
+ _arr.push(_s.value);
40
+ if (i && _arr.length === i) break;
41
+ }
42
+ } catch (err) {
43
+ _d = true;
44
+ _e = err;
45
+ } finally{
46
+ try {
47
+ if (!_n && _i["return"] != null) _i["return"]();
48
+ } finally{
49
+ if (_d) throw _e;
50
+ }
51
+ }
52
+ return _arr;
53
+ }
54
+ function _nonIterableRest() {
55
+ throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
56
+ }
57
+ function _nonIterableSpread() {
58
+ throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
59
+ }
60
+ function _objectSpread(target) {
61
+ for(var i = 1; i < arguments.length; i++){
62
+ var source = arguments[i] != null ? arguments[i] : {};
63
+ var ownKeys = Object.keys(source);
64
+ if (typeof Object.getOwnPropertySymbols === "function") {
65
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
66
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
67
+ }));
68
+ }
69
+ ownKeys.forEach(function(key) {
70
+ _defineProperty(target, key, source[key]);
71
+ });
72
+ }
73
+ return target;
74
+ }
75
+ function _slicedToArray(arr, i) {
76
+ return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
77
+ }
78
+ function _toConsumableArray(arr) {
79
+ return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
80
+ }
81
+ function _unsupportedIterableToArray(o, minLen) {
82
+ if (!o) return;
83
+ if (typeof o === "string") return _arrayLikeToArray(o, minLen);
84
+ var n = Object.prototype.toString.call(o).slice(8, -1);
85
+ if (n === "Object" && o.constructor) n = o.constructor.name;
86
+ if (n === "Map" || n === "Set") return Array.from(n);
87
+ if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
88
+ }
89
+ export * from './types';
90
+ export var useProductVariants = function(param) {
91
+ var product = param.product, defaultVariantId = param.defaultVariantId, _isLineItem = param.isLineItem, isLineItem = _isLineItem === void 0 ? false : _isLineItem;
92
+ var getVariantsWithInventory = function getVariantsWithInventory() {
93
+ var ref9;
94
+ if (isLineItem) {
95
+ var innerProduct = JSON.parse(JSON.stringify(product));
96
+ delete innerProduct.variants;
97
+ product.variants.unshift(innerProduct);
98
+ }
99
+ var variantsWithInventory = product === null || product === void 0 ? void 0 : (ref9 = product.variants) === null || ref9 === void 0 ? void 0 : ref9.reduce(function(map, _, index, arr) {
100
+ var ref10, ref5, ref6, ref7, ref8;
101
+ var currentVariant = arr[index];
102
+ if (isLineItem && !Array.isArray((ref10 = currentVariant) === null || ref10 === void 0 ? void 0 : ref10.variantAttributes)) {
103
+ currentVariant.variantAttributes = remapLineItemAttributes(currentVariant.variantAttributes);
104
+ }
105
+ var mappedAttributes = createMappedAttributes(currentVariant, isLineItem);
106
+ var mappedOptions = createMappedOptions(currentVariant === null || currentVariant === void 0 ? void 0 : currentVariant.availableOptions) || [];
107
+ var isAvailable = currentVariant === null || currentVariant === void 0 ? void 0 : (ref5 = currentVariant.inventorySnapshot) === null || ref5 === void 0 ? void 0 : ref5.canOrder;
108
+ var updatedVariant = _objectSpread({}, currentVariant, {
109
+ inventory: {
110
+ hasAvailableQuantity: (ref6 = currentVariant.inventorySnapshot) === null || ref6 === void 0 ? void 0 : ref6.canOrder,
111
+ availableQuantity: (ref7 = currentVariant.inventorySnapshot) === null || ref7 === void 0 ? void 0 : ref7.availableQuantity
112
+ },
113
+ isAvailable: isAvailable ? true : false,
114
+ quantity: isAvailable ? currentVariant === null || currentVariant === void 0 ? void 0 : (ref8 = currentVariant.inventorySnapshot) === null || ref8 === void 0 ? void 0 : ref8.availableQuantity : 0,
115
+ attributes: mappedAttributes,
116
+ options: mappedOptions,
117
+ selectedOptions: [],
118
+ labels: createLabelsMap(currentVariant.labels),
119
+ getAttribute: function(rule) {
120
+ return getSelectedAttribute(rule, mappedAttributes);
121
+ },
122
+ getAttributeByKey: function(key) {
123
+ var ref;
124
+ return (ref = mappedAttributes.find(function(a) {
125
+ return a.name === key;
126
+ })) === null || ref === void 0 ? void 0 : ref.value;
127
+ },
128
+ getAvailabilityByStore: function() {
129
+ var ref;
130
+ return processAvailabilityPerStore(currentVariant === null || currentVariant === void 0 ? void 0 : (ref = currentVariant.inventorySnapshot) === null || ref === void 0 ? void 0 : ref.availabilities);
131
+ }
132
+ });
133
+ map[isLineItem ? currentVariant.variantId : currentVariant._id] = updatedVariant;
134
+ return map;
135
+ }, {});
136
+ return variantsWithInventory || {};
137
+ };
138
+ var getCombinationsAndAttributeRules = function getCombinationsAndAttributeRules() {
139
+ if (isLineItem && !(product === null || product === void 0 ? void 0 : product.attributeRules)) {
140
+ product.attributeRules = remapLineItemAttributes(product === null || product === void 0 ? void 0 : product.attributes);
141
+ }
142
+ var mappedAttributeRules = createAttributeRulesMap(product, isLineItem);
143
+ var attributesWithId = Object.keys(variants || {}).map(function(key) {
144
+ return {
145
+ variantId: isLineItem ? (variants === null || variants === void 0 ? void 0 : variants[key]).variantId : variants === null || variants === void 0 ? void 0 : variants[key]._id,
146
+ attributes: isLineItem && !Array.isArray(variants === null || variants === void 0 ? void 0 : variants[key].attributes) ? remapLineItemAttributes(variants === null || variants === void 0 ? void 0 : variants[key].attributes) : variants === null || variants === void 0 ? void 0 : variants[key].attributes
147
+ };
148
+ });
149
+ var combinationsMap = createCombinationsMap(Object.keys(mappedAttributeRules), attributesWithId);
150
+ assignCombinationsToAttributeRules(mappedAttributeRules, combinationsMap);
151
+ return {
152
+ combinations: combinationsMap,
153
+ attributeRules: mappedAttributeRules
154
+ };
155
+ };
156
+ var getDefaultVariant = function getDefaultVariant() {
157
+ if (!defaultVariantId) return null;
158
+ return variants[defaultVariantId];
159
+ };
160
+ var selectVariantById = function selectVariantById(id) {
161
+ setSelectedVariant(variants[id]);
162
+ };
163
+ var selectVariantByCombination = function selectVariantByCombination(key, value) {
164
+ // Take collapseKey for SD
165
+ var split = value.split(':');
166
+ if (split[0] === 'collapseKey') {
167
+ var id = Object.keys(variants).find(function(el) {
168
+ var ref;
169
+ return variants[el].collapseKey === split[1] && ((ref = variants[el].attributes.find(function(attr) {
170
+ return attr.name === 'size';
171
+ })) === null || ref === void 0 ? void 0 : ref.value) === key;
172
+ });
173
+ return setSelectedVariant(variants[id]);
174
+ }
175
+ var id1 = combinations[key].params[value];
176
+ setSelectedVariant(variants[id1]);
177
+ };
178
+ var selectVariantByAttribute = function selectVariantByAttribute(key) {
179
+ var ref;
180
+ // Get by collapseKey for SD
181
+ var split = key.split(':');
182
+ if (split[0] === 'collapseKey') {
183
+ var id = Object.keys(variants).find(function(el) {
184
+ return variants[el].collapseKey === split[1];
185
+ });
186
+ return setSelectedVariant(variants[id]);
187
+ }
188
+ var id2 = (ref = combinations[key]) === null || ref === void 0 ? void 0 : ref.value;
189
+ setSelectedVariant(variants[id2]);
190
+ };
191
+ var getVariantByCombination = function getVariantByCombination(key, value) {
192
+ var ref11;
193
+ var split = value === null || value === void 0 ? void 0 : value.split(':');
194
+ if ((split === null || split === void 0 ? void 0 : split[0]) === 'collapseKey') {
195
+ var id = Object.keys(variants).find(function(el) {
196
+ var ref;
197
+ return variants[el].collapseKey === (split === null || split === void 0 ? void 0 : split[1]) && ((ref = variants[el].attributes.find(function(attr) {
198
+ return attr.name === 'size';
199
+ })) === null || ref === void 0 ? void 0 : ref.value) === key;
200
+ });
201
+ return variants[id];
202
+ }
203
+ var id3 = (ref11 = combinations[key]) === null || ref11 === void 0 ? void 0 : ref11.params[value];
204
+ return variants === null || variants === void 0 ? void 0 : variants[id3];
205
+ };
206
+ var getSelectedAttribute = function getSelectedAttribute(rule, attributes) {
207
+ var ref;
208
+ var attributeRule = (ref = attributeRules[rule]) === null || ref === void 0 ? void 0 : ref.values;
209
+ var result;
210
+ attributes.forEach(function(currentKey) {
211
+ var found = attributeRule === null || attributeRule === void 0 ? void 0 : attributeRule[currentKey.value];
212
+ if (found) result = _objectSpread({
213
+ id: currentKey.value,
214
+ name: rule
215
+ }, found);
216
+ });
217
+ return result;
218
+ };
219
+ var getVariantById = function getVariantById(id) {
220
+ return variants === null || variants === void 0 ? void 0 : variants[id];
221
+ };
222
+ var getVariantByAttribute = function getVariantByAttribute(key) {
223
+ var ref;
224
+ var id = (ref = combinations[key]) === null || ref === void 0 ? void 0 : ref.value;
225
+ return variants === null || variants === void 0 ? void 0 : variants[id];
226
+ };
227
+ var getCombinationsForAttributeRule = function getCombinationsForAttributeRule(key, id) {
228
+ var ref, ref12;
229
+ 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];
230
+ return result;
231
+ };
232
+ var selectVariantOptions = function selectVariantOptions(definitionKey, optionKey) {
233
+ if (selectedVariant) {
234
+ var newSelectedVariant = _objectSpread({}, selectedVariant);
235
+ newSelectedVariant.selectedOptions = newSelectedVariant.selectedOptions.filter(function(opt) {
236
+ return opt.definitionKey !== definitionKey;
237
+ });
238
+ newSelectedVariant.selectedOptions = _toConsumableArray(newSelectedVariant.selectedOptions).concat([
239
+ {
240
+ definitionKey: definitionKey,
241
+ optionKey: optionKey
242
+ },
243
+ ]);
244
+ newSelectedVariant.mappedSelectedOptions = newSelectedVariant.selectedOptions.map(function(option) {
245
+ return createSelectedOption2(newSelectedVariant.options, option);
246
+ });
247
+ setSelectedVariant(newSelectedVariant);
248
+ }
249
+ };
250
+ var getCustomFieldValue = function getCustomFieldValue(field) {
251
+ return customFields === null || customFields === void 0 ? void 0 : customFields[field];
252
+ };
253
+ var ref4 = _slicedToArray(React.useState(getCombinationsAndAttributeRules), 2), ref1 = ref4[0], combinations = ref1.combinations, attributeRules = ref1.attributeRules, setCombinationsAndAttributeRules = ref4[1];
254
+ var ref2 = _slicedToArray(React.useState(getVariantsWithInventory), 2), variants = ref2[0], setVariants = ref2[1];
255
+ var ref3 = _slicedToArray(React.useState(getDefaultVariant), 2), selectedVariant = ref3[0], setSelectedVariant = ref3[1];
256
+ React.useEffect(function() {
257
+ setVariants(getVariantsWithInventory());
258
+ }, [
259
+ product
260
+ ]);
261
+ React.useMemo(function() {
262
+ if (Object.keys(variants).length) {
263
+ var data = getCombinationsAndAttributeRules();
264
+ setCombinationsAndAttributeRules(data);
265
+ }
266
+ }, [
267
+ variants
268
+ ]);
269
+ var customFields = React.useMemo(function() {
270
+ var mappedFields = createCustomFieldsMap(product === null || product === void 0 ? void 0 : product.custom);
271
+ return mappedFields;
272
+ }, [
273
+ product === null || product === void 0 ? void 0 : product.custom
274
+ ]);
275
+ return {
276
+ combinations: combinations,
277
+ attributeRules: attributeRules,
278
+ variants: variants,
279
+ selectedVariant: selectedVariant,
280
+ setSelectedVariant: setSelectedVariant,
281
+ customFields: customFields,
282
+ getVariantByAttribute: getVariantByAttribute,
283
+ getVariantById: getVariantById,
284
+ getCombinationsAndAttributeRules: getCombinationsAndAttributeRules,
285
+ getCombinationsForAttributeRule: getCombinationsForAttributeRule,
286
+ getVariantByCombination: getVariantByCombination,
287
+ getCustomFieldValue: getCustomFieldValue,
288
+ selectVariantByAttribute: selectVariantByAttribute,
289
+ selectVariantByCombination: selectVariantByCombination,
290
+ selectVariantOptions: selectVariantOptions,
291
+ selectVariantById: selectVariantById
292
+ };
293
+ };
@@ -0,0 +1 @@
1
+ export { };
package/dist/esm/index.js CHANGED
@@ -92,6 +92,7 @@ export * from './hooks/useRouterRedirect';
92
92
  export * from './hooks/useProgressBar';
93
93
  export * from './hooks/useCatalogDiscounts';
94
94
  export * from './hooks/useLocalRating';
95
+ export * from './hooks/useProductVariants/types';
95
96
  // Utils
96
97
  export * from './utils';
97
98
  export * from './types';
@@ -143,6 +143,7 @@ export var useListingQuery = function() {
143
143
  postFilter: postFilter,
144
144
  offset: offset,
145
145
  config: config.meta,
146
+ includeVariants: config.includeVariants,
146
147
  slug: aliasedSlug
147
148
  });
148
149
  case 2:
@@ -1,7 +1,7 @@
1
1
  import React from 'react';
2
2
  import { useRouter } from 'next/router';
3
3
  import { usePredicate } from '../api';
4
- import { useListingConfig } from './';
4
+ import { useListingCategories, useListingConfig, getCategoryName, getFilterByType } from './index';
5
5
  import { useListingAlias } from '../../index';
6
6
  import { typeToSuffix } from '../../utils/typeToSuffix';
7
7
  import { getTranslatableField } from '../../utils/getTranslatableField';
@@ -33,10 +33,13 @@ function _unsupportedIterableToArray(o, minLen) {
33
33
  }
34
34
  var _instance;
35
35
  export var useListingBreadcrumbs = function() {
36
- var ref1 = usePredicate(), data = ref1.data, isLoading = ref1.isLoading;
36
+ var ref3 = usePredicate(), data = ref3.data, isLoading = ref3.isLoading;
37
37
  var config = useListingConfig();
38
38
  var router = useRouter();
39
39
  var slug = useListingAlias(config.meta).slug;
40
+ var ref1 = useListingCategories(), categoriesData = ref1.data;
41
+ var categoryName = getCategoryName(router, categoriesData);
42
+ var categoryId = getFilterByType('category', router);
40
43
  var result = React.useMemo(function() {
41
44
  if (config.type === ListingTypesEnum.AttrPage) {
42
45
  var ref;
@@ -57,7 +60,16 @@ export var useListingBreadcrumbs = function() {
57
60
  },
58
61
  ];
59
62
  }
60
- return createCatalogBreadcrumb(data, config.type, router);
63
+ var breadcrumbData = createCatalogBreadcrumb(data, config.type, router);
64
+ if (config.type === ListingTypesEnum.View || config.type === ListingTypesEnum.Brand) {
65
+ var ref2;
66
+ breadcrumbData.push({
67
+ name: categoryName || '',
68
+ id: categoryId,
69
+ link: processLink((ref2 = data) === null || ref2 === void 0 ? void 0 : ref2.slug, config.type, router.query.slug) + applyFilters(router)
70
+ });
71
+ }
72
+ return breadcrumbData;
61
73
  }, [
62
74
  data,
63
75
  config.type,
@@ -92,14 +92,14 @@ function generateUrl(current, router, slug, config) {
92
92
  res = "/".concat(router.query.slug, "/").concat(getTranslatableField(current.meta.slug), "/").concat(suffix);
93
93
  }
94
94
  if (pageType === ListingTypesEnum.View) {
95
- res = "/".concat(router.query.slug, "/").concat(getTranslatableField(current.meta.slug), "/").concat(suffix);
95
+ res = "/".concat(router.query.slug, "/").concat(suffix);
96
96
  }
97
97
  if (pageType === ListingTypesEnum.Search) {
98
98
  var _q;
99
99
  res = "/".concat(getTranslatableField(current.meta.slug), "/c?search=").concat((_q = router.query.q) !== null && _q !== void 0 ? _q : router.query.search);
100
100
  }
101
101
  var filter = router.query.filter;
102
- if (suffix === 'b') {
102
+ if (suffix === 'b' || suffix === 'v') {
103
103
  if (filter) {
104
104
  if (Array.isArray(filter)) {
105
105
  filter = _toConsumableArray(filter);
@@ -106,7 +106,7 @@ export var useListingMeta = function() {
106
106
  return result;
107
107
  };
108
108
  function processDisplayName(slug, config, name, breadcrumb, router, aggData, categoriesData) {
109
- var ref, ref5;
109
+ var ref, ref5, ref6;
110
110
  var listingName = getTranslatableField(name);
111
111
  var brandName = getBrandName(router, aggData);
112
112
  var categoryName = getCategoryName(router, categoriesData);
@@ -120,45 +120,45 @@ function processDisplayName(slug, config, name, breadcrumb, router, aggData, cat
120
120
  defaultName: listingName,
121
121
  attribute: (ref = config.titleAliases) === null || ref === void 0 ? void 0 : ref[slug],
122
122
  childAttribute: getTranslatableField(breadcrumb === null || breadcrumb === void 0 ? void 0 : breadcrumb.label),
123
- search: router === null || router === void 0 ? void 0 : (ref5 = router.query) === null || ref5 === void 0 ? void 0 : ref5.search,
123
+ search: (router === null || router === void 0 ? void 0 : (ref5 = router.query) === null || ref5 === void 0 ? void 0 : ref5.q) || (router === null || router === void 0 ? void 0 : (ref6 = router.query) === null || ref6 === void 0 ? void 0 : ref6.search),
124
124
  isWithAttribute: Boolean(router === null || router === void 0 ? void 0 : router.query.cat)
125
125
  });
126
126
  return title || listingName;
127
127
  }
128
- function getFilterByType(filterType, router) {
128
+ export function getFilterByType(filterType, router) {
129
129
  var filterId = '';
130
130
  if (Array.isArray(router === null || router === void 0 ? void 0 : router.query.filter)) {
131
- var ref, ref6;
131
+ var ref, ref7;
132
132
  var filters = (ref = router === null || router === void 0 ? void 0 : router.query.filter) === null || ref === void 0 ? void 0 : ref.filter(function(x) {
133
133
  return x === null || x === void 0 ? void 0 : x.includes(filterType);
134
134
  });
135
135
  if ((filters === null || filters === void 0 ? void 0 : filters.length) && filters.length > 1) {
136
136
  return '';
137
137
  }
138
- var filterTerm = (ref6 = /\d+/.exec((filters === null || filters === void 0 ? void 0 : filters[0]) || '')) === null || ref6 === void 0 ? void 0 : ref6[0];
138
+ var filterTerm = (ref7 = /\d+/.exec((filters === null || filters === void 0 ? void 0 : filters[0]) || '')) === null || ref7 === void 0 ? void 0 : ref7[0];
139
139
  if (!filterTerm) return '';
140
140
  filterId = filterTerm;
141
141
  } else {
142
- var ref7, ref8, ref9;
143
- var isFiltered = router === null || router === void 0 ? void 0 : (ref7 = router.query) === null || ref7 === void 0 ? void 0 : (ref8 = ref7.filter) === null || ref8 === void 0 ? void 0 : ref8.includes(filterType);
142
+ var ref8, ref9, ref10;
143
+ var isFiltered = router === null || router === void 0 ? void 0 : (ref8 = router.query) === null || ref8 === void 0 ? void 0 : (ref9 = ref8.filter) === null || ref9 === void 0 ? void 0 : ref9.includes(filterType);
144
144
  if (!isFiltered) {
145
145
  return '';
146
146
  }
147
- filterId = (ref9 = /(\w+)-(\d+)/.exec(router === null || router === void 0 ? void 0 : router.query.filter)) === null || ref9 === void 0 ? void 0 : ref9[0];
147
+ filterId = (ref10 = /(\w+)-(\d+)/.exec(router === null || router === void 0 ? void 0 : router.query.filter)) === null || ref10 === void 0 ? void 0 : ref10[0];
148
148
  }
149
149
  return filterId;
150
150
  }
151
151
  function getBrandName(router, filtersData) {
152
- var ref, ref10, ref11;
152
+ var ref, ref11, ref12;
153
153
  var brandId = getFilterByType('brand', router);
154
154
  var brand = (ref = filtersData === null || filtersData === void 0 ? void 0 : filtersData.find(function(x) {
155
155
  return x.name === 'Brands';
156
- })) === null || ref === void 0 ? void 0 : (ref10 = ref.items) === null || ref10 === void 0 ? void 0 : ref10.find(function(x) {
156
+ })) === null || ref === void 0 ? void 0 : (ref11 = ref.items) === null || ref11 === void 0 ? void 0 : ref11.find(function(x) {
157
157
  return x.data.key === brandId;
158
158
  });
159
- return getTranslatableField(brand === null || brand === void 0 ? void 0 : (ref11 = brand.meta) === null || ref11 === void 0 ? void 0 : ref11.name);
159
+ return getTranslatableField(brand === null || brand === void 0 ? void 0 : (ref12 = brand.meta) === null || ref12 === void 0 ? void 0 : ref12.name);
160
160
  }
161
- function getCategoryName(router, categories) {
161
+ export function getCategoryName(router, categories) {
162
162
  var categoryId = getFilterByType('category', router);
163
163
  if (!categoryId || !categories) return;
164
164
  var stack = _toConsumableArray(categories);
@@ -60,7 +60,7 @@ export var buildCategoryQuery = function(param) {
60
60
  filter += (ref = config === null || config === void 0 ? void 0 : config.filterExtension) !== null && ref !== void 0 ? ref : '';
61
61
  }
62
62
  if (postFilter) {
63
- if (router.query.cat) filter += '&';
63
+ if (filter) filter += '&';
64
64
  filter += postFilter;
65
65
  }
66
66
  break;
@@ -77,6 +77,14 @@ export var buildCategoryQuery = function(param) {
77
77
  filter += "text;eq;\"".concat(searchQuery, "\"");
78
78
  if (postFilter) filter += "&".concat(postFilter);
79
79
  break;
80
+ case ListingTypesEnum.View:
81
+ filter += applyViewsPostFilter({
82
+ postFilter: postFilter
83
+ });
84
+ if (config === null || config === void 0 ? void 0 : config.filterExtension) {
85
+ filter += filter ? "&".concat(config === null || config === void 0 ? void 0 : config.filterExtension) : config === null || config === void 0 ? void 0 : config.filterExtension;
86
+ }
87
+ break;
80
88
  default:
81
89
  return "".concat((config === null || config === void 0 ? void 0 : config.filterExtension) + "".concat(postFilter !== null && postFilter !== void 0 ? postFilter : ''));
82
90
  }
@@ -108,3 +116,16 @@ var applyBrandsPostFilter = function(param) {
108
116
  }
109
117
  return '';
110
118
  };
119
+ var applyViewsPostFilter = function(param) {
120
+ var postFilter = param.postFilter;
121
+ if (postFilter) {
122
+ var filtersArr = postFilter.split('&');
123
+ var filters = filtersArr.reduce(function(res, val, i) {
124
+ if (val.includes('category')) return res;
125
+ if (!res) return res + "".concat(val);
126
+ return res += "&".concat(val);
127
+ }, '');
128
+ return "".concat(filters);
129
+ }
130
+ return '';
131
+ };
@@ -22,6 +22,7 @@ export var TranslationsMeta = function() {
22
22
  var defaultTranslation = translationsMap[defaultLocale];
23
23
  var isError = router.route === '/404' || router.route === '/_error';
24
24
  if (isError) return null;
25
+ if ((allLocales === null || allLocales === void 0 ? void 0 : allLocales.length) < 2) return null;
25
26
  var domain = defaultTranslation === null || defaultTranslation === void 0 ? void 0 : defaultTranslation.domain;
26
27
  return(/*#__PURE__*/ React.createElement(Head, null, allLocales === null || allLocales === void 0 ? void 0 : allLocales.map(function(locale) {
27
28
  var translation = translationsMap === null || translationsMap === void 0 ? void 0 : translationsMap[locale];
@@ -1,10 +1,15 @@
1
1
  export var readFromLocalStorage = function(key) {
2
2
  if (typeof window !== 'undefined') {
3
- return JSON.parse(localStorage.getItem(key));
3
+ try {
4
+ return JSON.parse(localStorage.getItem(key));
5
+ } catch (e) {
6
+ console.log(e);
7
+ return null;
8
+ }
4
9
  }
5
10
  };
6
11
  export var writeToLocalStorage = function(key, data) {
7
- if (typeof window !== 'undefined') {
12
+ if (typeof window !== 'undefined' && data !== undefined) {
8
13
  localStorage.setItem(key, JSON.stringify(data));
9
14
  }
10
15
  };
@@ -1 +1,2 @@
1
1
  export * from './useReplaceBasketMutation';
2
+ export * from './useChangeGiftVariant';
@@ -0,0 +1,2 @@
1
+ import { OrderSelectableGiftVariantUpdateDTO, OrderDTO } from '@sentecacommerce/sdk';
2
+ export declare const useChangeGiftVariantMutation: () => import("react-query").MutationResultPair<OrderDTO, unknown, OrderSelectableGiftVariantUpdateDTO, unknown>;
@@ -1,2 +1,3 @@
1
1
  export * from './useReplaceBasket';
2
2
  export * from './useBasketCrossSaleItems';
3
+ export * from './useChangeGiftVariant';
@@ -0,0 +1,2 @@
1
+ import { useChangeGiftVariantMutation } from '../api/mutations';
2
+ export { useChangeGiftVariantMutation as useChangeGiftVariant };