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

106 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-07-30 23:04:39 +03:00
const {
SearchBox,
SearchBoxComposer,
OfferPanelComponent,
OfferPanelButton,
util,
dummyCoffeeApi
} = ourCoffeeSdk;
2023-07-30 15:25:21 +03:00
2023-07-30 23:04:39 +03:00
const buildCallButton = function (
offer,
container,
options
) {
return new OfferPanelButton("call", container, {
text: util.html`<a href="tel:${util.attrValue(
offer.phone
)}" style="color: inherit; text-decoration: none;">${
options.callButtonText
}</a>`
});
};
2023-07-30 15:25:21 +03:00
2023-07-30 23:04:39 +03:00
const buildCreateOrderButton =
OfferPanelComponent.buildCreateOrderButton;
const buildCloseButton =
OfferPanelComponent.buildCloseButton;
2023-07-30 15:25:21 +03:00
2023-07-30 23:04:39 +03:00
class CustomOfferPanel extends OfferPanelComponent {
show() {
this.options.buttonBuilders = this
.currentOffer.phone
? [
buildCreateOrderButton,
buildCallButton,
buildCloseButton
]
: [
buildCreateOrderButton,
buildCloseButton
];
return super.show();
2023-07-30 15:25:21 +03:00
}
}
2023-07-30 23:04:39 +03:00
class CustomComposer extends SearchBoxComposer {
buildOfferPanelComponent(
2023-07-30 15:25:21 +03:00
context,
container,
2023-07-30 23:04:39 +03:00
currentOffer,
2023-07-30 15:25:21 +03:00
contextOptions
) {
2023-07-30 23:04:39 +03:00
return new CustomOfferPanel(
2023-07-30 15:25:21 +03:00
context,
container,
2023-07-30 23:04:39 +03:00
this.generateCurrentOfferFullView(
currentOffer,
2023-07-30 15:25:21 +03:00
contextOptions
),
2023-07-30 23:04:39 +03:00
this.generateOfferPanelComponentOptions(
2023-07-30 15:25:21 +03:00
contextOptions
)
);
}
2023-07-30 23:04:39 +03:00
generateCurrentOfferFullView(offer, options) {
const offerFullView =
super.generateCurrentOfferFullView(
offer,
options
);
if (offer && offer.place.phone) {
offerFullView.phone = offer.place.phone;
}
return offerFullView;
}
2023-07-30 15:25:21 +03:00
}
2023-07-30 23:04:39 +03:00
class CustomSearchBox extends SearchBox {
2023-07-30 15:25:21 +03:00
buildComposer(context, container, options) {
return new CustomComposer(
context,
container,
options
);
}
createOrder(offer) {
alert(`Isn't actually implemented (yet)`);
return super.createOrder(offer);
}
}
const searchBox = new CustomSearchBox(
document.getElementById("search-box"),
2023-07-30 23:04:39 +03:00
dummyCoffeeApi,
{
offerPanel: {
createOrderButtonText: "🛒Place an Order",
callButtonText: "☎️ Make a Call",
closeButtonText: "❌Not Now"
}
}
2023-07-30 15:25:21 +03:00
);
searchBox.search("Lungo");