Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove static props and fix layout #578

Merged
merged 10 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion apps/codeforafrica/src/lib/data/common/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,17 @@ function getFooter(settings) {
secondaryMenus: secondaryNavigation?.menus || null,
};
}
function getPageSlug({ params }) {
kelvinkipruto marked this conversation as resolved.
Show resolved Hide resolved
const slugsCount = params?.slugs?.length;
// count < 3, page slug is the last slug e.g. ["about"] or ["knowldge/news"]
// count == 3, page slug is the 2nd slug (index 1); last slug (index 3)
// is the post. e.g. opportunities/grants/democratic-governance-in-zambia
const pageSlugIndex = slugsCount < 3 ? slugsCount - 1 : 1;
return params?.slugs?.[pageSlugIndex] || "index";
}

export async function getPageProps(api, slug) {
export async function getPageProps(api, context) {
const slug = getPageSlug(context);
const {
docs: [page],
} = await api.findPage(slug);
Expand Down
22 changes: 2 additions & 20 deletions apps/codeforafrica/src/lib/data/local/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,14 @@
import { getPageStaticProps } from "@/codeforafrica/lib";
import { getPageProps } from "@/codeforafrica/lib/data/common";
kelvinkipruto marked this conversation as resolved.
Show resolved Hide resolved
import { api } from "@/codeforafrica/lib/data/rest";

function getPageSlug({ params }) {
const slugsCount = params?.slugs?.length;
// count < 3, page slug is the last slug e.g. ["about"] or ["knowldge/news"]
// count == 3, page slug is the 2nd slug (index 1); last slug (index 3)
// is the post. e.g. opportunities/grants/democratic-governance-in-zambia
const pageSlugIndex = slugsCount < 3 ? slugsCount - 1 : 1;
return params?.slugs?.[pageSlugIndex] || "index";
}

export async function getPageServerSideProps(context) {
const slug = getPageSlug(context);
const pathname =
slug !== "index" ? `/${context.params.slugs.join("/")}` : "/";
const props = await getPageProps(api, slug);
const props = await getPageProps(api, context);
if (!props) {
return { notFound: true };
}

// TODO Remove static props
const { props: staticProps } = await getPageStaticProps({ slug: pathname });
return {
props: {
...staticProps,
...props,
},
props,
};
}

Expand Down
19 changes: 8 additions & 11 deletions apps/codeforafrica/src/pages/[...slugs].page.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Hero from "@/codeforafrica/components/Hero";
import MeetOurTeam from "@/codeforafrica/components/MeetOurTeam";
import NewsAndStories from "@/codeforafrica/components/NewsAndStories";
import OurPartners from "@/codeforafrica/components/OurPartners";
import Page from "@/codeforafrica/components/Page";
import PageHeader from "@/codeforafrica/components/PageHeader";
import { getPageServerSideProps } from "@/codeforafrica/lib/data";

Expand All @@ -23,7 +22,7 @@ const componentsBySlugs = {
projects: FeaturedProjects,
};

function Index({ blocks, fallback, ...props }) {
function Index({ blocks, fallback }) {
if (!blocks?.length) {
return null;
}
Expand All @@ -36,15 +35,13 @@ function Index({ blocks, fallback, ...props }) {
}
return (
<PageComponent {...pageComponentProps}>
<Page {...props}>
{blocks.map((block) => {
const Component = componentsBySlugs[block.slug];
if (!Component) {
return null;
}
return <Component {...block} key={block.id} />;
})}
</Page>
{blocks.map((block) => {
const Component = componentsBySlugs[block.slug];
if (!Component) {
return null;
}
return <Component {...block} key={block.id} />;
})}
</PageComponent>
);
}
Expand Down
7 changes: 6 additions & 1 deletion apps/codeforafrica/src/pages/_app.page.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,21 @@ import { DefaultSeo } from "next-seo";
import PropTypes from "prop-types";
import React, { useEffect } from "react";

import Page from "@/codeforafrica/components/Page";
import SEO from "@/codeforafrica/next-seo.config";
import "@/codeforafrica/theme/fonts.css";
import theme from "@/codeforafrica/theme";
import createEmotionCache from "@/codeforafrica/utils/createEmotionCache";

const clientSideEmotionCache = createEmotionCache();

function getDefaultLayout(page, pageProps) {
return <Page {...pageProps}>{page}</Page>;
}
kelvinkipruto marked this conversation as resolved.
Show resolved Hide resolved
function MyApp(props) {
const router = useRouter();
const { Component, emotionCache = clientSideEmotionCache, pageProps } = props;
const getLayout = Component.getLayout || getDefaultLayout;
useEffect(() => {
if (process.env.NEXT_PUBLIC_GOOGLE_ANALYTICS_ID) {
const handleRouteChange = (url) => {
Expand Down Expand Up @@ -55,7 +60,7 @@ function MyApp(props) {
</Head>
<ThemeProvider theme={theme}>
<CssBaseline />
<Component {...pageProps} />
{getLayout(<Component {...pageProps} />, pageProps)}
</ThemeProvider>
</CacheProvider>
<Script
Expand Down