Skip to content

Commit ffd77b0

Browse files
committed
Initial commit for fetchOffers
1 parent 68faea7 commit ffd77b0

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed

lib/kits/paged-publication/bootstrapper.ts

+19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default class Bootstrapper {
3030
keyboard: this.options.keyboard ?? 'enabled',
3131
pageId: this.options.pageId,
3232
eventTracker: this.options.eventTracker,
33+
fetchOffers: this.fetchOffers,
3334
pages: data.pages.map(({view, zoom}, i) => {
3435
const pageNumber = i + 1;
3536

@@ -170,4 +171,22 @@ export default class Bootstrapper {
170171
applyPageDecorations(viewer: Viewer, pageDecorations: V2PageDecoration[]) {
171172
viewer.applyPageDecorations(pageDecorations);
172173
}
174+
175+
fetchOffers = (offerIds: string[]) => {
176+
// if (this.options.viewerUrl) {
177+
// return request({
178+
// apiKey: this.options.apiKey,
179+
// coreUrl: this.options.viewerUrl,
180+
// url: `/api/paged-publications/${this.options.id}/${pageId}`
181+
// });
182+
// }
183+
184+
return request({
185+
apiKey: this.options.apiKey,
186+
coreUrl: this.options.coreUrl,
187+
url: `/v2/offers?offer_ids=${offerIds?.join(',')}`
188+
});
189+
190+
return false;
191+
};
173192
}

lib/kits/paged-publication/viewer.ts

+50-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import MicroEvent from '../../../vendor/microevent';
22
import * as translations from '../../translations';
33
import Verso from '../../verso-browser/verso';
4-
import {V2Hotspot, V2PageDecoration} from '../core';
4+
import {V2Hotspot, V2PageDecoration, V2Offer} from '../core';
55
import PageDecorations from '../core-ui/page-decorations';
66
import singleChoicePopover from '../core-ui/single-choice-popover';
77
import {Tracker} from '../events';
@@ -56,6 +56,7 @@ export interface ViewerInit {
5656
keyboard: 'disabled' | 'enabled' | 'global';
5757
hotspotRatio?: number;
5858
pickHotspot?: typeof defaultPickHotspot;
59+
fetchOffers: any;
5960
}
6061
class Viewer extends MicroEvent {
6162
_hotspots = new Hotspots();
@@ -94,6 +95,8 @@ class Viewer extends MicroEvent {
9495
_eventTracking: EventTracking;
9596
pageDecorations: V2PageDecoration[];
9697
options: ViewerInit;
98+
offersCache: V2Offer[] = [];
99+
pagesCache: number[] = [];
97100
// @ts-expect-error
98101
constructor(el: HTMLElement, options: ViewerInit = {}) {
99102
super();
@@ -183,9 +186,10 @@ class Viewer extends MicroEvent {
183186
this._eventTracking.trigger('pageLoaded', e);
184187
this.trigger('pageLoaded', e);
185188
});
186-
this._core.bind('pagesLoaded', (e) => {
189+
this._core.bind('pagesLoaded', async (e) => {
187190
this._hotspots.trigger('pagesLoaded', e);
188191
this.trigger('pagesLoaded', e);
192+
await this.processOffers(e);
189193
});
190194
this._core.bind('resized', (e) => {
191195
this._hotspots.trigger('resized');
@@ -336,6 +340,8 @@ class Viewer extends MicroEvent {
336340
(pageSpread) => pageSpread.getId() === hotspotRequest.id
337341
);
338342

343+
console.log('hotspots:::', hotspots);
344+
339345
this._hotspots.trigger('hotspotsReceived', {
340346
pageSpread: this._core.pageSpreads.get(hotspotRequest.id),
341347
versoPageSpread,
@@ -349,6 +355,7 @@ class Viewer extends MicroEvent {
349355
}
350356

351357
hotspotsRequested = (e) => {
358+
console.log('hjotspotsRequested:::', e);
352359
this.hotspotQueue.push(e);
353360
this.processHotspotQueue();
354361
};
@@ -443,6 +450,47 @@ class Viewer extends MicroEvent {
443450
this.trigger('hotspotPressed', hotspot);
444451
});
445452
};
453+
454+
// getHotspotsByPageNumber(pageNumber: number): {
455+
// if (!this.hotspots) return [];
456+
457+
// return Object.values(this.hotspots).filter(hotspot =>
458+
// hotspot.locations.hasOwnProperty(pageNumber)
459+
// );
460+
// }
461+
462+
getHotspotIdsByPageNumber = (pageNumber: number): string[] => {
463+
if (!this.hotspots) {
464+
return [];
465+
}
466+
467+
return Object.values(this.hotspots)
468+
.filter((hotspot) => hotspot.locations.hasOwnProperty(pageNumber))
469+
.map((hotspot) => hotspot.id);
470+
};
471+
472+
processOffers = async (e) => {
473+
for (const {pageNumber} of e.pages) {
474+
if (!this.pagesCache.includes(pageNumber)) {
475+
const hotspotIds = this.getHotspotIdsByPageNumber(pageNumber);
476+
console.log('hotspotIds', hotspotIds);
477+
if (hotspotIds.length) {
478+
const offers = await this.options.fetchOffers(hotspotIds);
479+
if (offers) {
480+
this.pagesCache.push(pageNumber);
481+
this.offersCache.push(...offers);
482+
483+
console.log('saved!', pageNumber);
484+
console.log('offers', offers);
485+
}
486+
}
487+
}
488+
}
489+
490+
console.log('pagesCache', this.pagesCache);
491+
console.log('offersCache', this.offersCache);
492+
// offers: V2Offer[]
493+
};
446494
}
447495

448496
export default Viewer;

0 commit comments

Comments
 (0)