|
| 1 | +import React, { useEffect, Fragment, useState } from "react"; |
| 2 | +import dayjs from "dayjs"; |
| 3 | +import localizedFormat from "dayjs/plugin/localizedFormat"; |
| 4 | +import { FormattedMessage, IntlProvider } from "react-intl"; |
| 5 | +import BaseSlideExecution from "../slide-utils/base-slide-execution"; |
| 6 | +import da from "./brnd/lang/da.json"; |
| 7 | +import { |
| 8 | + getFirstMediaUrlFromField, |
| 9 | + ThemeStyles, |
| 10 | +} from "../slide-utils/slide-util"; |
| 11 | +import BrndSportcenterToday from "./brnd/brnd-sportcenter-today"; |
| 12 | +import GlobalStyles from "../slide-utils/GlobalStyles"; |
| 13 | +import "./brnd/brnd.scss"; |
| 14 | +import templateConfig from "./brnd.json"; |
| 15 | + |
| 16 | +function id() { |
| 17 | + return templateConfig.id; |
| 18 | +} |
| 19 | + |
| 20 | +function config() { |
| 21 | + return templateConfig; |
| 22 | +} |
| 23 | + |
| 24 | +function renderSlide(slide, run, slideDone) { |
| 25 | + return ( |
| 26 | + <Brnd |
| 27 | + slide={slide} |
| 28 | + run={run} |
| 29 | + slideDone={slideDone} |
| 30 | + content={slide.content} |
| 31 | + executionId={slide.executionId} |
| 32 | + /> |
| 33 | + ); |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Brnd component. |
| 38 | + * |
| 39 | + * @param {object} props Props. |
| 40 | + * @param {object} props.slide The slide. |
| 41 | + * @param {object} props.content The slide content. |
| 42 | + * @param {string} props.run Whether or not the slide should start running. |
| 43 | + * @param {Function} props.slideDone Function to invoke when the slide is done playing. |
| 44 | + * @param {string} props.executionId Unique id for the instance. |
| 45 | + * @returns {JSX.Element} The component. |
| 46 | + */ |
| 47 | +function Brnd({ slide, content, run, slideDone, executionId }) { |
| 48 | + const [translations, setTranslations] = useState(); |
| 49 | + |
| 50 | + const { |
| 51 | + layout = "sportcenter-today", |
| 52 | + duration = 15000, |
| 53 | + fontSize, |
| 54 | + resourceUnavailableText, |
| 55 | + } = content; |
| 56 | + const { feedData = [] } = slide; |
| 57 | + |
| 58 | + const classes = ["template-brnd", fontSize]; |
| 59 | + const rootStyle = {}; |
| 60 | + |
| 61 | + const imageUrl = getFirstMediaUrlFromField(slide.mediaData, content.image); |
| 62 | + |
| 63 | + if (imageUrl) { |
| 64 | + rootStyle["--bg-image"] = `url("${imageUrl}")`; |
| 65 | + } |
| 66 | + |
| 67 | + /** Setup slide run function. */ |
| 68 | + const slideExecution = new BaseSlideExecution(slide, slideDone); |
| 69 | + useEffect(() => { |
| 70 | + if (run) { |
| 71 | + slideExecution.start(duration); |
| 72 | + } |
| 73 | + |
| 74 | + return function cleanup() { |
| 75 | + slideExecution.stop(); |
| 76 | + }; |
| 77 | + }, [run]); |
| 78 | + |
| 79 | + /** Imports language strings, sets localized formats. */ |
| 80 | + useEffect(() => { |
| 81 | + dayjs.extend(localizedFormat); |
| 82 | + |
| 83 | + setTranslations(da); |
| 84 | + }, []); |
| 85 | + |
| 86 | + const getTitle = (eventTitle) => { |
| 87 | + if (!eventTitle || eventTitle === "") { |
| 88 | + if (resourceUnavailableText) { |
| 89 | + return resourceUnavailableText; |
| 90 | + } |
| 91 | + return <FormattedMessage id="unavailable" defaultMessage="Unavailable" />; |
| 92 | + } |
| 93 | + return eventTitle; |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <IntlProvider messages={translations} locale="da" defaultLocale="da"> |
| 99 | + {layout === "sportcenter-today" && ( |
| 100 | + <BrndSportcenterToday |
| 101 | + bookings={feedData.bookings} |
| 102 | + content={content} |
| 103 | + templateClasses={classes} |
| 104 | + templateRootStyle={rootStyle} |
| 105 | + getTitle={getTitle} |
| 106 | + /> |
| 107 | + )} |
| 108 | + </IntlProvider> |
| 109 | + <ThemeStyles id={executionId} css={slide?.theme?.cssStyles} /> |
| 110 | + <GlobalStyles /> |
| 111 | + </> |
| 112 | + ); |
| 113 | +} |
| 114 | + |
| 115 | +export default { id, config, renderSlide }; |
0 commit comments