2023-08-01 01:25:30 +03:00
|
|
|
const {
|
|
|
|
SearchBox,
|
|
|
|
SearchBoxComposer,
|
|
|
|
OfferPanelComponent,
|
|
|
|
OfferPanelButton,
|
|
|
|
util,
|
|
|
|
dummyCoffeeApi
|
|
|
|
} = ourCoffeeSdk;
|
|
|
|
|
|
|
|
const { buildCreateOrderButton, buildCloseButton } =
|
|
|
|
OfferPanelComponent;
|
|
|
|
|
|
|
|
const buildCustomOrderButton = function (offer, container) {
|
|
|
|
return OfferPanelComponent.buildCreateOrderButton(
|
|
|
|
offer,
|
2023-07-30 23:04:39 +03:00
|
|
|
container,
|
2023-08-01 01:25:30 +03:00
|
|
|
{
|
|
|
|
createOrderButtonUrl:
|
|
|
|
offer && offer.createOrderButtonIcon,
|
|
|
|
createOrderButtonText:
|
|
|
|
(offer &&
|
|
|
|
`Buy now for just ${offer.price.formattedValue}`) ||
|
|
|
|
"Place an Order"
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
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-08-01 01:25:30 +03:00
|
|
|
const buildPreviousOfferButton = function (
|
|
|
|
offer,
|
|
|
|
container
|
|
|
|
) {
|
|
|
|
return new NavigateButton(
|
|
|
|
"left",
|
|
|
|
offer.previousOfferId,
|
|
|
|
container
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const buildNextOfferButton = function (offer, container) {
|
|
|
|
return new NavigateButton(
|
|
|
|
"right",
|
|
|
|
offer.nextOfferId,
|
|
|
|
container
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
class NavigateButton {
|
|
|
|
constructor(direction, offerId, container) {
|
|
|
|
this.action = "navigate";
|
|
|
|
this.offerId = offerId;
|
|
|
|
this.events = new util.EventEmitter();
|
|
|
|
const button = (this.button =
|
|
|
|
document.createElement("button"));
|
|
|
|
button.innerHTML = direction === "left" ? "⟨" : "⟩";
|
|
|
|
button.className = direction;
|
|
|
|
container.classList.add("custom-control");
|
|
|
|
this.container = container;
|
|
|
|
this.listener = () =>
|
|
|
|
this.events.emit("press", {
|
|
|
|
target: this
|
|
|
|
});
|
|
|
|
button.addEventListener("click", this.listener);
|
|
|
|
container.appendChild(button);
|
2023-07-30 23:04:39 +03:00
|
|
|
}
|
|
|
|
|
2023-08-01 01:25:30 +03:00
|
|
|
destroy() {
|
|
|
|
this.button.removeEventListener("click", this.listener);
|
|
|
|
this.button.parentElement.removeChild(this.button);
|
|
|
|
this.container.classList.remove("custom-control");
|
2023-07-30 15:25:21 +03:00
|
|
|
}
|
2023-08-01 01:25:30 +03:00
|
|
|
}
|
2023-07-30 15:25:21 +03:00
|
|
|
|
2023-08-01 01:25:30 +03:00
|
|
|
class CustomOfferPanel extends OfferPanelComponent {
|
|
|
|
show() {
|
|
|
|
const buttons = [];
|
|
|
|
const offer = this.currentOffer;
|
|
|
|
if (offer.previousOfferId) {
|
|
|
|
buttons.push(buildPreviousOfferButton);
|
2023-07-30 15:25:21 +03:00
|
|
|
}
|
2023-08-01 01:25:30 +03:00
|
|
|
buttons.push(buildCreateOrderButton);
|
|
|
|
if (offer.phone) {
|
|
|
|
buttons.push(buildCallButton);
|
|
|
|
}
|
|
|
|
buttons.push(buildCloseButton);
|
|
|
|
if (offer.nextOfferId) {
|
|
|
|
buttons.push(buildNextOfferButton);
|
|
|
|
}
|
|
|
|
this.options.buttonBuilders = buttons;
|
|
|
|
super.show();
|
2023-07-30 15:25:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-01 01:25:30 +03:00
|
|
|
class CustomComposer extends SearchBoxComposer {
|
|
|
|
buildOfferPanelComponent(
|
2023-07-30 15:25:21 +03:00
|
|
|
context,
|
|
|
|
container,
|
2023-08-01 01:25:30 +03:00
|
|
|
currentOffer,
|
2023-07-30 15:25:21 +03:00
|
|
|
contextOptions
|
|
|
|
) {
|
2023-08-01 01:25:30 +03:00
|
|
|
return new CustomOfferPanel(
|
2023-07-30 15:25:21 +03:00
|
|
|
context,
|
|
|
|
container,
|
2023-08-01 01:25:30 +03:00
|
|
|
this.generateCurrentOfferFullView(
|
|
|
|
currentOffer,
|
2023-07-30 15:25:21 +03:00
|
|
|
contextOptions
|
|
|
|
),
|
2023-08-01 01:25:30 +03:00
|
|
|
{
|
|
|
|
...CustomComposer.DEFAULT_OPTIONS,
|
|
|
|
...this.generateOfferPanelComponentOptions(
|
|
|
|
contextOptions
|
|
|
|
)
|
|
|
|
}
|
2023-07-30 15:25:21 +03:00
|
|
|
);
|
|
|
|
}
|
2023-08-01 01:25:30 +03:00
|
|
|
|
|
|
|
generateOfferPreviews(offerList) {
|
|
|
|
const result = super.generateOfferPreviews(offerList);
|
|
|
|
return result === null
|
|
|
|
? result
|
|
|
|
: result.map((preview, index) => ({
|
|
|
|
...preview,
|
|
|
|
imageUrl: offerList[index].place.icon
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
generateCurrentOfferFullView(offer, options) {
|
|
|
|
const offerFullView =
|
|
|
|
super.generateCurrentOfferFullView(offer, options);
|
|
|
|
if (offer) {
|
|
|
|
if (offer.place.phone) {
|
|
|
|
offerFullView.phone = offer.place.phone;
|
|
|
|
}
|
|
|
|
if (offer.place.icon) {
|
|
|
|
offerFullView.createOrderButtonIcon =
|
|
|
|
offer.place.icon;
|
|
|
|
}
|
|
|
|
if (this.offerList) {
|
|
|
|
const offers = this.offerList;
|
|
|
|
const index = offers.findIndex(
|
|
|
|
({ offerId }) => offerId === offer.offerId
|
|
|
|
);
|
|
|
|
if (index > 0) {
|
|
|
|
offerFullView.previousOfferId =
|
|
|
|
offers[index - 1].offerId;
|
|
|
|
}
|
|
|
|
if (index < offers.length - 1 && index >= 0) {
|
|
|
|
offerFullView.nextOfferId =
|
|
|
|
offers[index + 1].offerId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return offerFullView;
|
|
|
|
}
|
|
|
|
|
|
|
|
performAction(event) {
|
|
|
|
if (event.action === "navigate") {
|
|
|
|
this.selectOffer(event.target.offerId);
|
|
|
|
} else {
|
|
|
|
super.performAction(event);
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 15:25:21 +03:00
|
|
|
}
|
|
|
|
|
2023-08-01 01:25:30 +03:00
|
|
|
CustomComposer.DEFAULT_OPTIONS = {
|
|
|
|
createOrderButtonText: "🛒Place an Order",
|
|
|
|
callButtonText: "☎️ Make a Call",
|
|
|
|
closeButtonText: "❌Not Now"
|
|
|
|
};
|
|
|
|
|
|
|
|
class CustomSearchBox extends SearchBox {
|
2023-07-30 15:25:21 +03:00
|
|
|
buildComposer(context, container, options) {
|
2023-08-01 01:25:30 +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-01 01:25:30 +03:00
|
|
|
dummyCoffeeApi
|
2023-07-30 15:25:21 +03:00
|
|
|
);
|
|
|
|
searchBox.search("Lungo");
|