Skip to content

Commit 483b6b5

Browse files
committed
172: Worked on preview for playlist and slide
1 parent 754b7f2 commit 483b6b5

File tree

3 files changed

+141
-1
lines changed

3 files changed

+141
-1
lines changed

src/app.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ function App({ preview, previewId }) {
6161
function screenHandler(event) {
6262
const screenData = event.detail?.screen;
6363

64+
console.log('screenData', screenData);
65+
6466
if (screenData !== null) {
6567
setScreen(screenData);
6668
}
@@ -322,7 +324,23 @@ function App({ preview, previewId }) {
322324
document.addEventListener('contentEmpty', contentEmpty);
323325
document.addEventListener('contentNotEmpty', contentNotEmpty);
324326

325-
startContent(previewId);
327+
if (preview === 'screen') {
328+
startContent(previewId);
329+
return;
330+
}
331+
332+
setRunning(true);
333+
contentServiceRef.current = new ContentService();
334+
contentServiceRef.current.start();
335+
336+
document.dispatchEvent(
337+
new CustomEvent('startPreview', {
338+
detail: {
339+
mode: preview,
340+
id: previewId,
341+
},
342+
})
343+
);
326344
} else {
327345
const currentUrl = new URL(window.location.href);
328346

src/data-sync/pull-strategy.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import cloneDeep from 'lodash.clonedeep';
22
import isPublished from '../util/isPublished';
33
import Logger from '../logger/logger';
44
import ApiHelper from './api-helper';
5+
import apiHelper from "./api-helper";
56

67
/**
78
* PullStrategy.
@@ -408,6 +409,35 @@ class PullStrategy {
408409
document.dispatchEvent(event);
409410
}
410411

412+
getPath(id) {
413+
return this.apiHelper.getPath(id);
414+
}
415+
416+
async addTemplateData(slide) {
417+
return new Promise((resolve, reject) => {
418+
const newSlide = cloneDeep(slide);
419+
const templatePath = newSlide.templateInfo['@id'];
420+
421+
this.apiHelper.getPath(templatePath).then((templateData) => {
422+
resolve(templateData);
423+
});
424+
});
425+
}
426+
427+
async addFeedData(slide) {
428+
return new Promise((resolve, reject) => {
429+
const newSlide = cloneDeep(slide);
430+
431+
if (!newSlide?.feed?.feedUrl) {
432+
resolve([]);
433+
} else {
434+
this.apiHelper.getPath(newSlide.feed.feedUrl).then((feedData) => {
435+
resolve(feedData);
436+
});
437+
}
438+
});
439+
}
440+
411441
/**
412442
* Start the data synchronization.
413443
*/

src/service/contentService.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Logger from '../logger/logger';
44
import DataSync from '../data-sync/data-sync';
55
import ScheduleService from './scheduleService';
66
import ConfigLoader from '../config-loader';
7+
import PullStrategy from '../data-sync/pull-strategy';
78

89
/**
910
* ContentService.
@@ -176,6 +177,7 @@ class ContentService {
176177
document.addEventListener('content', this.contentHandler);
177178
document.addEventListener('regionReady', this.regionReadyHandler);
178179
document.addEventListener('regionRemoved', this.regionRemovedHandler);
180+
document.addEventListener('startPreview', this.startPreview);
179181
}
180182

181183
/**
@@ -189,6 +191,96 @@ class ContentService {
189191
document.removeEventListener('content', this.contentHandler);
190192
document.removeEventListener('regionReady', this.regionReadyHandler);
191193
document.removeEventListener('regionRemoved', this.regionRemovedHandler);
194+
document.removeEventListener('startPreview', this.startPreview);
195+
}
196+
197+
/**
198+
* Start preview.
199+
*
200+
* @param event
201+
*/
202+
async startPreview(event) {
203+
const data = event.detail;
204+
const { mode, id } = data;
205+
Logger.log('info', `Starting preview. Mode: ${mode}, ID: ${id}`);
206+
207+
const config = await ConfigLoader.loadConfig();
208+
209+
210+
if (mode === 'screen') {
211+
this.startSyncing(`/v2/screen/${id}`);
212+
} else if (mode === 'playlist') {
213+
const pullStrategy = new PullStrategy({
214+
endpoint: config.apiEndpoint,
215+
});
216+
217+
const playlist = await pullStrategy.getPath(
218+
`/v2/playlists/${id}`
219+
);
220+
221+
const playlistSlidesResponse = await pullStrategy.getPath(
222+
playlist.slides
223+
);
224+
225+
playlist.slidesData = playlistSlidesResponse['hydra:member'].map(
226+
(playlistSlide) => playlistSlide.slide
227+
);
228+
229+
for (const item of playlist.slidesData) {
230+
item.templateData = await pullStrategy.addTemplateData(item);
231+
}
232+
233+
const screen = {
234+
'@id': '/v2/screens/SCREEN01234567890123456789',
235+
'@type': 'Screen',
236+
title: 'Preview',
237+
description: 'Screen for preview.',
238+
layout: '/v2/layouts/LAYOUT01234567890123456789',
239+
regions: [
240+
'/v2/screens/SCREEN01234567890123456789/regions/REGION01234567890123456789/playlists',
241+
],
242+
regionData: {
243+
REGION01234567890123456789: [playlist],
244+
},
245+
layoutData: {
246+
'@id': '/v2/layouts/LAYOUT01234567890123456789',
247+
'@type': 'ScreenLayout',
248+
title: 'Full screen',
249+
grid: {
250+
rows: 1,
251+
columns: 1,
252+
},
253+
regions: [
254+
{
255+
'@type': 'ScreenLayoutRegions',
256+
'@id': '/v2/layouts/regions/REGION01234567890123456789',
257+
title: 'full',
258+
gridArea: ['a'],
259+
screenLayout: '/v2/layouts/LAYOUT01234567890123456789',
260+
},
261+
],
262+
},
263+
};
264+
265+
document.dispatchEvent(
266+
new CustomEvent('content', {
267+
detail: {
268+
screen,
269+
},
270+
})
271+
);
272+
} else if (mode === 'slide') {
273+
const pullStrategy = new PullStrategy({});
274+
// const slide = pullStrategy.getSlide(id);
275+
276+
const screen = {
277+
// TODO: Build fake screen.
278+
};
279+
280+
ContentService.emitScreen(screen);
281+
} else {
282+
Logger.error(`Unsupported preview mode: ${mode}.`);
283+
}
192284
}
193285

194286
/**

0 commit comments

Comments
 (0)