diff --git a/apps/climatemappedafrica/src/components/Hero/Hero.js b/apps/climatemappedafrica/src/components/Hero/Hero.js index abfd54da8..9af0e3106 100644 --- a/apps/climatemappedafrica/src/components/Hero/Hero.js +++ b/apps/climatemappedafrica/src/components/Hero/Hero.js @@ -4,6 +4,8 @@ import dynamic from "next/dynamic"; import PropTypes from "prop-types"; import React, { useState } from "react"; +import Legend from "./Legend"; + import heroBg from "@/climatemappedafrica/assets/images/bg-map-white.jpg"; import DropdownSearch from "@/climatemappedafrica/components/DropdownSearch"; import Image from "@/climatemappedafrica/components/Image"; @@ -23,6 +25,8 @@ function Hero({ properties, level, explorePageSlug, + averageTemperature, + legend, ...props }) { const isUpLg = useMediaQuery((theme) => theme.breakpoints.up("lg")); @@ -99,45 +103,44 @@ function Hero({ {/* Since map is dynamic-ally loaded, no need for implementation="css" */} - - - {center ? ( - - ) : null} - - - {hoverGeo} - - - - + + + {center ? ( + + ) : null} + + + + {hoverGeo} + + + @@ -154,6 +157,7 @@ Hero.propTypes = { properties: PropTypes.shape({}), level: PropTypes.string, explorePageSlug: PropTypes.string, + averageTemperature: PropTypes.string, }; export default Hero; diff --git a/apps/climatemappedafrica/src/components/Hero/Hero.snap.js b/apps/climatemappedafrica/src/components/Hero/Hero.snap.js index 6b4302a5f..b21c29c23 100644 --- a/apps/climatemappedafrica/src/components/Hero/Hero.snap.js +++ b/apps/climatemappedafrica/src/components/Hero/Hero.snap.js @@ -90,14 +90,54 @@ exports[` renders unchanged 1`] = `
+ class="MuiBox-root css-0" + > +
+
+
+
+
+
+
+
+
+
diff --git a/apps/climatemappedafrica/src/components/Hero/Hero.test.js b/apps/climatemappedafrica/src/components/Hero/Hero.test.js index ec342ec4f..2f50687e7 100644 --- a/apps/climatemappedafrica/src/components/Hero/Hero.test.js +++ b/apps/climatemappedafrica/src/components/Hero/Hero.test.js @@ -48,6 +48,15 @@ const defaultProps = { }, variant: "explore", icon: null, + legend: [ + { min: 10, max: 13, color: "#021AFE" }, + { min: 13, max: 16, color: "#5455FF" }, + { min: 16, max: 19, color: "#928EFD" }, + { min: 19, max: 22, color: "#B494DF" }, + { min: 22, max: 25, color: "#FA9B9B" }, + { min: 25, max: 28, color: "#F96264" }, + { min: 28, max: 31, color: "#F80701" }, + ], }; describe("", () => { diff --git a/apps/climatemappedafrica/src/components/Hero/Legend.js b/apps/climatemappedafrica/src/components/Hero/Legend.js new file mode 100644 index 000000000..f1a3f8139 --- /dev/null +++ b/apps/climatemappedafrica/src/components/Hero/Legend.js @@ -0,0 +1,65 @@ +import { RichTypography } from "@commons-ui/legacy"; +import { Box, Tooltip } from "@mui/material"; +import PropTypes from "prop-types"; +import React, { useState, forwardRef } from "react"; + +const Legend = forwardRef(function Legend({ data, title }, ref) { + const [hoveredValue, setHoveredValue] = useState(null); + + const handleMouseEnter = (value) => { + setHoveredValue(value); + }; + + const handleMouseLeave = () => { + setHoveredValue(null); + }; + + return ( + + + {title} + + + {data.map((item) => ( + + handleMouseEnter(`${item.min} - ${item.max}`)} + onMouseLeave={handleMouseLeave} + sx={{ + backgroundColor: item.color, + height: 24, + minWidth: 24, + cursor: "pointer", + }} + /> + + ))} + {hoveredValue && ( + + {hoveredValue} + + )} + + + ); +}); + +Legend.propTypes = { + data: PropTypes.arrayOf( + PropTypes.shape({ + min: PropTypes.number.isRequired, + max: PropTypes.number.isRequired, + color: PropTypes.string.isRequired, + }), + ).isRequired, +}; + +export default Legend; diff --git a/apps/climatemappedafrica/src/components/Hero/Map.js b/apps/climatemappedafrica/src/components/Hero/Map.js index 5a828363f..eab88d671 100644 --- a/apps/climatemappedafrica/src/components/Hero/Map.js +++ b/apps/climatemappedafrica/src/components/Hero/Map.js @@ -23,19 +23,23 @@ function Map({ onLayerMouseOver, featuredLocations, explorePageSlug, + choropleth, }) { const router = useRouter(); - const countyCodes = featuredLocations?.map(({ code }) => code); - + const regionCodes = featuredLocations?.map(({ code }) => code); const theme = useTheme(); const onEachFeature = (feature, layer) => { + const choroplethColor = choropleth?.find?.( + (c) => c.code.toLowerCase() === feature.properties.code.toLowerCase(), + ); layer.setStyle({ fillColor: theme.palette.background.default, + ...choroplethColor, fillOpacity: 1, }); - if (countyCodes.includes(feature.properties.code?.toLowerCase())) { + if (regionCodes.includes(feature.properties.code?.toLowerCase())) { layer.setStyle({ weight: 1.5, dashArray: 0, @@ -51,10 +55,7 @@ function Map({ }); layer.on("mouseout", () => { onLayerMouseOver(null); - layer.setStyle({ - fillOpacity: 1, - fillColor: theme.palette.background.default, - }); + layer.setStyle({ ...choroplethColor, fillOpacity: 1 }); }); layer.on("click", () => { if (explorePageSlug) { @@ -67,36 +68,38 @@ function Map({ }; return ( - - + - - + + + + ); } diff --git a/apps/climatemappedafrica/src/lib/data/blockify/hero.js b/apps/climatemappedafrica/src/lib/data/blockify/hero.js index 0f4cf6684..f225159d4 100644 --- a/apps/climatemappedafrica/src/lib/data/blockify/hero.js +++ b/apps/climatemappedafrica/src/lib/data/blockify/hero.js @@ -1,3 +1,5 @@ +import { generateChoropleth } from "@hurumap/next"; + import { fetchProfile, fetchProfileGeography, @@ -12,6 +14,7 @@ export default async function hero(block, _api, _context, { hurumap }) { const { profilePage, rootGeography: { center, code, hasData: pinRootGeography }, + profile: hurumapProfile, } = hurumap ?? { rootGeography: {} }; const { geometries } = await fetchProfileGeography(code.toLowerCase()); const { level } = geometries.boundary?.properties ?? {}; @@ -21,6 +24,12 @@ export default async function hero(block, _api, _context, { hurumap }) { }; const childLevel = childLevelMaps[level]; const { locations, preferredChildren } = await fetchProfile(); + const chloropleth = hurumapProfile?.choropleth ?? null; + const { choropleth, legend } = generateChoropleth( + chloropleth, + locations, + "choropleth", + ); const preferredChildrenPerLevel = preferredChildren[level]; const { children } = geometries; const preferredLevel = @@ -40,5 +49,7 @@ export default async function hero(block, _api, _context, { hurumap }) { pinRootGeography, properties: geometries.boundary?.properties, slug: "hero", + choropleth, + legend, }; } diff --git a/apps/climatemappedafrica/src/payload/blocks/Hero.js b/apps/climatemappedafrica/src/payload/blocks/Hero.js index 674343824..6c2b6c23c 100644 --- a/apps/climatemappedafrica/src/payload/blocks/Hero.js +++ b/apps/climatemappedafrica/src/payload/blocks/Hero.js @@ -52,6 +52,13 @@ const Hero = { label: "Comment", localized: true, }, + { + name: "averageTemperature", + type: "text", + label: "Average Temperature", + defaultValue: "Average Temperature", + localized: true, + }, ], }; diff --git a/packages/hurumap-next/src/index.js b/packages/hurumap-next/src/index.js index 52d900496..9a9fc3d0e 100644 --- a/packages/hurumap-next/src/index.js +++ b/packages/hurumap-next/src/index.js @@ -1,2 +1,3 @@ export { default as Source } from "./Source"; export { default as Map } from "./Map"; +export { generateChoropleth } from "./Map/utils";