From 95fd9293661c29e58e9cda36278fe9569811700e Mon Sep 17 00:00:00 2001 From: kilemensi Date: Fri, 10 Nov 2023 16:32:42 +0300 Subject: [PATCH] Switch from Flourish data script to iframe This means this component can no longer be reusable for different kind of charts and hence the renaming --- .../components/EmbeddedChart/EmbeddedChart.js | 38 ------ .../src/components/EmbeddedChart/index.js | 3 - .../components/FlourishChart/FlourishChart.js | 61 ++++++++++ .../FlourishChart.snap.js} | 17 ++- .../FlourishChart.test.js} | 13 +- .../src/components/FlourishChart/index.js | 3 + .../src/pages/[...slugs].page.js | 4 +- .../src/payload/blocks/EmbeddedChart.js | 112 ------------------ .../src/payload/blocks/FlourishChart.js | 62 ++++++++++ .../src/payload/collections/Pages.js | 4 +- 10 files changed, 142 insertions(+), 175 deletions(-) delete mode 100644 apps/charterafrica/src/components/EmbeddedChart/EmbeddedChart.js delete mode 100644 apps/charterafrica/src/components/EmbeddedChart/index.js create mode 100644 apps/charterafrica/src/components/FlourishChart/FlourishChart.js rename apps/charterafrica/src/components/{EmbeddedChart/EmbeddedChart.snap.js => FlourishChart/FlourishChart.snap.js} (69%) rename apps/charterafrica/src/components/{EmbeddedChart/EmbeddedChart.test.js => FlourishChart/FlourishChart.test.js} (65%) create mode 100644 apps/charterafrica/src/components/FlourishChart/index.js delete mode 100644 apps/charterafrica/src/payload/blocks/EmbeddedChart.js create mode 100644 apps/charterafrica/src/payload/blocks/FlourishChart.js diff --git a/apps/charterafrica/src/components/EmbeddedChart/EmbeddedChart.js b/apps/charterafrica/src/components/EmbeddedChart/EmbeddedChart.js deleted file mode 100644 index 91b392030..000000000 --- a/apps/charterafrica/src/components/EmbeddedChart/EmbeddedChart.js +++ /dev/null @@ -1,38 +0,0 @@ -import { RichTypography, Section } from "@commons-ui/core"; -import { Box } from "@mui/material"; -import React from "react"; - -const EmbeddedChart = React.forwardRef(function EmbeddedChart(props, ref) { - const { backgroundColor, html, subtitle, title, width } = props; - - return ( - -
- - {title} - - - {subtitle} - - {html} -
-
- ); -}); - -export default EmbeddedChart; diff --git a/apps/charterafrica/src/components/EmbeddedChart/index.js b/apps/charterafrica/src/components/EmbeddedChart/index.js deleted file mode 100644 index f99e65e36..000000000 --- a/apps/charterafrica/src/components/EmbeddedChart/index.js +++ /dev/null @@ -1,3 +0,0 @@ -import EmbeddedChart from "./EmbeddedChart"; - -export default EmbeddedChart; diff --git a/apps/charterafrica/src/components/FlourishChart/FlourishChart.js b/apps/charterafrica/src/components/FlourishChart/FlourishChart.js new file mode 100644 index 000000000..e314266b5 --- /dev/null +++ b/apps/charterafrica/src/components/FlourishChart/FlourishChart.js @@ -0,0 +1,61 @@ +/* eslint-env browser */ +import { RichTypography, Section } from "@commons-ui/core"; +import { Box } from "@mui/material"; +import React, { useEffect, useState } from "react"; + +const FlourishChart = React.forwardRef(function FlourishChart(props, ref) { + const { backgroundColor, chartId, subtitle, title } = props; + const [height, setHeight] = useState(0); + + useEffect(() => { + const handleMessageEvent = (event) => { + const { sender, context, ...message } = JSON.parse(event.data) || {}; + if (sender === "Flourish" && context === "iframe.resize") { + setHeight(Math.ceil(message.height)); + } + }; + window.addEventListener("message", handleMessageEvent); + return () => { + document.removeEventListener("message", handleMessageEvent); + }; + }); + + if (!chartId?.length) { + return null; + } + return ( + +
+ + {title} + + + {subtitle} + +