generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cards.js
92 lines (81 loc) · 2.79 KB
/
cards.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
import { createOptimizedPicture } from '../../scripts/lib-franklin.js';
import {
a, div, li, strong, ul, p, img,
} from '../../scripts/dom-helpers.js';
import { walgreensUrl } from '../../scripts/scripts.js';
function decorateCuratedCards(block) {
const cardsWithBorder = block.classList.contains('border');
/* change to ul, li */
const list = ul();
[...block.children].forEach((row) => {
const listItem = li({ class: `card ${cardsWithBorder ? ' with-border' : ''}` });
const link = row.querySelector('a');
let parent = listItem;
if (link) {
link.textContent = '';
parent = link;
listItem.appendChild(link);
}
while (row.firstElementChild) parent.append(row.firstElementChild);
[...parent.children].forEach((child) => {
if (child.children.length === 1 && child.querySelector('picture')) child.className = 'card-image';
else child.className = 'card-body';
});
list.append(listItem);
});
list.querySelectorAll('img').forEach((image) => image.closest('picture').replaceWith(
createOptimizedPicture(image.src, image.alt, false, [{ width: '750' }]),
));
block.textContent = '';
block.append(list);
}
function apiCardLink(offer) {
if ('plucode' in offer) {
return `https://www.walgreens.com/store/store/xpo_products.jsp?pluCode=${offer.plucode}`;
}
if ('eventCode' in offer) {
return `https://www.walgreens.com/store/BalanceRewardsOffers/balance-rewards-offer.jsp?eventCode=${offer.eventCode}`;
}
// eslint-disable-next-line no-console
console.warn('Could not generate link for the following offer', offer);
return '';
}
async function decorateAPICards(block) {
const cardsWithBorder = block.classList.contains('border');
const apiEndpoint = block.querySelector('a').href;
block.innerHTML = '';
const apiResponse = await fetch(apiEndpoint);
if (!apiResponse.ok) {
return;
}
const apiInfo = JSON.parse(await apiResponse.text());
block.append(
ul(
...apiInfo.offers.map((offer) => (
li({ class: `card ${cardsWithBorder ? ' with-border' : ''}` },
a({ href: apiCardLink(offer) },
div({ class: 'card-image' },
img({
src: walgreensUrl(offer.imageUrl),
loading: 'lazy',
alt: `Offer Image: ${offer.title}`,
}),
),
div({ class: 'card-body' },
p(strong(offer.title)),
offer.brand ? p(strong(offer.brand)) : '',
offer.offerDescription ? p(offer.offerDescription) : '',
),
),
)),
),
),
);
}
export default async function decorate(block) {
if (block.children.length === 1 && block.querySelectorAll('a').length === 1) {
await decorateAPICards(block);
} else {
decorateCuratedCards(block);
}
}