Skip to content

Commit

Permalink
fix(gatsby-theme-docz): add null fields for entries
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Mar 18, 2019
1 parent 9e3314a commit b0409a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 31 deletions.
25 changes: 13 additions & 12 deletions core/gatsby-theme-docz/src/node/sourceNodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ module.exports = async ({ actions, createNodeId }, opts) => {
})
}

const createEntriesNode = async () => {
const map = await entries.get()
const contentDigest = digest(JSON.stringify(map))
const values = Object.entries(map)

values.forEach(([key, entry]) => {
const createEntriesNodes = payload => {
const values = Array.isArray(payload) ? payload : []
values.forEach(({ value: entry }) => {
const contentDigest = digest(JSON.stringify(entry))
createNode({
...entry,
children: [],
link: entry.link || false,
menu: entry.menu || false,
internal: {
contentDigest,
type: 'DoczEntries',
type: `DoczEntries`,
},
})
})
}

const createNodes = async () => {
await createDbNode()
await createEntriesNode()
const createStateNodes = async ({ type, payload }) => {
if (type === 'state.entries') createEntriesNodes(payload)
}

await createNodes()
dataServer.onStateChange(async () => createNodes())
dataServer.onStateChange(async state => {
await createDbNode()
await createStateNodes(state)
})
}
32 changes: 13 additions & 19 deletions core/gatsby-theme-docz/templates/Layout.tpl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { Fragment } from 'react'
import PropTypes from 'prop-types'
import { StaticQuery, graphql } from 'gatsby'
import { useStaticQuery, graphql } from 'gatsby'
import { AsyncRoute, useComponents } from 'docz'
import { MDXProvider } from '@mdx-js/tag'

Expand Down Expand Up @@ -44,25 +44,19 @@ const parseDatabase = data => {

const Layout = ({ children, ...defaultProps }) => {
const { pageContext: ctx } = defaultProps
return (
<StaticQuery
query={query}
render={data => {
const db = parseDatabase(data)
const entry = db.entries && db.entries.find(entry => entry.filepath === ctx.filepath)
const data = useStaticQuery(query)
const db = parseDatabase(data)
const entry = db.entries && db.entries.find(entry => entry.filepath === ctx.filepath)

return (
<Fragment>
{entry && <SEO title={entry.value.name} />}
<Theme db={db} linkComponent={Link} <% if (wrapper) {%>wrapper={Wrapper}<%}%>>
<Route {...defaultProps} entry={entry}>
{children}
</Route>
</Theme>
</Fragment>
)
}}
/>
return (
<Fragment>
{entry && <SEO title={entry.value.name} />}
<Theme db={db} linkComponent={Link} <% if (wrapper) {%>wrapper={Wrapper}<%}%>>
<Route {...defaultProps} entry={entry}>
{children}
</Route>
</Theme>
</Fragment>
)
}

Expand Down

0 comments on commit b0409a8

Please sign in to comment.