Skip to content

Commit

Permalink
Improve explore page
Browse files Browse the repository at this point in the history
Signed-off-by: Kipruto <[email protected]>
  • Loading branch information
kelvinkipruto committed Oct 24, 2024
1 parent 5dbdfb9 commit 67c8b22
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
18 changes: 13 additions & 5 deletions apps/climatemappedafrica/src/components/ExplorePage/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import useStyles from "./useStyles";

import Panel from "@/climatemappedafrica/components/HURUmap/Panel";

function initialState(profiles, onClick, explorePagePath, initialLocation) {
function initialState(profiles, onClick, explorePagePath, initialLocationCode) {
return {
profiles: Array.isArray(profiles) ? profiles : [profiles],
options: [
{ color: "primary", onClick },
{ color: "secondary", onClick },
],
explorePagePath,
initialLocation,
initialLocationCode,
};
}

function ExplorePage({
center,
initialLocation,
explorePagePath,
panel: PanelProps = {},
profile: profileProp,
...props
}) {
const { center, name: initialLocationCode } = initialLocation;
const theme = useTheme();
const classes = useStyles(props);
// NOTE: This setState and the corresponding useEffect are "hacks" since at
Expand All @@ -41,7 +41,12 @@ function ExplorePage({
setGeoCode(code);
};
const [state, dispatch] = useExplore(
initialState(profileProp, handleClickTag, explorePagePath, initialLocation),
initialState(
profileProp,
handleClickTag,
explorePagePath,
initialLocationCode,
),
);
useEffect(() => {
dispatch({
Expand Down Expand Up @@ -176,7 +181,10 @@ function ExplorePage({

ExplorePage.propTypes = {
center: PropTypes.arrayOf(PropTypes.number),
initialLocation: PropTypes.string,
initialLocation: PropTypes.shape({
center: PropTypes.arrayOf(PropTypes.number),
name: PropTypes.string,
}),
explorePagePath: PropTypes.string,
panel: PropTypes.shape({}),
profile: PropTypes.oneOfType([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ function extendProfileTags(profile, options, explorePagePath) {
return { ...other, tags };
}

function initializer({ explorePagePath, initialLocation, profiles, options }) {
function initializer({
explorePagePath,
initialLocationCode,
profiles,
options,
}) {
const [primary, secondary] = profiles;
const [primaryOptions, secondaryOptions] = options;

Expand All @@ -30,12 +35,12 @@ function initializer({ explorePagePath, initialLocation, profiles, options }) {
primary: extendProfileTags(primary, primaryOptions, explorePagePath),
secondary: extendProfileTags(secondary, secondaryOptions, explorePagePath),
explorePagePath,
initialLocation,
initialLocationCode,
};
}

function reducer(state, action) {
const { explorePagePath, initialLocation } = state;
const { explorePagePath, initialLocationCode } = state;
switch (action.type) {
case "fetch": {
const code = action.payload?.code;
Expand Down Expand Up @@ -85,7 +90,7 @@ function reducer(state, action) {
return state;
}
case "pin":
if (state.primary.geography.code.toLowerCase() !== initialLocation) {
if (state.primary.geography.code.toLowerCase() !== initialLocationCode) {
return { ...state, isPinning: true };
}
return { ...state, isPinning: false };
Expand Down
17 changes: 9 additions & 8 deletions apps/climatemappedafrica/src/lib/data/blockify/explore-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ const fetchProfileGeography = async (geoCode) => {
return data;
};

async function explorePage({
slug: code,
center,
initialLocation,
explorePagePath,
}) {
async function explorePage({ slugs }, _, __, hurumap) {
const {
initialLocation,
page: { value },
} = hurumap;
const { name } = initialLocation;
const code = slugs.length ? slugs[0] : name;
const hurumapProfile = await fetchProfile();

const { locations, preferredChildren, mapType, choropleth } = hurumapProfile;
Expand Down Expand Up @@ -86,10 +87,9 @@ async function explorePage({
const res = {
id: "explore-page",
blockType: "explore-page",
center,
choropleth,
initialLocation,
explorePagePath,
explorePagePath: value.slug,
locations,
mapType,
panel,
Expand All @@ -99,6 +99,7 @@ async function explorePage({
};

return res;
// return {}
}

export default explorePage;
33 changes: 11 additions & 22 deletions apps/climatemappedafrica/src/lib/data/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,27 +54,6 @@ function getNavBar(siteSettings, variant, { slug }) {
};
}

async function processExplorePage(slugs, hurumap, explorePage) {
const {
initialLocation: { name, center },
} = hurumap;
const slug = slugs.length ? slugs[0] : name;
const blocks = await blockify([
{
blockType: "explore-page",
center,
initialLocation: name,
slug: slug.trim().toLowerCase(),
explorePagePath: explorePage.slug,
},
{
blockType: "tutorial",
},
]);

return blocks;
}

export async function getPageProps(api, context) {
// For now, ClimatemappedAfrica only supports single paths i.e. /, /about, etc.,
// so params.slugs[0] is good enough
Expand Down Expand Up @@ -104,7 +83,17 @@ export async function getPageProps(api, context) {
const menus = getNavBar(siteSettings, variant, explorePage);

if (slug === explorePage.slug) {
blocks = await processExplorePage(slugs.slice(1), hurumap, explorePage);
// The explore page is a special case. The only block we need to render is map and tutorial.
const explorePageBlocks = [
{
blockType: "explore-page",
slugs: slugs.slice(1),
},
{
blockType: "tutorial",
},
];
blocks = await blockify(explorePageBlocks, api, context, hurumap);
}

return {
Expand Down
9 changes: 9 additions & 0 deletions apps/climatemappedafrica/src/payload/globals/HURUMap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ const HURUMap = {
type: "point",
defaultValue: [20.0, 4.25],
},
{
name: "pinInitialLocation",
type: "checkbox",
localized: true,
label: {
en: "Allow pinning of initial location",
},
defaultValue: false,
},
],
},
],
Expand Down

0 comments on commit 67c8b22

Please sign in to comment.