Skip to content

Commit

Permalink
Switch from Flourish data script to iframe
Browse files Browse the repository at this point in the history
This means this component can no longer be reusable for different kind
of charts and hence the renaming
  • Loading branch information
kilemensi committed Nov 10, 2023
1 parent 75db70d commit 95fd929
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 175 deletions.
38 changes: 0 additions & 38 deletions apps/charterafrica/src/components/EmbeddedChart/EmbeddedChart.js

This file was deleted.

3 changes: 0 additions & 3 deletions apps/charterafrica/src/components/EmbeddedChart/index.js

This file was deleted.

61 changes: 61 additions & 0 deletions apps/charterafrica/src/components/FlourishChart/FlourishChart.js
Original file line number Diff line number Diff line change
@@ -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 (
<Box
sx={{
backgroundColor: { backgroundColor },
}}
ref={ref}
>
<Section
sx={{
px: { xs: 5, sm: 0 },
py: 2.5,
}}
>
<RichTypography html={false} variant="h5SemiBold" color="neutral.dark">
{title}
</RichTypography>
<RichTypography color="neutral.dark" variant="p1" sx={{ my: 1 }}>
{subtitle}
</RichTypography>
<iframe
key={chartId}
title={title || "Flourish Chart "}
src={`https://flo.uri.sh/${chartId}/embed?auto=1`}
scrolling="no"
style={{
border: 0,
height,
width: "100%",
}}
/>
</Section>
</Box>
);
});

export default FlourishChart;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`<EmbeddedChart /> renders unchanged 1`] = `
exports[`<FlourishChart /> renders unchanged 1`] = `
<div>
<div
class="MuiBox-root css-1k9ek97"
Expand All @@ -13,18 +13,17 @@ exports[`<EmbeddedChart /> renders unchanged 1`] = `
>
Flourish Charts
</h5>
<p
<div
class="MuiTypography-root MuiTypography-p1 css-h5smwp-MuiTypography-root"
>
Flourish Charts
</p>
<div
class="MuiTypography-root MuiTypography-p1 css-a3yf55-MuiTypography-root"
>
<div>
Flourish Charts
</div>
</div>
<iframe
scrolling="no"
src="https://flo.uri.sh/story/1946372/embed?auto=1"
style="border: 0px; height: 0px; width: 100%;"
title="Flourish Charts"
/>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRender } from "@commons-ui/testing-library";
import React from "react";

import EmbeddedChart from "./EmbeddedChart";
import FlourishChart from "./FlourishChart";

import theme from "@/charterafrica/theme";

Expand All @@ -10,18 +10,13 @@ const render = createRender({ theme });

const defaultProps = {
backgroundColor: "#fff",
html: "<div>Flourish Charts</div>",
chartId: "story/1946372",
subtitle: "Flourish Charts",
title: "Flourish Charts",
width: {
xs: "100%",
sm: "100%",
md: "100%",
},
};

describe("<EmbeddedChart />", () => {
const { container } = render(<EmbeddedChart {...defaultProps} />);
describe("<FlourishChart />", () => {
const { container } = render(<FlourishChart {...defaultProps} />);

it("renders unchanged", async () => {
await expect(container).toMatchSnapshot();
Expand Down
3 changes: 3 additions & 0 deletions apps/charterafrica/src/components/FlourishChart/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import FlourishChart from "./FlourishChart";

export default FlourishChart;
4 changes: 2 additions & 2 deletions apps/charterafrica/src/pages/[...slugs].page.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import Datasets from "@/charterafrica/components/Datasets";
import Document from "@/charterafrica/components/Document";
import Documents from "@/charterafrica/components/Documents";
import Ecosystem from "@/charterafrica/components/Ecosystem";
import EmbeddedChart from "@/charterafrica/components/EmbeddedChart";
import Entity from "@/charterafrica/components/Entity";
import Explainers from "@/charterafrica/components/Explainers";
import FAQ from "@/charterafrica/components/FAQ";
import FeaturedPostCard from "@/charterafrica/components/FeaturedPostCard";
import FlourishChart from "@/charterafrica/components/FlourishChart";
import FocalCountries from "@/charterafrica/components/FocalCountries";
import Grantees from "@/charterafrica/components/Grantees";
import GuidingPrinciples from "@/charterafrica/components/GuidingPrinciples";
Expand Down Expand Up @@ -46,13 +46,13 @@ const componentsBySlugs = {
dataset: Dataset,
documents: Documents,
ecosystem: Ecosystem,
"embedded-chart": EmbeddedChart,
"embedded-playlist": Consultations,
document: Document,
explainers: Explainers,
faq: FAQ,
"featured-post": FeaturedPostCard,
"fellowships-and-grants-header": OpportunityHeader,
"flourish-chart": FlourishChart,
"focal-countries": FocalCountries,
"guiding-principles": GuidingPrinciples,
helpdesk: Helpdesk,
Expand Down
112 changes: 0 additions & 112 deletions apps/charterafrica/src/payload/blocks/EmbeddedChart.js

This file was deleted.

62 changes: 62 additions & 0 deletions apps/charterafrica/src/payload/blocks/FlourishChart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { validateHexColor } from "../utils/colors";

const FlourishChart = {
slug: "flourish-chart",
labels: {
singular: {
en: "Flourish Chart",
},
plural: {
en: "Flourish Chart",
},
},
fields: [
{
name: "title",
label: {
en: "Title",
fr: "Titre",
pt: "Título",
},
type: "text",
localized: true,
},
{
name: "subtitle",
label: {
en: "Subtitle",
fr: "Sous-titre",
pt: "Subtítulo",
},
type: "text",
localized: true,
},
{
name: "chartId",
label: {
en: "Chart ID",
},
type: "text",
required: true,
localized: true,
admin: {
description:
"This should be in type/number format e.g. visualisation/14834715 or story/1946372",
},
},
{
name: "backgroundColor",
label: {
en: "Background color",
fr: "Couleur de fond",
pt: "Cor de fundo",
},
type: "text",
required: true,
defaultValue: "#ffffff",
validate: validateHexColor,
},
],
};

export default FlourishChart;
Loading

0 comments on commit 95fd929

Please sign in to comment.