forked from forcedotcom/commerce-on-lightning-components
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilderProductPricingTiers.js
More file actions
205 lines (183 loc) · 5.64 KB
/
builderProductPricingTiers.js
File metadata and controls
205 lines (183 loc) · 5.64 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
/*
* 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, wire } from 'lwc';
import { generateStyleProperties } from 'experience/styling';
import { SessionContextAdapter } from 'commerce/contextApi';
import { priceAdjustmentTiers } from './designTimeData'; // <-- design-time only
/**
* @typedef {{[key: string]: *}} JsonData
*/
/**
* Dedicated Building Block component which displays pricing tiers for a product with tiered discounts.
*
* **Important Note:**
*
* This example uses a technique that can have a minimal negative impact on the runtime behavior of the component.
* Namely, this example loads component-specific mock data at design-time (when in the Experience Builder) so that
* a user is not left without any visualization of the component during the declarative administration of a page.
*
* If you prefer to change this behavior, simply remove the places marked with "design-time only".
* @slot title
*/
export default class BuilderPricingTiers extends LightningElement {
static renderMode = 'light';
/**
* @type {?JsonData}
* @private
*/
_sessionContext; // <-- design-time only
/**
* @type {?JsonData}
* @private
*/
_productPricing;
/**
* @type {?JsonData}
* @private
*/
_designTimeData; // <-- design-time only
/**
* We need to show the pricing tiers during design-time / preview mode. So we're using
* the session context to retrieve that information.
* @param {object} response Wire adapter response
* @param {JsonData} [response.data] The wired session context data
* @private
*/
@wire(SessionContextAdapter) // <-- design-time only
wireSessionContext({ data }) {
this._sessionContext = data;
if (this._sessionContext?.isPreview && !this._designTimeData) {
this._designTimeData = priceAdjustmentTiers();
}
}
/**
* The row label text displayed for the second row of adjustment tiers where the quantities are displayed.
* @type {?string}
*/
@api
quantityRowLabel;
/**
* The row label text displayed for the first row of adjustment tiers where the discounts per unit are displayed.
* @type {?string}
*/
@api
discountRowLabel;
/**
* Border radius of the price adjustment tiers
* @type {?number}
*/
@api
borderRadius;
/**
* Background color of the price adjustment tiers
* @type {?string}
*/
@api
backgroundColor;
/**
* Row title text for price adjustment tiers
* @type {?string}
*/
@api
rowTitleTextColor;
/**
* Label text for price adjustment tiers
* @type {?string}
*/
@api
labelTextColor;
/**
* Color for the price adjustment tiers
* @type {?string}
*/
@api
textColor;
/**
* Border Color of the price adjustment tiers
* @type {?string}
*/
@api
borderColor;
/**
* Product detail data
* @type {?JsonData}
*/
@api
product;
/**
* Product variant data
* @type {?JsonData}
*/
@api
productVariant;
/**
* Product pricing data
* @type {?JsonData}
*/
@api
set productPricing(value) {
this._productPricing = value;
}
get productPricing() {
if (
this.isPreviewMode &&
this._productPricing &&
!Array.isArray(this._productPricing.priceAdjustment?.priceAdjustmentTiers) &&
this._designTimeData
) {
// This whole condition is for the design-time only
return { ...this._productPricing, priceAdjustment: { priceAdjustmentTiers: this._designTimeData } };
}
return this._productPricing;
}
/**
* design-time only
* @private
* @returns {boolean} Whether we operate in the design-time / preview mode
*/
get isPreviewMode() {
return !!this._sessionContext?.isPreview;
}
/**
* Whether the tier pricing is disabled
* @private
* @returns {boolean} true if tier pricing is disabled
*/
get isTierPricingDisabled() {
return (
this.productVariant?.isValid === false ||
this.product?.productClass === 'VariationParent' ||
this.product?.productClass === 'Set'
);
}
/**
* @private
* @returns {boolean} true if the pricing tier should be shown
*/
get showPricingTiers() {
return (
// The `isPreviewMode` condition portion is for the design-time only
this.isPreviewMode ||
(this?.productPricing?.priceAdjustment?.priceAdjustmentTiers !== undefined && !this.isTierPricingDisabled)
);
}
/**
* @private
* @returns {string} string of pricing tiers custom styles
*/
get pricingTiersCustomStyles() {
return generateStyleProperties({
'--ref-c-product-pricing-tiers-border-radius:': this.borderRadius ? this.borderRadius + 'px' : 'initial',
'--ref-c-product-pricing-tiers-background-color:': this.backgroundColor || 'initial',
'--ref-c-product-pricing-tiers-row-title-text-color:': this.rowTitleTextColor || 'initial',
'--ref-c-product-pricing-tiers-label-text-color:': this.labelTextColor || 'initial',
'--ref-c-product-pricing-tiers-text-color:': this.textColor || 'initial',
'--ref-c-product-pricing-tiers-border-color:': this.borderColor || 'initial',
});
}
}