Skip to content

Commit 0322d05

Browse files
committed
173: Added slide preview
1 parent ff48627 commit 0322d05

File tree

4 files changed

+123
-57
lines changed

4 files changed

+123
-57
lines changed

src/app.js

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

64-
console.log('screenData', screenData);
65-
6664
if (screenData !== null) {
6765
setScreen(screenData);
6866
}

src/data-sync/pull-strategy.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -413,31 +413,36 @@ class PullStrategy {
413413
return this.apiHelper.getPath(id);
414414
}
415415

416-
async addTemplateData(slide) {
416+
async getTemplateData(slide) {
417417
return new Promise((resolve, reject) => {
418-
const newSlide = cloneDeep(slide);
419-
const templatePath = newSlide.templateInfo['@id'];
418+
const templatePath = slide.templateInfo['@id'];
420419

421-
this.apiHelper.getPath(templatePath).then((templateData) => {
422-
resolve(templateData);
420+
this.apiHelper.getPath(templatePath).then((data) => {
421+
resolve(data);
423422
});
424423
});
425424
}
426425

427-
async addFeedData(slide) {
426+
async getFeedData(slide) {
428427
return new Promise((resolve, reject) => {
429-
const newSlide = cloneDeep(slide);
430-
431-
if (!newSlide?.feed?.feedUrl) {
428+
if (!slide?.feed?.feedUrl) {
432429
resolve([]);
433430
} else {
434-
this.apiHelper.getPath(newSlide.feed.feedUrl).then((feedData) => {
435-
resolve(feedData);
431+
this.apiHelper.getPath(slide.feed.feedUrl).then((data) => {
432+
resolve(data);
436433
});
437434
}
438435
});
439436
}
440437

438+
async getMediaData(media) {
439+
return new Promise((resolve, reject) => {
440+
this.apiHelper.getPath(media).then((data) => {
441+
resolve(data);
442+
});
443+
});
444+
}
445+
441446
/**
442447
* Start the data synchronization.
443448
*/

src/service/contentService.js

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import sha256 from 'crypto-js/sha256';
22
import Base64 from 'crypto-js/enc-base64';
3+
import cloneDeep from 'lodash.clonedeep';
4+
import { object, string } from 'prop-types';
35
import Logger from '../logger/logger';
46
import DataSync from '../data-sync/data-sync';
57
import ScheduleService from './scheduleService';
68
import ConfigLoader from '../config-loader';
79
import PullStrategy from '../data-sync/pull-strategy';
10+
import {
11+
screenForPlaylistPreview,
12+
screenForSlidePreview,
13+
} from '../util/preview';
814

915
/**
1016
* ContentService.
@@ -197,7 +203,7 @@ class ContentService {
197203
/**
198204
* Start preview.
199205
*
200-
* @param event
206+
* @param {CustomEvent} event The event.
201207
*/
202208
async startPreview(event) {
203209
const data = event.detail;
@@ -206,17 +212,14 @@ class ContentService {
206212

207213
const config = await ConfigLoader.loadConfig();
208214

209-
210215
if (mode === 'screen') {
211216
this.startSyncing(`/v2/screen/${id}`);
212217
} else if (mode === 'playlist') {
213218
const pullStrategy = new PullStrategy({
214219
endpoint: config.apiEndpoint,
215220
});
216221

217-
const playlist = await pullStrategy.getPath(
218-
`/v2/playlists/${id}`
219-
);
222+
const playlist = await pullStrategy.getPath(`/v2/playlists/${id}`);
220223

221224
const playlistSlidesResponse = await pullStrategy.getPath(
222225
playlist.slides
@@ -226,41 +229,13 @@ class ContentService {
226229
(playlistSlide) => playlistSlide.slide
227230
);
228231

229-
for (const item of playlist.slidesData) {
230-
item.templateData = await pullStrategy.addTemplateData(item);
232+
// eslint-disable-next-line no-restricted-syntax
233+
for (const slide of playlist.slidesData) {
234+
// eslint-disable-next-line no-await-in-loop
235+
await ContentService.attachReferencesToSlide(pullStrategy, slide);
231236
}
232237

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-
};
238+
const screen = screenForPlaylistPreview(playlist);
264239

265240
document.dispatchEvent(
266241
new CustomEvent('content', {
@@ -270,19 +245,47 @@ class ContentService {
270245
})
271246
);
272247
} else if (mode === 'slide') {
273-
const pullStrategy = new PullStrategy({});
274-
// const slide = pullStrategy.getSlide(id);
248+
const pullStrategy = new PullStrategy({
249+
endpoint: config.apiEndpoint,
250+
});
275251

276-
const screen = {
277-
// TODO: Build fake screen.
278-
};
252+
const slide = await pullStrategy.getPath(`/v2/slides/${id}`);
279253

280-
ContentService.emitScreen(screen);
254+
// eslint-disable-next-line no-await-in-loop
255+
await ContentService.attachReferencesToSlide(pullStrategy, slide);
256+
257+
const screen = screenForSlidePreview(slide);
258+
259+
document.dispatchEvent(
260+
new CustomEvent('content', {
261+
detail: {
262+
screen,
263+
},
264+
})
265+
);
281266
} else {
282267
Logger.error(`Unsupported preview mode: ${mode}.`);
283268
}
284269
}
285270

271+
static async attachReferencesToSlide(strategy, slide) {
272+
/* eslint-disable no-param-reassign */
273+
slide.templateData = await strategy.getTemplateData(slide);
274+
slide.feedData = await strategy.getFeedData(slide);
275+
276+
slide.mediaData = {};
277+
// eslint-disable-next-line no-restricted-syntax
278+
for (const media of slide.media) {
279+
// eslint-disable-next-line no-await-in-loop
280+
slide.mediaData[media] = await strategy.getMediaData(media);
281+
}
282+
283+
if (typeof slide.theme === 'string' || slide.theme instanceof String) {
284+
slide.theme = await strategy.getPath(slide.theme);
285+
}
286+
/* eslint-enable no-param-reassign */
287+
}
288+
286289
/**
287290
* Emit screen.
288291
*

src/util/preview.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Create screen data for displaying playlist preview.
3+
*
4+
* @param {object} playlist Playlist data.
5+
* @returns {object} Screen data.
6+
*/
7+
function screenForPlaylistPreview(playlist) {
8+
return {
9+
'@id': '/v2/screens/SCREEN01234567890123456789',
10+
'@type': 'Screen',
11+
title: 'Preview',
12+
description: 'Screen for preview.',
13+
layout: '/v2/layouts/LAYOUT01234567890123456789',
14+
regions: [
15+
'/v2/screens/SCREEN01234567890123456789/regions/REGION01234567890123456789/playlists',
16+
],
17+
regionData: {
18+
REGION01234567890123456789: [playlist],
19+
},
20+
layoutData: {
21+
'@id': '/v2/layouts/LAYOUT01234567890123456789',
22+
'@type': 'ScreenLayout',
23+
title: 'Full screen',
24+
grid: {
25+
rows: 1,
26+
columns: 1,
27+
},
28+
regions: [
29+
{
30+
'@type': 'ScreenLayoutRegions',
31+
'@id': '/v2/layouts/regions/REGION01234567890123456789',
32+
title: 'full',
33+
gridArea: ['a'],
34+
screenLayout: '/v2/layouts/LAYOUT01234567890123456789',
35+
},
36+
],
37+
},
38+
};
39+
}
40+
41+
/**
42+
* Create screen data for displaying slide preview.
43+
*
44+
* @param {object} slide Slide data.
45+
* @returns {object} Screen data.
46+
*/
47+
function screenForSlidePreview(slide) {
48+
const playlist = {
49+
'@id': '/v2/playlists/01HT6WPZCR50W8EF9004PVQ11P',
50+
'@type': 'Playlist',
51+
title: 'Preview playlist',
52+
description: 'Playlist for preview',
53+
schedules: [],
54+
slides: '/v2/playlists/PLAYLIST12345678901234567/slides',
55+
slidesData: [slide],
56+
};
57+
return screenForPlaylistPreview(playlist);
58+
}
59+
60+
export { screenForPlaylistPreview, screenForSlidePreview };

0 commit comments

Comments
 (0)