1
0
mirror of https://github.com/twirl/The-API-Book.git synced 2025-05-13 21:26:26 +02:00

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-08-04 10:49:50 +03:00
const {
SearchBox,
SearchBoxComposer,
OfferListComponent,
dummyCoffeeApi,
util
} = ourCoffeeSdk;
class CustomOfferList extends OfferListComponent {
constructor(context, container, offerList, options) {
2023-08-01 01:25:30 +03:00
super(context, container, offerList, options);
2023-08-04 10:49:50 +03:00
this.onClickListener = (e) => {
const { target, value: action } = util.findDataField(
e.target,
"action"
);
if (target === null || action === null) {
return;
}
const { target: container, value: offerId } =
util.findDataField(target, "offerId");
if (container === null || offerId === null) {
return;
}
switch (action) {
case "expand":
this.expand(container);
break;
case "collapse":
this.collapse(container);
break;
case "createOrder":
this.context.createOrder(offerId);
break;
2023-07-30 23:04:39 +03:00
}
};
2023-08-01 01:25:30 +03:00
}
2023-07-30 23:04:39 +03:00
2023-08-04 10:49:50 +03:00
expand(item) {
item.classList.add("expanded");
2023-08-01 01:25:30 +03:00
}
2023-08-04 10:49:50 +03:00
collapse(item) {
item.classList.remove("expanded");
2023-07-30 23:04:39 +03:00
}
2023-08-04 10:49:50 +03:00
generateOfferHtml(offer) {
return util.html`<li
class="custom-offer"
data-offer-id="${util.attrValue(offer.offerId)}"
>
<button data-action="expand" class="preview"><aside class="expand-action">&gt;</aside><strong>${
offer.title
}</strong> · ${offer.price.formattedValue}</button>
<div class="fullview">
<button data-action="collapse" class="collapse-action"></button>
<div><strong>${offer.title}</strong> · ${
offer.price.formattedValue
}</div>
<div>${offer.subtitle}</div>
<div>${offer.bottomLine}</div>
<button data-action="createOrder" class="action-button">Place an Order</button>
</div>
</li>`.toString();
2023-07-30 23:04:39 +03:00
}
}
2023-07-30 15:25:21 +03:00
2023-08-04 10:49:50 +03:00
class CustomComposer extends SearchBoxComposer {
2023-07-30 23:04:39 +03:00
buildOfferListComponent(
context,
container,
offerList,
contextOptions
) {
return new CustomOfferList(
context,
container,
2023-08-04 10:49:50 +03:00
this.generateOfferPreviews(offerList, contextOptions),
this.generateOfferListComponentOptions(contextOptions)
2023-07-30 23:04:39 +03:00
);
}
2023-07-30 15:25:21 +03:00
}
2023-08-04 10:49:50 +03:00
class CustomSearchBox extends SearchBox {
2023-07-30 15:25:21 +03:00
buildComposer(context, container, options) {
2023-08-04 10:49:50 +03:00
return new CustomComposer(context, container, options);
2023-07-30 15:25:21 +03:00
}
createOrder(offer) {
alert(`Isn't actually implemented (yet)`);
return super.createOrder(offer);
}
}
const searchBox = new CustomSearchBox(
document.getElementById("search-box"),
2023-08-04 10:49:50 +03:00
dummyCoffeeApi
2023-07-30 15:25:21 +03:00
);
searchBox.search("Lungo");