@searchspring/snap-store-mobx 0.53.4 → 0.54.0
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/Search/Stores/SearchResultStore.d.ts +17 -7
- package/dist/cjs/Search/Stores/SearchResultStore.d.ts.map +1 -1
- package/dist/cjs/Search/Stores/SearchResultStore.js +89 -41
- package/dist/cjs/types.d.ts +6 -2
- package/dist/cjs/types.d.ts.map +1 -1
- package/dist/esm/Search/Stores/SearchResultStore.d.ts +17 -7
- package/dist/esm/Search/Stores/SearchResultStore.d.ts.map +1 -1
- package/dist/esm/Search/Stores/SearchResultStore.js +86 -36
- package/dist/esm/types.d.ts +6 -2
- package/dist/esm/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { StoreServices, StoreConfigs, VariantSelectionOptions } from '../../types';
|
|
1
|
+
import type { StoreServices, StoreConfigs, VariantSelectionOptions, VariantConfig } from '../../types';
|
|
2
2
|
import type { SearchResponseModelResult, SearchResponseModelPagination, SearchResponseModelMerchandising, SearchResponseModelResultMappings, SearchResponseModelMerchandisingContentInline, SearchResponseModelMerchandisingContentConfig } from '@searchspring/snapi-types';
|
|
3
3
|
export declare class SearchResultStore extends Array<Product | Banner> {
|
|
4
4
|
static get [Symbol.species](): ArrayConstructor;
|
|
@@ -17,7 +17,11 @@ export declare class Banner {
|
|
|
17
17
|
export type VariantData = {
|
|
18
18
|
mappings: SearchResponseModelResultMappings;
|
|
19
19
|
attributes: Record<string, unknown>;
|
|
20
|
-
options: Record<string,
|
|
20
|
+
options: Record<string, {
|
|
21
|
+
value: string;
|
|
22
|
+
attributeId?: string;
|
|
23
|
+
optionId?: string;
|
|
24
|
+
}>;
|
|
21
25
|
};
|
|
22
26
|
type ProductMinimal = {
|
|
23
27
|
id: string;
|
|
@@ -49,9 +53,11 @@ export declare class Variants {
|
|
|
49
53
|
data: Variant[];
|
|
50
54
|
selections: VariantSelection[];
|
|
51
55
|
setActive: (variant: Variant) => void;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
update(
|
|
56
|
+
private config?;
|
|
57
|
+
constructor(variantData: VariantData[], mask: ProductMask, config?: VariantConfig);
|
|
58
|
+
update(variantData: VariantData[], config?: VariantConfig | undefined): void;
|
|
59
|
+
makeSelections(options?: Record<string, string[]>): void;
|
|
60
|
+
refineSelections(fromSelection: VariantSelection): void;
|
|
55
61
|
}
|
|
56
62
|
type SelectionValue = {
|
|
57
63
|
value: string;
|
|
@@ -68,7 +74,7 @@ export declare class VariantSelection {
|
|
|
68
74
|
values: SelectionValue[];
|
|
69
75
|
private variantsUpdate;
|
|
70
76
|
constructor(variants: Variants, selectorConfig: VariantSelectionOptions);
|
|
71
|
-
|
|
77
|
+
refineValues(variants: Variants): void;
|
|
72
78
|
reset(): void;
|
|
73
79
|
select(value: string, internalSelection?: boolean): void;
|
|
74
80
|
}
|
|
@@ -76,7 +82,11 @@ export declare class Variant {
|
|
|
76
82
|
type: string;
|
|
77
83
|
available: boolean;
|
|
78
84
|
attributes: Record<string, unknown>;
|
|
79
|
-
options: Record<string,
|
|
85
|
+
options: Record<string, {
|
|
86
|
+
value: string;
|
|
87
|
+
attributeId?: string;
|
|
88
|
+
optionId?: string;
|
|
89
|
+
}>;
|
|
80
90
|
mappings: SearchResponseModelResultMappings;
|
|
81
91
|
custom: {};
|
|
82
92
|
constructor(variantData: VariantData);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SearchResultStore.d.ts","sourceRoot":"","sources":["../../../../src/Search/Stores/SearchResultStore.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAqB,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"SearchResultStore.d.ts","sourceRoot":"","sources":["../../../../src/Search/Stores/SearchResultStore.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAqB,aAAa,EA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,EAAE,MAAM,CAAC;IACf,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,SAAS,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,qBAAa,gBAAgB;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,MAAM,CAAM;IACvB,kBAAkB,CAAC,EAAE,MAAM,CAAM;IACjC,MAAM,EAAE,cAAc,EAAE,CAAM;IAErC,OAAO,CAAC,cAAc,CAAa;gBAEvB,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,uBAAuB;IAgBhE,YAAY,CAAC,QAAQ,EAAE,QAAQ;IA4D/B,KAAK;IAKL,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,iBAAiB,UAAQ;CAYtD;AAED,qBAAa,OAAO;IACZ,IAAI,SAAa;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM;IACzC,OAAO,EAAE,MAAM,CACrB,MAAM,EACN;QACC,KAAK,EAAE,MAAM,CAAC;QACd,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;KAClB,CACD,CAAC;IAEK,QAAQ,EAAE,iCAAiC,CAEhD;IACK,MAAM,KAAM;gBAEP,WAAW,EAAE,WAAW;CAapC;AAED,cAAM,KAAK;IACH,IAAI,SAAW;IACf,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM;IACzC,MAAM,KAAM;gBAEP,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,yBAAyB;CAUtE"}
|
|
@@ -95,7 +95,7 @@ var Banner = /** @class */ (function () {
|
|
|
95
95
|
exports.Banner = Banner;
|
|
96
96
|
var Product = /** @class */ (function () {
|
|
97
97
|
function Product(services, result, config) {
|
|
98
|
-
var _a, _b, _c;
|
|
98
|
+
var _a, _b, _c, _d;
|
|
99
99
|
this.type = 'product';
|
|
100
100
|
this.attributes = {};
|
|
101
101
|
this.mappings = {
|
|
@@ -113,14 +113,14 @@ var Product = /** @class */ (function () {
|
|
|
113
113
|
try {
|
|
114
114
|
// parse the field (JSON)
|
|
115
115
|
var parsedVariants = JSON.parse(this.attributes[variantsField]);
|
|
116
|
-
this.variants = new Variants(parsedVariants, this.mask);
|
|
116
|
+
this.variants = new Variants(parsedVariants, this.mask, (_c = config.settings) === null || _c === void 0 ? void 0 : _c.variants);
|
|
117
117
|
}
|
|
118
118
|
catch (err) {
|
|
119
119
|
// failed to parse the variant JSON
|
|
120
120
|
console.error(err, "Invalid variant JSON for product id: ".concat(result.id));
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
-
if ((
|
|
123
|
+
if ((_d = result === null || result === void 0 ? void 0 : result.children) === null || _d === void 0 ? void 0 : _d.length) {
|
|
124
124
|
this.children = result.children.map(function (variant, index) {
|
|
125
125
|
return new Child(services, __assign({ id: "".concat(result.id, "-").concat(index) }, variant));
|
|
126
126
|
});
|
|
@@ -178,38 +178,59 @@ var ProductMask = /** @class */ (function () {
|
|
|
178
178
|
}());
|
|
179
179
|
exports.ProductMask = ProductMask;
|
|
180
180
|
var Variants = /** @class */ (function () {
|
|
181
|
-
function Variants(variantData, mask) {
|
|
181
|
+
function Variants(variantData, mask, config) {
|
|
182
182
|
var _this = this;
|
|
183
183
|
this.data = [];
|
|
184
184
|
this.selections = [];
|
|
185
|
-
var options = [];
|
|
186
|
-
// create variants objects
|
|
187
|
-
this.data = variantData.map(function (variant) {
|
|
188
|
-
Object.keys(variant.options).forEach(function (variantOption) {
|
|
189
|
-
if (!options.includes(variantOption)) {
|
|
190
|
-
options.push(variantOption);
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
return new Variant(variant);
|
|
194
|
-
});
|
|
195
|
-
options.map(function (option) {
|
|
196
|
-
// TODO - merge with variant config before constructing selection (for label overrides and swatch mappings)
|
|
197
|
-
var optionConfig = {
|
|
198
|
-
field: option,
|
|
199
|
-
label: option,
|
|
200
|
-
};
|
|
201
|
-
_this.selections.push(new VariantSelection(_this, optionConfig));
|
|
202
|
-
});
|
|
203
185
|
// setting function in constructor to prevent exposing mask as class property
|
|
204
186
|
this.setActive = function (variant) {
|
|
205
187
|
_this.active = variant;
|
|
206
188
|
mask.set({ mappings: _this.active.mappings, attributes: _this.active.attributes });
|
|
207
189
|
};
|
|
208
|
-
|
|
209
|
-
this.
|
|
190
|
+
this.config = config;
|
|
191
|
+
this.update(variantData, config);
|
|
210
192
|
}
|
|
193
|
+
Variants.prototype.update = function (variantData, config) {
|
|
194
|
+
var _this = this;
|
|
195
|
+
if (config === void 0) { config = this.config; }
|
|
196
|
+
try {
|
|
197
|
+
var options_1 = [];
|
|
198
|
+
// create variants objects
|
|
199
|
+
this.data = variantData.map(function (variant) {
|
|
200
|
+
Object.keys(variant.options).forEach(function (variantOption) {
|
|
201
|
+
if (!options_1.includes(variantOption)) {
|
|
202
|
+
options_1.push(variantOption);
|
|
203
|
+
}
|
|
204
|
+
});
|
|
205
|
+
return new Variant(variant);
|
|
206
|
+
});
|
|
207
|
+
//need to reset this.selections first
|
|
208
|
+
this.selections = [];
|
|
209
|
+
options_1.map(function (option) {
|
|
210
|
+
// TODO - merge with variant config before constructing selection (for label overrides and swatch mappings)
|
|
211
|
+
var optionConfig = {
|
|
212
|
+
field: option,
|
|
213
|
+
label: option,
|
|
214
|
+
};
|
|
215
|
+
_this.selections.push(new VariantSelection(_this, optionConfig));
|
|
216
|
+
});
|
|
217
|
+
var preselectedOptions_1 = {};
|
|
218
|
+
if (config === null || config === void 0 ? void 0 : config.options) {
|
|
219
|
+
Object.keys(config === null || config === void 0 ? void 0 : config.options).forEach(function (option) {
|
|
220
|
+
if (config.options[option].preSelected) {
|
|
221
|
+
preselectedOptions_1[option] = config.options[option].preSelected;
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
// select first available
|
|
226
|
+
this.makeSelections(preselectedOptions_1);
|
|
227
|
+
}
|
|
228
|
+
catch (err) {
|
|
229
|
+
// failed to parse the variant JSON
|
|
230
|
+
console.error(err, "Invalid variant JSON for: ".concat(variantData));
|
|
231
|
+
}
|
|
232
|
+
};
|
|
211
233
|
Variants.prototype.makeSelections = function (options) {
|
|
212
|
-
// TODO - support for affinity to attempt to pre-selected options
|
|
213
234
|
// options = {color: 'Blue', size: 'L'};
|
|
214
235
|
if (!options) {
|
|
215
236
|
// select first available for each selection
|
|
@@ -220,8 +241,40 @@ var Variants = /** @class */ (function () {
|
|
|
220
241
|
}
|
|
221
242
|
});
|
|
222
243
|
}
|
|
244
|
+
else {
|
|
245
|
+
this.selections.forEach(function (selection, idx) {
|
|
246
|
+
// filter by first available, then by preselected option preference
|
|
247
|
+
//make all options available for first selection.
|
|
248
|
+
var availableOptions = selection.values.filter(function (value) { return (idx == 0 ? true : value.available); });
|
|
249
|
+
var preferedOptions = options[selection.field];
|
|
250
|
+
var preferencedOption = availableOptions[0];
|
|
251
|
+
// if theres a preference for that field
|
|
252
|
+
if (preferedOptions) {
|
|
253
|
+
var checkIfAvailable_1 = function (preference) {
|
|
254
|
+
//see if that option is in the available options
|
|
255
|
+
var availablePreferedOptions = availableOptions.find(function (value) { return value.value.toLowerCase() == preference.toLowerCase(); });
|
|
256
|
+
//use it
|
|
257
|
+
if (availablePreferedOptions) {
|
|
258
|
+
preferencedOption = availablePreferedOptions;
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
if (Array.isArray(preferedOptions)) {
|
|
262
|
+
//loop through each preference option
|
|
263
|
+
preferedOptions.forEach(function (preference) {
|
|
264
|
+
checkIfAvailable_1(preference);
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
checkIfAvailable_1(preferedOptions);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (preferencedOption) {
|
|
272
|
+
selection.select(preferencedOption.value);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
}
|
|
223
276
|
};
|
|
224
|
-
Variants.prototype.
|
|
277
|
+
Variants.prototype.refineSelections = function (fromSelection) {
|
|
225
278
|
var _this = this;
|
|
226
279
|
// need to ensure the update originator is at the BOTTOM of the list for refinement
|
|
227
280
|
var orderedSelections = __spreadArray([], this.selections, true);
|
|
@@ -232,13 +285,13 @@ var Variants = /** @class */ (function () {
|
|
|
232
285
|
return -1;
|
|
233
286
|
});
|
|
234
287
|
// refine selections ensuring that the selection that triggered the update refines LAST
|
|
235
|
-
orderedSelections.forEach(function (selection) { return selection.
|
|
288
|
+
orderedSelections.forEach(function (selection) { return selection.refineValues(_this); });
|
|
236
289
|
// check to see if we have enough selections made to update the display
|
|
237
|
-
var selectedSelections = this.selections.filter(function (selection) {
|
|
290
|
+
var selectedSelections = this.selections.filter(function (selection) { return selection.selected; });
|
|
238
291
|
if (selectedSelections.length) {
|
|
239
292
|
var availableVariants = this.data;
|
|
240
293
|
var _loop_1 = function (selectedSelection) {
|
|
241
|
-
availableVariants = availableVariants.filter(function (variant) { return selectedSelection.selected == variant.options[selectedSelection.field] && variant.available; });
|
|
294
|
+
availableVariants = availableVariants.filter(function (variant) { return selectedSelection.selected == variant.options[selectedSelection.field].value && variant.available; });
|
|
242
295
|
};
|
|
243
296
|
// loop through selectedSelections and only include available products that match current selections
|
|
244
297
|
for (var _i = 0, selectedSelections_1 = selectedSelections; _i < selectedSelections_1.length; _i++) {
|
|
@@ -263,21 +316,21 @@ var VariantSelection = /** @class */ (function () {
|
|
|
263
316
|
this.field = selectorConfig.field;
|
|
264
317
|
this.label = selectorConfig.label;
|
|
265
318
|
// needed to prevent attaching variants as class property
|
|
266
|
-
this.variantsUpdate = function () { return variants.
|
|
319
|
+
this.variantsUpdate = function () { return variants.refineSelections(_this); };
|
|
267
320
|
// create possible values from the data and refine them
|
|
268
|
-
this.
|
|
321
|
+
this.refineValues(variants);
|
|
269
322
|
(0, mobx_1.makeObservable)(this, {
|
|
270
323
|
selected: mobx_1.observable,
|
|
271
324
|
values: mobx_1.observable,
|
|
272
325
|
});
|
|
273
326
|
}
|
|
274
|
-
VariantSelection.prototype.
|
|
327
|
+
VariantSelection.prototype.refineValues = function (variants) {
|
|
275
328
|
var _this = this;
|
|
276
329
|
// current selection should only consider OTHER selections for availability
|
|
277
330
|
var selectedSelections = variants.selections.filter(function (selection) { return selection.field != _this.field && selection.selected; });
|
|
278
331
|
var availableVariants = variants.data;
|
|
279
332
|
var _loop_2 = function (selectedSelection) {
|
|
280
|
-
availableVariants = availableVariants.filter(function (variant) { return selectedSelection.selected == variant.options[selectedSelection.field] && variant.available; });
|
|
333
|
+
availableVariants = availableVariants.filter(function (variant) { return selectedSelection.selected == variant.options[selectedSelection.field].value && variant.available; });
|
|
281
334
|
};
|
|
282
335
|
// loop through selectedSelections and remove products that do not match
|
|
283
336
|
for (var _i = 0, selectedSelections_2 = selectedSelections; _i < selectedSelections_2.length; _i++) {
|
|
@@ -288,15 +341,11 @@ var VariantSelection = /** @class */ (function () {
|
|
|
288
341
|
.filter(function (variant) { return variant.options[_this.field]; })
|
|
289
342
|
.reduce(function (values, variant) {
|
|
290
343
|
var _a;
|
|
291
|
-
if (!values.some(function (val) { return variant.options[_this.field] == val.value; })) {
|
|
292
|
-
values.push({
|
|
293
|
-
value: variant.options[_this.field],
|
|
294
|
-
label: variant.options[_this.field],
|
|
344
|
+
if (!values.some(function (val) { return variant.options[_this.field].value == val.value; })) {
|
|
345
|
+
values.push(__assign({ label: variant.options[_this.field].value,
|
|
295
346
|
// TODO: use configurable mappings from config
|
|
296
347
|
// TODO: set background for swatches (via configurable mappings) from config
|
|
297
|
-
thumbnailImageUrl: (_a = variant.mappings.core) === null || _a === void 0 ? void 0 : _a.thumbnailImageUrl,
|
|
298
|
-
available: Boolean(availableVariants.some(function (availableVariant) { return availableVariant.options[_this.field] == variant.options[_this.field]; })),
|
|
299
|
-
});
|
|
348
|
+
thumbnailImageUrl: (_a = variant.mappings.core) === null || _a === void 0 ? void 0 : _a.thumbnailImageUrl, available: Boolean(availableVariants.some(function (availableVariant) { return availableVariant.options[_this.field].value == variant.options[_this.field].value; })) }, variant.options[_this.field]));
|
|
300
349
|
}
|
|
301
350
|
// TODO: use sorting function from config
|
|
302
351
|
return values;
|
|
@@ -347,7 +396,6 @@ var Variant = /** @class */ (function () {
|
|
|
347
396
|
function Variant(variantData) {
|
|
348
397
|
this.type = 'variant';
|
|
349
398
|
this.attributes = {};
|
|
350
|
-
this.options = {};
|
|
351
399
|
this.mappings = {
|
|
352
400
|
core: {},
|
|
353
401
|
};
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -4,8 +4,13 @@ export type StoreConfig = {
|
|
|
4
4
|
id: string;
|
|
5
5
|
[any: string]: unknown;
|
|
6
6
|
};
|
|
7
|
-
type VariantConfig = {
|
|
7
|
+
export type VariantConfig = {
|
|
8
8
|
field: string;
|
|
9
|
+
options?: {
|
|
10
|
+
[field: string]: {
|
|
11
|
+
preSelected?: string[];
|
|
12
|
+
};
|
|
13
|
+
};
|
|
9
14
|
};
|
|
10
15
|
export type VariantSelectionOptions = {
|
|
11
16
|
field: string;
|
|
@@ -120,5 +125,4 @@ export type SelectedSelection = {
|
|
|
120
125
|
export type FinderStoreState = {
|
|
121
126
|
persisted: boolean;
|
|
122
127
|
};
|
|
123
|
-
export {};
|
|
124
128
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/cjs/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,KAAK,EAAE,wCAAwC,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAExI,MAAM,MAAM,WAAW,GAAG;IACzB,EAA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,MAAM,CAAC;IACjB,IAAI,EAAE,wCAAwC,EAAE,CAAC;IACjD,KAAK,EAAE,GAAG,CAAC;CACX,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC9B,SAAS,EAAE,OAAO,CAAC;CACnB,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { StoreServices, StoreConfigs, VariantSelectionOptions } from '../../types';
|
|
1
|
+
import type { StoreServices, StoreConfigs, VariantSelectionOptions, VariantConfig } from '../../types';
|
|
2
2
|
import type { SearchResponseModelResult, SearchResponseModelPagination, SearchResponseModelMerchandising, SearchResponseModelResultMappings, SearchResponseModelMerchandisingContentInline, SearchResponseModelMerchandisingContentConfig } from '@searchspring/snapi-types';
|
|
3
3
|
export declare class SearchResultStore extends Array<Product | Banner> {
|
|
4
4
|
static get [Symbol.species](): ArrayConstructor;
|
|
@@ -17,7 +17,11 @@ export declare class Banner {
|
|
|
17
17
|
export type VariantData = {
|
|
18
18
|
mappings: SearchResponseModelResultMappings;
|
|
19
19
|
attributes: Record<string, unknown>;
|
|
20
|
-
options: Record<string,
|
|
20
|
+
options: Record<string, {
|
|
21
|
+
value: string;
|
|
22
|
+
attributeId?: string;
|
|
23
|
+
optionId?: string;
|
|
24
|
+
}>;
|
|
21
25
|
};
|
|
22
26
|
type ProductMinimal = {
|
|
23
27
|
id: string;
|
|
@@ -49,9 +53,11 @@ export declare class Variants {
|
|
|
49
53
|
data: Variant[];
|
|
50
54
|
selections: VariantSelection[];
|
|
51
55
|
setActive: (variant: Variant) => void;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
update(
|
|
56
|
+
private config?;
|
|
57
|
+
constructor(variantData: VariantData[], mask: ProductMask, config?: VariantConfig);
|
|
58
|
+
update(variantData: VariantData[], config?: VariantConfig | undefined): void;
|
|
59
|
+
makeSelections(options?: Record<string, string[]>): void;
|
|
60
|
+
refineSelections(fromSelection: VariantSelection): void;
|
|
55
61
|
}
|
|
56
62
|
type SelectionValue = {
|
|
57
63
|
value: string;
|
|
@@ -68,7 +74,7 @@ export declare class VariantSelection {
|
|
|
68
74
|
values: SelectionValue[];
|
|
69
75
|
private variantsUpdate;
|
|
70
76
|
constructor(variants: Variants, selectorConfig: VariantSelectionOptions);
|
|
71
|
-
|
|
77
|
+
refineValues(variants: Variants): void;
|
|
72
78
|
reset(): void;
|
|
73
79
|
select(value: string, internalSelection?: boolean): void;
|
|
74
80
|
}
|
|
@@ -76,7 +82,11 @@ export declare class Variant {
|
|
|
76
82
|
type: string;
|
|
77
83
|
available: boolean;
|
|
78
84
|
attributes: Record<string, unknown>;
|
|
79
|
-
options: Record<string,
|
|
85
|
+
options: Record<string, {
|
|
86
|
+
value: string;
|
|
87
|
+
attributeId?: string;
|
|
88
|
+
optionId?: string;
|
|
89
|
+
}>;
|
|
80
90
|
mappings: SearchResponseModelResultMappings;
|
|
81
91
|
custom: {};
|
|
82
92
|
constructor(variantData: VariantData);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SearchResultStore.d.ts","sourceRoot":"","sources":["../../../../src/Search/Stores/SearchResultStore.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAqB,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"SearchResultStore.d.ts","sourceRoot":"","sources":["../../../../src/Search/Stores/SearchResultStore.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAqB,aAAa,EAAE,YAAY,EAAE,uBAAuB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAC;AAC1H,OAAO,KAAK,EACX,yBAAyB,EACzB,6BAA6B,EAC7B,gCAAgC,EAChC,iCAAiC,EACjC,6CAA6C,EAC7C,6CAA6C,EAC7C,MAAM,2BAA2B,CAAC;AAEnC,qBAAa,iBAAkB,SAAQ,KAAK,CAAC,OAAO,GAAG,MAAM,CAAC;IAC7D,MAAM,KAAK,CAAC,MAAM,CAAC,OAAO,CAAC,IAA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,cAAc,EAAE,CAAM;IAErC,OAAO,CAAC,cAAc,CAAa;gBAEvB,QAAQ,EAAE,QAAQ,EAAE,cAAc,EAAE,uBAAuB;IAgBhE,YAAY,CAAC,QAAQ,EAAE,QAAQ;IA4D/B,KAAK;IAKL,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,iBAAiB,UAAQ;CAYtD;AAED,qBAAa,OAAO;IACZ,IAAI,SAAa;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM;IACzC,OAAO,EAAE,MAAM,CACrB,MAAM,EACN;QACC,KAAK,EAAE,MAAM,CAAC;QACd,WAAW,CAAC,EAAE,MAAM,CAAC;QACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;KAClB,CACD,CAAC;IAEK,QAAQ,EAAE,iCAAiC,CAEhD;IACK,MAAM,KAAM;gBAEP,WAAW,EAAE,WAAW;CAapC;AAED,cAAM,KAAK;IACH,IAAI,SAAW;IACf,EAAE,EAAE,MAAM,CAAC;IACX,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAM;IACzC,MAAM,KAAM;gBAEP,QAAQ,EAAE,aAAa,EAAE,MAAM,EAAE,yBAAyB;CAUtE"}
|
|
@@ -61,7 +61,7 @@ export class Product {
|
|
|
61
61
|
try {
|
|
62
62
|
// parse the field (JSON)
|
|
63
63
|
const parsedVariants = JSON.parse(this.attributes[variantsField]);
|
|
64
|
-
this.variants = new Variants(parsedVariants, this.mask);
|
|
64
|
+
this.variants = new Variants(parsedVariants, this.mask, config.settings?.variants);
|
|
65
65
|
}
|
|
66
66
|
catch (err) {
|
|
67
67
|
// failed to parse the variant JSON
|
|
@@ -123,37 +123,56 @@ export class ProductMask {
|
|
|
123
123
|
}
|
|
124
124
|
}
|
|
125
125
|
export class Variants {
|
|
126
|
-
constructor(variantData, mask) {
|
|
126
|
+
constructor(variantData, mask, config) {
|
|
127
127
|
this.data = [];
|
|
128
128
|
this.selections = [];
|
|
129
|
-
const options = [];
|
|
130
|
-
// create variants objects
|
|
131
|
-
this.data = variantData.map((variant) => {
|
|
132
|
-
Object.keys(variant.options).forEach((variantOption) => {
|
|
133
|
-
if (!options.includes(variantOption)) {
|
|
134
|
-
options.push(variantOption);
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
return new Variant(variant);
|
|
138
|
-
});
|
|
139
|
-
options.map((option) => {
|
|
140
|
-
// TODO - merge with variant config before constructing selection (for label overrides and swatch mappings)
|
|
141
|
-
const optionConfig = {
|
|
142
|
-
field: option,
|
|
143
|
-
label: option,
|
|
144
|
-
};
|
|
145
|
-
this.selections.push(new VariantSelection(this, optionConfig));
|
|
146
|
-
});
|
|
147
129
|
// setting function in constructor to prevent exposing mask as class property
|
|
148
130
|
this.setActive = (variant) => {
|
|
149
131
|
this.active = variant;
|
|
150
132
|
mask.set({ mappings: this.active.mappings, attributes: this.active.attributes });
|
|
151
133
|
};
|
|
152
|
-
|
|
153
|
-
this.
|
|
134
|
+
this.config = config;
|
|
135
|
+
this.update(variantData, config);
|
|
136
|
+
}
|
|
137
|
+
update(variantData, config = this.config) {
|
|
138
|
+
try {
|
|
139
|
+
const options = [];
|
|
140
|
+
// create variants objects
|
|
141
|
+
this.data = variantData.map((variant) => {
|
|
142
|
+
Object.keys(variant.options).forEach((variantOption) => {
|
|
143
|
+
if (!options.includes(variantOption)) {
|
|
144
|
+
options.push(variantOption);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
return new Variant(variant);
|
|
148
|
+
});
|
|
149
|
+
//need to reset this.selections first
|
|
150
|
+
this.selections = [];
|
|
151
|
+
options.map((option) => {
|
|
152
|
+
// TODO - merge with variant config before constructing selection (for label overrides and swatch mappings)
|
|
153
|
+
const optionConfig = {
|
|
154
|
+
field: option,
|
|
155
|
+
label: option,
|
|
156
|
+
};
|
|
157
|
+
this.selections.push(new VariantSelection(this, optionConfig));
|
|
158
|
+
});
|
|
159
|
+
const preselectedOptions = {};
|
|
160
|
+
if (config?.options) {
|
|
161
|
+
Object.keys(config?.options).forEach((option) => {
|
|
162
|
+
if (config.options[option].preSelected) {
|
|
163
|
+
preselectedOptions[option] = config.options[option].preSelected;
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
// select first available
|
|
168
|
+
this.makeSelections(preselectedOptions);
|
|
169
|
+
}
|
|
170
|
+
catch (err) {
|
|
171
|
+
// failed to parse the variant JSON
|
|
172
|
+
console.error(err, `Invalid variant JSON for: ${variantData}`);
|
|
173
|
+
}
|
|
154
174
|
}
|
|
155
175
|
makeSelections(options) {
|
|
156
|
-
// TODO - support for affinity to attempt to pre-selected options
|
|
157
176
|
// options = {color: 'Blue', size: 'L'};
|
|
158
177
|
if (!options) {
|
|
159
178
|
// select first available for each selection
|
|
@@ -164,8 +183,40 @@ export class Variants {
|
|
|
164
183
|
}
|
|
165
184
|
});
|
|
166
185
|
}
|
|
186
|
+
else {
|
|
187
|
+
this.selections.forEach((selection, idx) => {
|
|
188
|
+
// filter by first available, then by preselected option preference
|
|
189
|
+
//make all options available for first selection.
|
|
190
|
+
const availableOptions = selection.values.filter((value) => (idx == 0 ? true : value.available));
|
|
191
|
+
const preferedOptions = options[selection.field];
|
|
192
|
+
let preferencedOption = availableOptions[0];
|
|
193
|
+
// if theres a preference for that field
|
|
194
|
+
if (preferedOptions) {
|
|
195
|
+
const checkIfAvailable = (preference) => {
|
|
196
|
+
//see if that option is in the available options
|
|
197
|
+
const availablePreferedOptions = availableOptions.find((value) => value.value.toLowerCase() == preference.toLowerCase());
|
|
198
|
+
//use it
|
|
199
|
+
if (availablePreferedOptions) {
|
|
200
|
+
preferencedOption = availablePreferedOptions;
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
if (Array.isArray(preferedOptions)) {
|
|
204
|
+
//loop through each preference option
|
|
205
|
+
preferedOptions.forEach((preference) => {
|
|
206
|
+
checkIfAvailable(preference);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
checkIfAvailable(preferedOptions);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
if (preferencedOption) {
|
|
214
|
+
selection.select(preferencedOption.value);
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}
|
|
167
218
|
}
|
|
168
|
-
|
|
219
|
+
refineSelections(fromSelection) {
|
|
169
220
|
// need to ensure the update originator is at the BOTTOM of the list for refinement
|
|
170
221
|
const orderedSelections = [...this.selections];
|
|
171
222
|
orderedSelections.sort((a) => {
|
|
@@ -175,14 +226,14 @@ export class Variants {
|
|
|
175
226
|
return -1;
|
|
176
227
|
});
|
|
177
228
|
// refine selections ensuring that the selection that triggered the update refines LAST
|
|
178
|
-
orderedSelections.forEach((selection) => selection.
|
|
229
|
+
orderedSelections.forEach((selection) => selection.refineValues(this));
|
|
179
230
|
// check to see if we have enough selections made to update the display
|
|
180
|
-
const selectedSelections = this.selections.filter((selection) => selection.selected
|
|
231
|
+
const selectedSelections = this.selections.filter((selection) => selection.selected);
|
|
181
232
|
if (selectedSelections.length) {
|
|
182
233
|
let availableVariants = this.data;
|
|
183
234
|
// loop through selectedSelections and only include available products that match current selections
|
|
184
235
|
for (const selectedSelection of selectedSelections) {
|
|
185
|
-
availableVariants = availableVariants.filter((variant) => selectedSelection.selected == variant.options[selectedSelection.field] && variant.available);
|
|
236
|
+
availableVariants = availableVariants.filter((variant) => selectedSelection.selected == variant.options[selectedSelection.field].value && variant.available);
|
|
186
237
|
}
|
|
187
238
|
// set active variant
|
|
188
239
|
if (availableVariants.length == 1) {
|
|
@@ -199,33 +250,33 @@ export class VariantSelection {
|
|
|
199
250
|
this.field = selectorConfig.field;
|
|
200
251
|
this.label = selectorConfig.label;
|
|
201
252
|
// needed to prevent attaching variants as class property
|
|
202
|
-
this.variantsUpdate = () => variants.
|
|
253
|
+
this.variantsUpdate = () => variants.refineSelections(this);
|
|
203
254
|
// create possible values from the data and refine them
|
|
204
|
-
this.
|
|
255
|
+
this.refineValues(variants);
|
|
205
256
|
makeObservable(this, {
|
|
206
257
|
selected: observable,
|
|
207
258
|
values: observable,
|
|
208
259
|
});
|
|
209
260
|
}
|
|
210
|
-
|
|
261
|
+
refineValues(variants) {
|
|
211
262
|
// current selection should only consider OTHER selections for availability
|
|
212
263
|
const selectedSelections = variants.selections.filter((selection) => selection.field != this.field && selection.selected);
|
|
213
264
|
let availableVariants = variants.data;
|
|
214
265
|
// loop through selectedSelections and remove products that do not match
|
|
215
266
|
for (const selectedSelection of selectedSelections) {
|
|
216
|
-
availableVariants = availableVariants.filter((variant) => selectedSelection.selected == variant.options[selectedSelection.field] && variant.available);
|
|
267
|
+
availableVariants = availableVariants.filter((variant) => selectedSelection.selected == variant.options[selectedSelection.field].value && variant.available);
|
|
217
268
|
}
|
|
218
269
|
const newValues = variants.data
|
|
219
270
|
.filter((variant) => variant.options[this.field])
|
|
220
271
|
.reduce((values, variant) => {
|
|
221
|
-
if (!values.some((val) => variant.options[this.field] == val.value)) {
|
|
272
|
+
if (!values.some((val) => variant.options[this.field].value == val.value)) {
|
|
222
273
|
values.push({
|
|
223
|
-
|
|
224
|
-
label: variant.options[this.field],
|
|
274
|
+
label: variant.options[this.field].value,
|
|
225
275
|
// TODO: use configurable mappings from config
|
|
226
276
|
// TODO: set background for swatches (via configurable mappings) from config
|
|
227
277
|
thumbnailImageUrl: variant.mappings.core?.thumbnailImageUrl,
|
|
228
|
-
available: Boolean(availableVariants.some((availableVariant) => availableVariant.options[this.field] == variant.options[this.field])),
|
|
278
|
+
available: Boolean(availableVariants.some((availableVariant) => availableVariant.options[this.field].value == variant.options[this.field].value)),
|
|
279
|
+
...variant.options[this.field],
|
|
229
280
|
});
|
|
230
281
|
}
|
|
231
282
|
// TODO: use sorting function from config
|
|
@@ -274,7 +325,6 @@ export class Variant {
|
|
|
274
325
|
constructor(variantData) {
|
|
275
326
|
this.type = 'variant';
|
|
276
327
|
this.attributes = {};
|
|
277
|
-
this.options = {};
|
|
278
328
|
this.mappings = {
|
|
279
329
|
core: {},
|
|
280
330
|
};
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -4,8 +4,13 @@ export type StoreConfig = {
|
|
|
4
4
|
id: string;
|
|
5
5
|
[any: string]: unknown;
|
|
6
6
|
};
|
|
7
|
-
type VariantConfig = {
|
|
7
|
+
export type VariantConfig = {
|
|
8
8
|
field: string;
|
|
9
|
+
options?: {
|
|
10
|
+
[field: string]: {
|
|
11
|
+
preSelected?: string[];
|
|
12
|
+
};
|
|
13
|
+
};
|
|
9
14
|
};
|
|
10
15
|
export type VariantSelectionOptions = {
|
|
11
16
|
field: string;
|
|
@@ -120,5 +125,4 @@ export type SelectedSelection = {
|
|
|
120
125
|
export type FinderStoreState = {
|
|
121
126
|
persisted: boolean;
|
|
122
127
|
};
|
|
123
|
-
export {};
|
|
124
128
|
//# sourceMappingURL=types.d.ts.map
|
package/dist/esm/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,KAAK,EAAE,wCAAwC,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAExI,MAAM,MAAM,WAAW,GAAG;IACzB,EAAE,EAAE,MAAM,CAAC;IACX,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC;CACvB,CAAC;AAEF,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AACjE,OAAO,KAAK,EAAE,wCAAwC,EAAE,wBAAwB,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAExI,MAAM,MAAM,WAAW,GAAG;IACzB,EAAE,EAAE,MAAM,CA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/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
@@ -20,16 +20,16 @@
|
|
|
20
20
|
"test:watch": "jest --watch"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@searchspring/snap-toolbox": "^0.
|
|
23
|
+
"@searchspring/snap-toolbox": "^0.54.0",
|
|
24
24
|
"mobx": "6.9.0"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
|
-
"@searchspring/snap-client": "^0.
|
|
28
|
-
"@searchspring/snap-url-manager": "^0.
|
|
27
|
+
"@searchspring/snap-client": "^0.54.0",
|
|
28
|
+
"@searchspring/snap-url-manager": "^0.54.0"
|
|
29
29
|
},
|
|
30
30
|
"sideEffects": false,
|
|
31
31
|
"files": [
|
|
32
32
|
"dist/**/*"
|
|
33
33
|
],
|
|
34
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "a54d2c6579bf83500ed2faca8ff72b3e3495f063"
|
|
35
35
|
}
|