forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproductQuantityAdd.js
More file actions
298 lines (267 loc) · 7.34 KB
/
productQuantityAdd.js
File metadata and controls
298 lines (267 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
297
298
/*
* 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 { api, LightningElement } from 'lwc';
import { ADD_PRODUCT_TO_CART_EVT, DEFAULT_QUANTITY, OUT_OF_STOCK_EVT } from './constants';
/**
* An event fired when the user indicates a desire to add a quantity of an item to their cart.
* @event ProductQuantityAdd#addproducttocart
* @type {CustomEvent}
* @property {object} detail CustomEvent details
* @property {number} detail.quantity
* The quantity of the product to add to the cart
*/
/**
* An event fired when the inventory validation takes place
* @event ProductQuantityAdd#outofstock
* @type {CustomEvent}
* @property {object} detail CustomEvent details
* @property {boolean} detail.isOutOfStock
* The boolean flag to indicate availability of the product
*/
/**
* @typedef {('primary' | 'secondary' | 'tertiary')} ButtonVariant
*/
/**
* @typedef {object} PurchaseQuantityRule
* @property {?string} minimum The minimum purchase quantity
* @property {?string} maximum The maximum purchase quantity
* @property {?string} increment
* The stepping interval used to increment the purchase quantity
* when clicking the up and down spinner buttons
*/
/**
* A UI control to add N number of product to the cart
* @fires ProductQuantityAdd#addproducttocart
* @fires ProductQuantityAdd#outofstock
*/
export default class ProductQuantityAdd extends LightningElement {
static renderMode = 'light';
/**
* Whether the quantity provided is invalid
* @type {boolean}
* @private
*/
isQuantityInvalid = false;
/**
* In PQR mode, the inventory count is validated against the PQR rule.
* When the inventory is invalid, we show out of stock label and disable the button.
* @type {boolean}
* @private
*/
isInventoryInvalid = false;
/**
* Quantity that is currently set in the input field
* @type {number}
* @private
*/
currentQuantity = DEFAULT_QUANTITY;
/**
* Gets or sets the text of the button that triggers the addition of items.
* @type {?string}
*/
@api
buttonText;
/**
* The style of the add to cart button (primary/secondary/tertiary)
* @type {?ButtonVariant}
*/
@api
buttonVariant;
/**
* Rule which constrains what quantities of a product may be purchased.
* @type {?PurchaseQuantityRule}
* @example
* {
* minimum : '10.0',
* maximum : '100.0',
* increment : '5.0'
* }
*/
@api
quantityRule;
/**
* Label for Inventory Information: Out of Stock
* @type {?string}
*/
@api
outOfStockText;
/**
* Inventory count for a productId
* @type {?number}
*/
@api
availableQuantity;
/**
* The quantity to set the quantity selector to.
* @type {?number}
*/
@api
set quantity(value) {
if (value !== undefined) {
this.currentQuantity = value;
}
}
get quantity() {
return this.currentQuantity;
}
/**
* Whether the component should be disabled
* @type {boolean}
*/
@api
disabled = false;
/**
* Gets or sets the name of the optional SLDS icon (e.g. "utility:success") to be displayed alongside the button text.
* If no value is provided (i.e. the value is null, undefined, or an empty string), no icon is displayed.
* @type {?string}
*/
@api
iconName;
/**
* Gets or sets the Quantity Rules Minimum Text
* @type {?string}
*/
@api
minimumText;
/**
* Gets or sets the Quantity Rules Maximum Text
* @type {?string}
*/
@api
maximumText;
/**
* Gets or sets the Quantity Rules Increment Text
* @type {?string}
*/
@api
incrementText;
/**
* Gets or sets the label for Quantity Rules
* @type {?string}
*/
@api
quantitySelectorLabel;
/**
* Gets the text of the button based on inversion of the product.
* Do not compute the logic for inventory check here. Let the quantity selector handle the logic.
* @type {?string}
* @readonly
* @private
*/
get computedAddToCartButtonText() {
return this.isInventoryInvalid ? this.outOfStockText : this.buttonText;
}
/**
* The increment quantity of the product that is allowed to be be purchased
* @type {?string}
* @readonly
* @private
*/
get quantityRuleIncrement() {
return this.quantityRule?.increment ?? null;
}
/**
* The minimum quantity of the product that may be purchased
* @type {?string}
* @readonly
* @private
*/
get quantityRuleMinimum() {
return this.quantityRule?.minimum ?? null;
}
/**
* The maximum quantity of the product that may be purchased
* @type {?string}
* @readonly
* @private
*/
get quantityRuleMaximum() {
return this.quantityRule?.maximum ?? null;
}
/**
* Gets whether the 'add to cart' button is disabled
* @type {boolean}
* @readonly
* @private
*/
get buttonDisabled() {
return this.disabled || this.isQuantityInvalid || this.isInventoryInvalid;
}
/**
* Whether to show a button icon
* @type {boolean}
* @readonly
* @private
*/
get hasIcon() {
return Boolean(this.iconName);
}
/**
* Gets the button icon's variant
* @type {?'inverse'}
* @readonly
* @private
*/
get buttonIconVariant() {
return this.buttonVariant === 'primary' ? 'inverse' : undefined;
}
/**
* Handler for the 'valuechanged' event fired from the
* 'quantity-selector'
* @param {CustomEvent} event the event object
* @private
*/
handleValueChanged(event) {
const value = event?.detail?.value;
this.isQuantityInvalid = false;
this.currentQuantity = value !== undefined && value !== null ? value : DEFAULT_QUANTITY;
}
/**
* Handler for the 'validitychanged' event fired from the
* 'quantity-selector'
* @param {CustomEvent} event the event object
* @private
*/
handleValidityChanged(event) {
this.isQuantityInvalid = !event.detail?.isValid;
}
/**
* Handler for the 'outofstock' event fired from the
* 'quantity-selector'
* @param {CustomEvent} event the event object
* @private
*/
handleOutOfStock(event) {
event.stopPropagation();
this.isInventoryInvalid = event.detail?.isOutOfStock;
this.dispatchEvent(
new CustomEvent(OUT_OF_STOCK_EVT, {
detail: {
isOutOfStock: this.isInventoryInvalid,
},
})
);
}
/**
* Handler for the 'click' event fired from the 'add-to-cart'
* @param {CustomEvent} event the event object
* @private
*/
handleAddToCart(event) {
event.stopPropagation();
this.dispatchEvent(
new CustomEvent(ADD_PRODUCT_TO_CART_EVT, {
bubbles: true,
composed: true,
detail: {
quantity: this.currentQuantity,
},
})
);
}
}