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