Skip to content

Commit

Permalink
feat(indexfield): add pageId to onRender cb
Browse files Browse the repository at this point in the history
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
  • Loading branch information
schettn authored Oct 10, 2021
1 parent 2a548a2 commit 4204c62
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
2 changes: 1 addition & 1 deletion examples/my-gatsby-site/src/templates/SamplePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const SamplePage: JaenTemplate = () => {
<Box maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden">
<h1>IndexField</h1>
<fields.IndexField
onRender={page => <div> {Object.keys(page.children)}</div>}
onRender={(page, pageId) => <div> {Object.keys(page.children)}</div>}
/>
</Box>
<Box maxW="sm" borderWidth="1px" borderRadius="lg" overflow="hidden">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IndexFieldProps> = 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)
1 change: 1 addition & 0 deletions packages/jaen-pages/src/contexts/cms.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export const CMSProvider: React.FC<CMSProviderType> = ({
const id = jaenPageContext?.id || node.id

site.allSitePage.nodes[id] = {
id,
parent: jaenPageContext?.parent || null,
children: jaenPageContext?.children || [],
path: node.path,
Expand Down
1 change: 1 addition & 0 deletions packages/jaen-pages/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export type PageMetadata = {
}

type BasePageType = {
id: string
slug: string
pageMetadata?: PageMetadata
images: {
Expand Down

0 comments on commit 4204c62

Please sign in to comment.