forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilderSearchResults.js
More file actions
296 lines (261 loc) · 7.34 KB
/
builderSearchResults.js
File metadata and controls
296 lines (261 loc) · 7.34 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
* 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 { LightningElement, api, wire } from 'lwc';
import { navigate, NavigationContext } from 'lightning/navigation';
import { createCartItemAddAction, createSearchFiltersUpdateAction, dispatchAction } from 'commerce/actionApi';
import CommonModal from 'c/commonModal';
import { Labels } from './labels';
/**
* @typedef {import('../searchResults/searchResults').ProductSearchResultSummary} ProductSearchResultSummary
*/
/**
* @typedef {import('../searchResults/searchResults').CardContentMappingItem} CardContentMappingItem
*/
/**
* Component that displays products for search and category page.
*/
export default class BuilderSearchResults extends LightningElement {
static renderMode = 'light';
@wire(NavigationContext)
navContext;
/**
* Results returned from the Search Data Provider
* @type {?ProductSearchResultSummary}
*/
@api
searchResults;
/**
* Default field to show in results
* @type {?string}
*/
@api
searchResultsFields;
/**
* The layout of the results tiles.
* @type {?('grid' | 'list')}
*/
@api
resultsLayout;
/**
* The size of the spacing between the grid columns.
* @type {?('small' | 'medium' | 'large'| 'none')}
*/
@api
gridColumnSpacing;
/**
* The size of the spacing between the grid rows.
* @type {?('small' | 'medium' | 'large'| 'none')}
*/
@api
gridRowSpacing;
/**
* The maximum number of grid columns to be displayed.
* Accepted values are between 1 and 8.
* @type {?number}
*/
@api
gridMaxColumnsDisplayed;
/**
* The size of the spacing between the list rows.
* @type {?('small' | 'medium' | 'large'| 'none')}
*/
@api
listRowSpacing;
/**
* Font color for the card background field, as 'rgb', 'rgba' or 'hex' CSS value.
* @type {?string}
*/
@api
cardBackgroundColor;
/**
* The alignment of the results cards.
* @type {?('right' | 'center' | 'left')}
*/
@api
cardAlignment;
/**
* Font color for the card border field, as 'rgb', 'rgba' or 'hex' CSS value.
* @type {?string}
*/
@api
cardBorderColor;
/**
* The value of the border radius for the results card.
* @type {?string}
*/
@api
cardBorderRadius;
/**
* Font color for the card divider field, as 'rgb', 'rgba' or 'hex' CSS value.
* @type {?string}
*/
@api
cardDividerColor;
/**
* The font size of the negotiated price.
* @type {?('small' | 'medium' | 'large')}
*/
@api
negotiatedPriceTextSize;
/**
* Whether to display the negotiated price.
* @type {boolean}
* @default false
*/
@api
showNegotiatedPrice = false;
/**
* Font color for the negotiated price text field, as 'rgb', 'rgba' or 'hex' CSS value.
* @type {?string}
*/
@api
negotiatedPriceTextColor;
/**
* Whether to display the original price.
* @type {boolean}
* @default false
*/
@api
showOriginalPrice = false;
/**
* The font size of the original price.
* @type {?('small' | 'medium' | 'large')}
*/
@api
originalPriceTextSize;
/**
* Font color for the original price text field, as 'rgb', 'rgba' or 'hex' CSS value.
* @type {?string}
*/
@api
originalPriceTextColor;
/**
* Whether to display the product image.
* @type {boolean}
* @default false
*/
@api
showProductImage = false;
/**
* The product fields to display in the productCard cmp.
* @type {string}
*/
@api
cardContentMapping;
/**
* Whether to display the action button.
* @type {boolean}
* @default false
*/
@api
showCallToActionButton = false;
/**
* The text for the add to cart button
* @type {?string}
*/
@api
addToCartButtonText;
/**
* The button style for add to cart button
* Accepted values primary, secondary, tertiary
* @type {?('primary' | 'secondary' | 'tertiary')}
*/
@api
addToCartButtonStyle;
/**
* The text for the add to cart button when cart is processing
* @type {?string}
*/
@api
addToCartButtonProcessingText;
/**
* The text for the view options button
* @type {?string}
*/
@api
viewOptionsButtonText;
/**
* The current page number of the results.
* @type {?string}
*/
@api
currentPage;
/**
* @type {CardContentMappingItem[]}
* @readonly
* @private
*/
get normalizedCardContentMapping() {
return JSON.parse(this.cardContentMapping ?? '[]');
}
/**
* Handles the 'addproducttocart' event.
* Adds the product to the cart and then on success opens the add to cart modal.
* @param {CustomEvent<{ productId: string; quantity: number }>} event The event object
* @private
*/
handleAddToCart(event) {
event.stopPropagation();
const { productId, quantity } = event.detail;
dispatchAction(this, createCartItemAddAction(productId, quantity), {
onSuccess: () => {
CommonModal.open({
label: Labels.messageSuccessfullyAddedToCart,
size: 'small',
secondaryActionLabel: Labels.actionContinueShopping,
primaryActionLabel: Labels.actionViewCart,
onprimaryactionclick: () => this.navigateToCart(),
});
},
});
}
/**
* Navigates to the cart page when the primary button is clicked
* from the modal after adding an item to the cart
* @private
*/
navigateToCart() {
this.navContext &&
navigate(this.navContext, {
type: 'comm__namedPage',
attributes: {
name: 'Current_Cart',
},
});
}
/**
* Handles navigating to the product detail page from the search results page.
* @param {CustomEvent<{productId: string; productName: string}>} event The event object
*/
handleNavigateToProductPage(event) {
event.stopPropagation();
const urlName = this.searchResults?.cardCollection.find((card) => card.id === event.detail.productId)?.urlName;
this.navContext &&
navigate(this.navContext, {
type: 'standard__recordPage',
attributes: {
objectApiName: 'Product2',
recordId: event.detail.productId,
actionName: 'view',
urlName: urlName ?? undefined,
},
state: {
recordName: event.detail.productName,
},
});
}
/**
* Trigger an update of the page number at the closest `SearchDataProvider`
* @param {CustomEvent<{newPageNumber: number}>} event The event object
* @private
*/
handleUpdateCurrentPage(event) {
event.stopPropagation();
dispatchAction(this, createSearchFiltersUpdateAction({ page: event.detail.newPageNumber }));
}
}