generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
/
inline-price.js
316 lines (298 loc) · 8.79 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
import {
createPlaceholder,
definePlaceholder,
selectPlaceholders,
updatePlaceholder,
} from './placeholder.js';
import { selectOffers, useService } from './utilities.js';
import { GeoMap } from './settings';
// countries where tax is displayed for all segments by default
const DISPLAY_ALL_TAX_COUNTRIES = [
GeoMap.uk,
GeoMap.au,
GeoMap.fr,
GeoMap.at,
GeoMap.be_en,
GeoMap.be_fr,
GeoMap.be_nl,
GeoMap.bg,
GeoMap.ch_de,
GeoMap.ch_fr,
GeoMap.ch_it,
GeoMap.cz,
GeoMap.de,
GeoMap.dk,
GeoMap.ee,
GeoMap.eg_ar,
GeoMap.eg_en,
GeoMap.es,
GeoMap.fi,
GeoMap.fr,
GeoMap.gr_el,
GeoMap.gr_en,
GeoMap.hu,
GeoMap.ie,
GeoMap.it,
GeoMap.lu_de,
GeoMap.lu_en,
GeoMap.lu_fr,
GeoMap.nl,
GeoMap.no,
GeoMap.pl,
GeoMap.pt,
GeoMap.ro,
GeoMap.se,
GeoMap.si,
GeoMap.sk,
GeoMap.tr,
GeoMap.ua,
GeoMap.id_en,
GeoMap.id_id,
GeoMap.in_en,
GeoMap.in_hi,
GeoMap.jp,
GeoMap.my_en,
GeoMap.my_ms,
GeoMap.nz,
GeoMap.th_en,
GeoMap.th_th,
];
// countries where tax is displayed for some segments only by default
const DISPLAY_TAX_MAP = {
INDIVIDUAL_COM: [
GeoMap.za,
GeoMap.lt,
GeoMap.lv,
GeoMap.ng,
GeoMap.sa_ar,
GeoMap.sa_en,
GeoMap.za,
GeoMap.sg,
GeoMap.kr,
], // individual
TEAM_COM: [
GeoMap.za,
GeoMap.lt,
GeoMap.lv,
GeoMap.ng,
GeoMap.za,
GeoMap.co,
GeoMap.kr,
], // business
INDIVIDUAL_EDU: [GeoMap.lt, GeoMap.lv, GeoMap.sa_en, GeoMap.sea], // student
TEAM_EDU: [GeoMap.sea, GeoMap.kr], // school and uni
};
/** @type {Commerce.Price.PlaceholderConstructor} */
export class HTMLPriceSpanElement 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',
];
}
/** @type {Commerce.Price.PlaceholderConstructor["createInlinePrice"]} */
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);
/** @type {Commerce.Price.Placeholder} */
// @ts-ignore
const element = createPlaceholder(HTMLPriceSpanElement, {
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
perpetual,
promotionCode,
quantity,
template,
wcsOsi,
});
return element;
}
// TODO: consider moving this function to the `web-components` package
/** @type {Commerce.Price.PlaceholderConstructor["getInlinePrices"]} */
static getInlinePrices(container) {
/** @type {Commerce.Price.Placeholder[]} */
// @ts-ignore
const elements = selectPlaceholders(HTMLPriceSpanElement, container);
return elements;
}
get isInlinePrice() {
return true;
}
/**
* Returns `this`, typed as Placeholder mixin.
* @type {Commerce.Price.Placeholder}
*/
get placeholder() {
// @ts-ignore
return this;
}
/**
* 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.placeholder,
);
if (!options.wcsOsi.length) return false;
/*
Commented out until issues in content with manually added tax labels are resolved
if (!this.placeholder.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.placeholder.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,
});
version ??= this.placeholder.togglePending(options);
if (offers.length) {
if (this.placeholder.toggleResolved(version, offers, options)) {
this.innerHTML = service.buildPriceHTML(offers, options);
return true;
}
} else {
const error = new Error(`Not provided: ${options?.wcsOsi ?? '-'}`);
if (this.placeholder.toggleFailed(version, error, options)) {
this.innerHTML = '';
return true;
}
}
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);
updatePlaceholder(this, {
displayOldPrice,
displayPerUnit,
displayRecurrence,
displayTax,
forceTaxExclusive,
perpetual,
promotionCode,
quantity,
template,
wcsOsi,
});
return true;
}
}
export const InlinePrice = definePlaceholder(HTMLPriceSpanElement);