forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearchResultsUtils.js
More file actions
231 lines (214 loc) · 8.3 KB
/
searchResultsUtils.js
File metadata and controls
231 lines (214 loc) · 8.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (c) 2023, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: Apache-2.0
* For full license text, see the LICENSE file in the repo
* root or https://opensource.org/licenses/apache-2-0/
*/
import variationAttributeLabel from '@salesforce/label/c.Search_ProductCard_variationAttribute';
/**
* @typedef {import('../searchResults').ProductSearchResultSummary} ProductSearchResultSummary
*/
/**
* @typedef {import('../searchResults').ProductCardDetail} ProductCardDetail
*/
/**
* @typedef {import('../searchResults').ResultsConfiguration} ResultsConfiguration
*/
/**
* @typedef {import('../searchResults').BuilderLayoutConfiguration} BuilderLayoutConfiguration
*/
/**
* @typedef {import('../searchResults').CardContentMappingItem} CardContentMappingItem
*/
/**
* @typedef {import('../searchProductGrid/searchProductGrid').BuilderFieldItem} BuilderFieldItem
*/
/**
* @typedef {import('../searchProductCard/searchProductCard').FieldValueData} FieldValueData
*/
/**
* @typedef {import('../searchProductCard/searchProductCard').FieldValueDetailData} FieldValueDetailData
*/
/**
* @typedef {import('../searchProductCard/searchProductCard').ProductAttributeSetSummary} ProductAttributeSetSummary
*/
/**
* @typedef {{[key: string]: *}} BuilderCardConfiguration
*/
/**
* Computes a list of attributes from the given attribute set.
* @param {ProductAttributeSetSummary} attributeSet - A product variation attribute set
* @returns {string} A comma separated ordered list of attributes.
*/
export function computeVariationAttributesField(attributeSet) {
const { attributes = [] } = attributeSet || {};
return [...attributes]
.sort((value1, value2) => {
const { sequence: sequence1 } = value1;
const { sequence: sequence2 } = value2;
return sequence1 - sequence2;
})
.map((item) => {
const { label, value } = item;
return variationAttributeLabel.replace('{name}', label).replace('{value}', value);
})
.join(', ');
}
/**
* Consolidate Experience Builder's content mapping fields with runtime product fields.
* In order to merge these fields, {@link ProductCardDetail}'s field should
* match to @type {@link BuilderFieldItem}'s 'name'.
*
* Note: We have to make all fields of a particular card to be navigable to PDP,
* but only 1 field to have tab stop per card to make the accessibility smooth.
* So this function does the following logic to make one field tabStoppable in
* a list of FieldValueDetailData.
* - make the first field with the name = 'Name' as the tabStoppable field.
* - if there is no field called 'Name' in the list, make the first item in
* the list as tabStoppable.
* @param {{[key: string]: FieldValueData}} productFieldMap Key-value pairs of field names and their value objects. This is from runtime.
* @param {string} productClass The class the product belongs to
* @param {Array<CardContentMappingItem>} contentMapping An array of content map field configuration. This is from builder.
* @param {boolean} tabStoppable Whether to make a field in the map as tab-stoppable.
* @returns {Array<FieldValueDetailData>} A list of {@link FieldValueDetailData}
*/
export function generateContentMappedFields(
productFieldMap = {},
productClass = '',
contentMapping = [],
tabStoppable = false
) {
const PRODUCT_FIELD_NAME = 'Name';
const fieldMap = productFieldMap;
const fields = (contentMapping || [])
.map(({ name, label, type }) => ({
name,
label: productClass === 'Variation' && type === 'VARIATION' ? '' : label,
type,
// eslint-disable-next-line
value: (fieldMap[name] || {})['value'] || '',
tabStoppable: false,
}))
.filter(({ value, type }) => (productClass === 'VariationParent' && type === 'VARIATION') || value !== '');
if (tabStoppable && fields.length > 0) {
const nameField = fields.find((item) => item.name === PRODUCT_FIELD_NAME);
const fieldValueItem = nameField || fields[0];
fieldValueItem.tabStoppable = true;
}
return fields;
}
/**
* @param {({[key: string]: FieldValueData} | FieldValueDetailData[])} fields
* The fields belonging to the card item.
* @param {?string} productClass
* The product class
* @param {?ProductAttributeSetSummary} variationAttributeSet
* The variation attribute set
* @param {BuilderCardConfiguration} cardConfig
* The product card configuration for Experience Builder
* @returns {Array<FieldValueDetailData>}
* A list of {@link FieldValueDetailData}
*/
function consolidateFieldDisplayData(fields, productClass, variationAttributeSet, cardConfig) {
const transformedFields = new Map();
for (const [key, value] of Object.entries(fields)) {
transformedFields.set(key, value);
}
if (productClass === 'Variation' && variationAttributeSet) {
transformedFields.set('VariationAttributes', {
value: computeVariationAttributesField(variationAttributeSet),
});
}
return generateContentMappedFields(
Object.fromEntries(transformedFields),
productClass,
cardConfig.cardContentMapping,
cardConfig.showCallToActionButton === false
);
}
/**
* Transform the product search query results into to a UI friendly display-data format.
* @param {ProductSearchResultSummary} results
* The search results data as it's passed from the search data provider.
* @param {BuilderCardConfiguration} cardConfiguration
* The product card configuration object for Experience Builder.
* @returns {ProductSearchResultSummary}
* The normalized/transformed search results data for the UI consumption.
*/
export function transformDataWithConfiguration(results, cardConfiguration = {}) {
const cardCollection = ((results || {}).cardCollection || []).map((cardDetail) => {
const { fields, productClass, variationAttributeSet } = cardDetail;
return {
...cardDetail,
fields: consolidateFieldDisplayData(fields, productClass, variationAttributeSet, cardConfiguration),
};
});
return {
...results,
cardCollection,
};
}
/**
* Compute the layout configuration from the given set of UI properties.
* @param {BuilderLayoutConfiguration} configParameters The builder layout configuration from builder properties.
* @returns {ResultsConfiguration} The complete configuration for the search results.
*/
export function computeConfiguration(configParameters) {
const {
layout = '',
gridMaxColumnsDisplayed = 4,
addToCartDisabled = false,
builderCardConfiguration,
} = configParameters || {};
const {
addToCartButtonText = '',
addToCartButtonProcessingText = '',
cardContentMapping = [],
showCallToActionButton = false,
showNegotiatedPrice = false,
showOriginalPrice: showListingPrice = false,
showProductImage = false,
viewOptionsButtonText = '',
showQuantitySelector = false,
minimumQuantityGuideText = '',
maximumQuantityGuideText = '',
incrementQuantityGuideText = '',
showQuantityRulesText = true,
quantitySelectorLabelText = '',
} = builderCardConfiguration || {};
const fieldConfiguration = cardContentMapping.reduce((acc, { name, fontSize, fontColor, showLabel = false }) => {
acc[name] = {
fontSize,
fontColor,
showLabel,
};
return acc;
}, {});
return {
layoutConfiguration: {
layout,
gridMaxColumnsDisplayed,
cardConfiguration: {
addToCartButtonText,
addToCartButtonProcessingText,
showCallToActionButton,
viewOptionsButtonText,
showQuantitySelector,
minimumQuantityGuideText,
maximumQuantityGuideText,
incrementQuantityGuideText,
showQuantityRulesText,
quantitySelectorLabelText,
showProductImage,
addToCartDisabled,
layout,
fieldConfiguration,
priceConfiguration: {
showNegotiatedPrice,
showListingPrice,
},
},
},
};
}