From 4204c625a64af378081dd6b0c95a890d88339c9c Mon Sep 17 00:00:00 2001 From: Nico Schett <52858351+schettn@users.noreply.github.com> Date: Sun, 10 Oct 2021 08:39:10 +0000 Subject: [PATCH] feat(indexfield): add pageId to onRender cb This change makes it possible to get the id of the page it is implemented. This is useful when no fixedSlug is used and the pageId is autogenerated, thus not predictable. re #37 --- examples/my-gatsby-site/src/templates/SamplePage.tsx | 2 +- .../src/containers/fields/IndexField/index.tsx | 9 +++++---- packages/jaen-pages/src/contexts/cms.tsx | 1 + packages/jaen-pages/src/types.ts | 1 + 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/my-gatsby-site/src/templates/SamplePage.tsx b/examples/my-gatsby-site/src/templates/SamplePage.tsx index 283da68f..918f7ad4 100644 --- a/examples/my-gatsby-site/src/templates/SamplePage.tsx +++ b/examples/my-gatsby-site/src/templates/SamplePage.tsx @@ -25,7 +25,7 @@ const SamplePage: JaenTemplate = () => {

IndexField

{Object.keys(page.children)}
} + onRender={(page, pageId) =>
{Object.keys(page.children)}
} />
diff --git a/packages/jaen-pages/src/containers/fields/IndexField/index.tsx b/packages/jaen-pages/src/containers/fields/IndexField/index.tsx index 2f2b55fc..b149ea6b 100644 --- a/packages/jaen-pages/src/containers/fields/IndexField/index.tsx +++ b/packages/jaen-pages/src/containers/fields/IndexField/index.tsx @@ -5,18 +5,19 @@ import {withRedux} from '@store/withRedux' interface IndexFieldProps { fixedSlug?: string - onRender: (page: ResolvedPageType) => JSX.Element + onRender: (page: ResolvedPageType, pageId: string) => JSX.Element } const IndexField: React.FC = props => { const {jaenPageContext} = useTemplate() - const page = useResolvedPage(props.fixedSlug || jaenPageContext.id) + const pageId = props.fixedSlug || jaenPageContext.id + const page = useResolvedPage(pageId) if (!page) { - throw new Error(`Page not found: ${props.fixedSlug || jaenPageContext.id}`) + throw new Error(`Page not found!: ${pageId}`) } - return props.onRender(page) + return props.onRender(page, pageId) } export default withRedux(IndexField) diff --git a/packages/jaen-pages/src/contexts/cms.tsx b/packages/jaen-pages/src/contexts/cms.tsx index 7cd8a0c0..b2f3cbee 100644 --- a/packages/jaen-pages/src/contexts/cms.tsx +++ b/packages/jaen-pages/src/contexts/cms.tsx @@ -234,6 +234,7 @@ export const CMSProvider: React.FC = ({ const id = jaenPageContext?.id || node.id site.allSitePage.nodes[id] = { + id, parent: jaenPageContext?.parent || null, children: jaenPageContext?.children || [], path: node.path, diff --git a/packages/jaen-pages/src/types.ts b/packages/jaen-pages/src/types.ts index bd3ae671..2f8600fc 100644 --- a/packages/jaen-pages/src/types.ts +++ b/packages/jaen-pages/src/types.ts @@ -98,6 +98,7 @@ export type PageMetadata = { } type BasePageType = { + id: string slug: string pageMetadata?: PageMetadata images: {