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

117 lines
2.5 KiB
JavaScript
Raw Normal View History

2023-08-01 01:25:30 +03:00
const {
SearchBox,
SearchBoxComposer,
DummyMapApi,
dummyCoffeeApi,
util
} = ourCoffeeSdk;
class CustomOfferList {
constructor(context, container, offerList) {
this.context = context;
this.container = container;
this.events = new util.EventEmitter();
this.offerList = null;
this.map = null;
this.onMarkerClick = (markerId) => {
this.events.emit("offerSelect", {
offerId: markerId
});
};
this.setOfferList = ({ offerList: newOfferList }) => {
if (this.map) {
this.map.destroy();
this.map = null;
}
this.offerList = newOfferList;
if (newOfferList) {
this.map = new DummyMapApi(this.container, [
[16.355, 48.2],
[16.375, 48.214]
]);
for (const offer of newOfferList) {
this.map.addMarker(
offer.offerId,
offer.location,
this.onMarkerClick
);
}
}
};
this.setOfferList({ offerList });
this.contextListeners = [
context.events.on(
"offerPreviewListChange",
this.setOfferList
),
context.events.on(
"offerFullViewToggle",
({ offer }) => {
this.map.selectSingleMarker(
offer && offer.offerId
);
}
)
];
}
destroy() {
if (this.map) {
this.map.destroy();
2023-07-30 23:04:39 +03:00
}
2023-08-01 01:25:30 +03:00
for (const listener of this.contextListeners) {
listener.off();
}
}
}
2023-07-30 23:04:39 +03:00
2023-08-01 01:25:30 +03:00
class CustomComposer extends SearchBoxComposer {
buildOfferListComponent(
context,
container,
offerList,
contextOptions
) {
return new CustomOfferList(
context,
container,
this.generateOfferPreviews(offerList, contextOptions),
this.generateOfferListComponentOptions(contextOptions)
2023-07-30 23:04:39 +03:00
);
2023-08-01 01:25:30 +03:00
}
generateOfferPreviews(offerList) {
const result = super.generateOfferPreviews(offerList);
2023-07-30 23:04:39 +03:00
return result === null
? result
: result.map((preview, index) => ({
...preview,
2023-08-01 01:25:30 +03:00
location: offerList[index].place.location
2023-07-30 23:04:39 +03:00
}));
}
}
2023-08-01 01:25:30 +03:00
class CustomSearchBox extends SearchBox {
2023-07-30 23:04:39 +03:00
buildComposer(context, container, options) {
2023-08-01 01:25:30 +03:00
return new CustomComposer(context, container, options);
2023-07-30 23:04:39 +03:00
}
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 23:04:39 +03:00
{
offerPanel: {
2023-08-01 01:25:30 +03:00
transparent: true
2023-07-30 23:04:39 +03:00
}
}
2023-07-30 15:25:21 +03:00
);
searchBox.search("Lungo");