add support for generic pages

This commit is contained in:
2024-05-10 19:39:59 +02:00
parent 511715b75b
commit 8a66da7f6c
9 changed files with 305 additions and 9 deletions

View File

@ -0,0 +1,83 @@
import { graphql } from "@/gql";
import { GenericFragment } from "@/gql/graphql";
import { getClient } from "@/app/client";
import { Blocks } from "@/components/blocks/Blocks";
import { notFound } from 'next/navigation'
export const dynamicParams = false
const GenericFragmentDefinition = graphql(`
fragment Generic on GenericPage {
__typename
id
title
body {
id
blockType
field
... on RichTextBlock {
rawValue
value
}
}
}
`);
export async function generateStaticParams() {
const allGenericSlugsQuery = graphql(`
query allGenericSlugs {
pages(contentType: "generic.GenericPage") {
id
urlPath
}
}
`);
const { data } = await getClient().query(allGenericSlugsQuery, {});
return data?.pages.map((page: any) => {
// wagtail-grapple prepends the home page slug to the full path on multisite setups
// we also strip the trailing slash
const urlPath = page.urlPath.replace(/\/home/, "").replace(/\/$/, "");
console.log("adding", urlPath);
return {
url: [urlPath],
};
});
}
export default async function Page({ params }: { params: { url: string } }) {
const { url } = params;
// for the page /foo/bar we need to look for `/home/foo/bar/`
const urlPath = `/home/${url}/`;
console.log("real urlPath for", url, "is", urlPath);
const genericPageByUrlPathQuery = graphql(`
query genericPageByUrl($urlPath: String!) {
page: page(contentType: "generic.GenericPage", urlPath: $urlPath) {
... on GenericPage {
...Generic
}
}
}
`);
const { data, error } = await getClient().query(genericPageByUrlPathQuery, {
urlPath: urlPath,
});
const page = (data?.page ?? []) as GenericFragment[];
if (!page) {
return notFound()
}
return (
<main className="site-main" id="main">
<section className="page-header">
<h1>{page.title}</h1>
</section>
<Blocks blocks={page.body} />
</main>
);
}