@sentecacommerce-theme/lib 0.13.2 → 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.
- 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/seo/components/TranslationsMeta/index.js +1 -0
- 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/seo/components/TranslationsMeta/index.js +1 -0
- 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/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
|
});
|
@@ -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];
|
@@ -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
|
+
}
|