generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 177
/
Copy pathinline-price.js
337 lines (310 loc) · 8.88 KB
/
inline-price.js
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import {
createMasElement,
updateMasElement,
MasElement,
} from './mas-element.js';
import { selectOffers, useService } from './utilities.js';
// countries where tax is displayed for all segments by default
const DISPLAY_ALL_TAX_COUNTRIES = [
'GB_en',
'AU_en',
'FR_fr',
'AT_de',
'BE_en',
'BE_fr',
'BE_nl',
'BG_bg',
'CH_de',
'CH_fr',
'CH_it',
'CZ_cs',
'DE_de',
'DK_da',
'EE_et',
'EG_ar',
'EG_en',
'ES_es',
'FI_fi',
'FR_fr',
'GR_el',
'GR_en',
'HU_hu',
'IE_en',
'IT_it',
'LU_de',
'LU_en',
'LU_fr',
'NL_nl',
'NO_nb',
'PL_pl',
'PT_pt',
'RO_ro',
'SE_sv',
'SI_sl',
'SK_sk',
'TR_tr',
'UA_uk',
'ID_en',
'ID_in',
'IN_en',
'IN_hi',
'JP_ja',
'MY_en',
'MY_ms',
'NZ_en',
'TH_en',
'TH_th',
];
// countries where tax is displayed for some segments only by default
const DISPLAY_TAX_MAP = {
// individual
INDIVIDUAL_COM: [
'ZA_en',
'LT_lt',
'LV_lv',
'NG_en',
'SA_ar',
'SA_en',
'ZA_en',
'SG_en',
'KR_ko',
],
// business
TEAM_COM: ['ZA_en', 'LT_lt', 'LV_lv', 'NG_en', 'ZA_en', 'CO_es', 'KR_ko'],
// student
INDIVIDUAL_EDU: ['LT_lt', 'LV_lv', 'SA_en', 'SG_en'],
// school and uni
TEAM_EDU: ['SG_en', 'KR_ko'],
};
export class InlinePrice extends HTMLSpanElement {
static is = 'inline-price';
static tag = 'span';
static get observedAttributes() {
return [
'data-display-old-price',
'data-display-per-unit',
'data-display-recurrence',
'data-display-tax',
'data-perpetual',
'data-promotion-code',
'data-tax-exclusive',
'data-template',
'data-wcs-osi',
];
}
static createInlinePrice(options) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const service = useService();
if (!service) return null;
const {
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
perpetual,
promotionCode,
quantity,
template,
wcsOsi,
} = service.collectPriceOptions(options);
const element = createMasElement(InlinePrice, {
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
perpetual,
promotionCode,
quantity,
template,
wcsOsi,
});
return element;
}
constructor() {
super();
this.handleClick = this.handleClick.bind(this);
}
get isInlinePrice() {
return true;
}
masElement = new MasElement(this);
attributeChangedCallback(name, _, value) {
this.masElement.attributeChangedCallback(name, _, value);
}
connectedCallback() {
this.masElement.connectedCallback();
this.addEventListener('click', this.handleClick);
}
disconnectedCallback() {
this.masElement.disconnectedCallback();
this.removeEventListener('click', this.handleClick.bind(this));
}
handleClick(event) {
/* c8 ignore next 4 */
if (event.target === this) return;
// re-dispatch click event from the price element
event.stopImmediatePropagation();
this.dispatchEvent(new CustomEvent('click', { bubbles: true }));
}
onceSettled() {
return this.masElement.onceSettled();
}
get value() {
return this.masElement.value;
}
requestUpdate(force = false) {
return this.masElement.requestUpdate(force);
}
/**
* Resolves default value of displayTax property, based on provided geo info and segments.
* @returns {boolean}
*/
/* c8 ignore next 26 */
resolveDisplayTaxForGeoAndSegment(
country,
language,
customerSegment,
marketSegment,
) {
const locale = `${country}_${language}`;
if (
DISPLAY_ALL_TAX_COUNTRIES.includes(country) ||
DISPLAY_ALL_TAX_COUNTRIES.includes(locale)
) {
return true;
}
const segmentConfig =
DISPLAY_TAX_MAP[`${customerSegment}_${marketSegment}`];
if (!segmentConfig) {
return false;
}
if (segmentConfig.includes(country) || segmentConfig.includes(locale)) {
return true;
}
return false;
}
/**
* Resolves default value of displayTax property, based on provided geo info and segments extracted from offers object.
* @returns {boolean}
*/
/* c8 ignore next 15 */
async resolveDisplayTax(service, options) {
const [offerSelectors] = await service.resolveOfferSelectors(options);
const offers = selectOffers(await offerSelectors, options);
if (offers?.length) {
const { country, language } = options;
const offer = offers[0];
const [marketSegment = ''] = offer.marketSegments;
return this.resolveDisplayTaxForGeoAndSegment(
country,
language,
offer.customerSegment,
marketSegment,
);
}
}
/**
* Resolves associated osi via Wcs and renders price offer.
* @param {Record<string, any>} overrides
*/
async render(overrides = {}) {
if (!this.isConnected) return false;
// eslint-disable-next-line react-hooks/rules-of-hooks
const service = useService();
if (!service) return false;
const options = service.collectPriceOptions(overrides, this);
if (!options.wcsOsi.length) return false;
/*
Commented out until issues in content with manually added tax labels are resolved
if (!this.masElement.dataset.displayTax) {
// set default value for displayTax if not set neither in OST nor in price URL
options.displayTax =
(await this.resolveDisplayTax(service, options)) || false;
}
*/
const version = this.masElement.togglePending(options);
this.innerHTML = '';
const [promise] = service.resolveOfferSelectors(options);
return this.renderOffers(
selectOffers(await promise, options),
options,
version,
);
}
// TODO: can be extended to accept array of offers and compute subtotal price
/**
* Renders price offer as HTML of this component
* using consonant price template functions
* from package `@dexter/tacocat-consonant-templates`.
* @param {Commerce.Wcs.Offer[]} offers
* @param {Record<string, any>} overrides
* Optional object with properties to use as overrides
* over those collected from dataset of this component.
*/
renderOffers(offers, overrides = {}, version = undefined) {
if (!this.isConnected) return;
// eslint-disable-next-line react-hooks/rules-of-hooks
const service = useService();
if (!service) return false;
const options = service.collectPriceOptions(
{
...this.dataset,
...overrides,
},
this,
);
version ??= this.masElement.togglePending(options);
if (offers.length) {
if (this.masElement.toggleResolved(version, offers, options)) {
this.innerHTML = service.buildPriceHTML(offers, options);
return true;
}
} else {
const error = new Error(`Not provided: ${options?.wcsOsi ?? '-'}`);
if (this.masElement.toggleFailed(version, error, options)) {
this.innerHTML = '';
return true;
}
}
/* c8 ignore next 1 */
return false;
}
updateOptions(options) {
// eslint-disable-next-line react-hooks/rules-of-hooks
const service = useService();
if (!service) return false;
const {
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
perpetual,
promotionCode,
quantity,
template,
wcsOsi,
} = service.collectPriceOptions(options);
updateMasElement(this, {
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
perpetual,
promotionCode,
quantity,
template,
wcsOsi,
});
return true;
}
}
// Define custom DOM element
if (!window.customElements.get(InlinePrice.is)) {
window.customElements.define(InlinePrice.is, InlinePrice, {
extends: InlinePrice.tag,
});
}