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

feat: restructure preview config #360

Merged
merged 9 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
49 changes: 30 additions & 19 deletions packages/gatsby-plugin-prismic-previews/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,12 @@ const PreviewPage = () => {
)
}

export default withPrismicPreviewResolver(PreviewPage, {
[process.env.GATSBY_PRISMIC_REPOSITORY_NAME]: { linkResolver },
})
export default withPrismicPreviewResolver(PreviewPage, [
{
repositoryName: process.env.GATSBY_PRISMIC_REPOSITORY_NAME,
linkResolver,
},
])
```

You can see that the Link Resolver provided to `withPrismicPreviewResolver()` is
Expand All @@ -216,14 +219,16 @@ from two repositories, each with their own Link Resolver, your
import { mainLinkResolver } from '../mainLinkResolver'
import { secondaryLinkResolver } from '../secondaryLinkResolver'

export default withPrismicPreviewResolver(PreviewPage, {
[process.env.GATSBY_PRISMIC_MAIN_REPOSITORY_NAME]: {
export default withPrismicPreviewResolver(PreviewPage, [
{
repositoryName: process.env.GATSBY_PRISMIC_MAIN_REPOSITORY_NAME,
linkResolver: mainLinkResolver,
},
[process.env.GATSBY_PRISMIC_SECONDARY_REPOSITORY_NAME]: {
{
repositoryName: process.env.GATSBY_PRISMIC_SECONDARY_REPOSITORY_NAME,
linkResolver: secondaryLinkResolver,
},
})
])
```

For more details on setting up a preview resolver page and the available
Expand Down Expand Up @@ -265,9 +270,12 @@ const PageTemplate = ({ data }) => {
)
}

export default withPrismicPreview(PageTemplate, {
[process.env.GATSBY_PRISMIC_REPOSITORY_NAME]: { linkResolver },
})
export default withPrismicPreview(PageTemplate, [
{
repositoryName: process.env.GATSBY_PRISMIC_REPOSITORY_NAME,
linkResolver,
},
])

export const query = graphql`
query PageTemplate($id: ID!) {
Expand Down Expand Up @@ -303,14 +311,16 @@ from two repositories, each with their own Link Resolver, your
import { mainLinkResolver } from '../mainLinkResolver'
import { secondaryLinkResolver } from '../secondaryLinkResolver'

export default withPrismicPreview(PageTemplate, {
[process.env.GATSBY_PRISMIC_MAIN_REPOSITORY_NAME]: {
export default withPrismicPreview(PageTemplate, [
{
repositoryName: process.env.GATSBY_PRISMIC_MAIN_REPOSITORY_NAME,
linkResolver: mainLinkResolver,
},
[process.env.GATSBY_PRISMIC_SECONDARY_REPOSITORY_NAME]: {
{
repositoryName: process.env.GATSBY_PRISMIC_SECONDARY_REPOSITORY_NAME,
linkResolver: secondaryLinkResolver,
},
})
])
```

For more details on connecting your pages and templates to preview data and the
Expand All @@ -337,9 +347,10 @@ import {
componentResolverFromMap,
} from 'gatsby-plugin-prismic-previews'

import { PageTemplate } from './PageTemplate'
import { linkResolver } from '../linkResolver'

import PageTemplate from './PageTemplate'

const NotFoundPage = ({ data }) => {
const page = data.prismicPage

Expand All @@ -350,15 +361,15 @@ const NotFoundPage = ({ data }) => {
)
}

export default withPrismicUnpublishedPreview(
PageTemplate,
{ 'my-repository-name': { linkResolver } },
export default withPrismicUnpublishedPreview(PageTemplate, [
{
repositoryName: 'my-repository-name',
linkResolver,
componentResolver: componentResolverFromMap({
page: PageTemplate,
}),
},
)
])

export const query = graphql`
query NotFoundPage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ must mark documents in your query as "previewable." This involves adding a
```typescript
function withPrismicPreview(
WrappedComponent: React.ComponentType,
repositoryConfigs: Record<
string,
{
linkResolver: LinkResolver
htmlSerializer?: HTMLSerializer
}
>,
repositoryConfigs: {
repositoryName: string
linkResolver: LinkResolver
htmlSerializer?: HTMLSerializer
transformFieldName?: FieldNameTransformer
}[],
config: {
mergePreviewData?: boolean
},
Expand All @@ -50,10 +49,11 @@ in your app:
for the configured Prismic repository. This should be the same HTML Serializer
provided to [`gatsby-source-prismic`][gsp] in your app's `gatsby-config.js`.

- **`transformFieldName`**<br/>The optional field transformer for the configured
Prismic repository. This should be the same `transformFieldName` function
provided to [`gatsby-source-prismic`][gsp] in your app's `gatsby-config.js` if
used. Most projects will not need to provide a value for this option.
- **`transformFieldName`**<br/>The optional field name transformer for the
configured Prismic repository. This should be the same `transformFieldName`
function provided to [`gatsby-source-prismic`][gsp] in your app's
`gatsby-config.js` if used. Most projects will not need to provide a value for
this option.

Configuration values:

Expand Down Expand Up @@ -96,9 +96,12 @@ const PageTemplate = ({ data }) => {
)
}

export default withPrismicPreview(PageTemplate, {
'my-repository-name': { linkResolver },
})
export default withPrismicPreview(PageTemplate, [
{
repositoryName: 'my-repository-name',
linkResolver,
},
])

export const query = graphql`
query PageTemplate($id: ID!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ updates.
```typescript
function withPrismicPreviewResolver(
WrappedComponent: React.ComponentType,
repositoryConfigs: Record<
string,
{
linkResolver: LinkResolver
}
>,
repositoryConfigs: {
repositoryName: string
linkResolver: LinkResolver
}[],
config: {
autoRedirect?: boolean
},
Expand Down Expand Up @@ -85,9 +83,12 @@ const PreviewPage = () => {
)
}

export default withPrismicPreviewResolver(PreviewPage, {
'my-repository-name': { linkResolver },
})
export default withPrismicPreviewResolver(PreviewPage, [
{
repositoryName: 'my-repository-name',
linkResolver,
},
])
```

[hoc]: https://reactjs.org/docs/higher-order-components.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,19 @@ appears toward the top of the page. This will hide Gatsby's default development
```typescript
function withPrismicUnpublishedPreview(
WrappedComponent: React.ComponentType,
repositoryConfigs: Record<
string,
{
linkResolver: LinkResolver
htmlSerializer?: HTMLSerializer
}
>,
config: {
repositoryConfigs: {
repositoryName: string
linkResolver: LinkResolver
htmlSerializer?: HTMLSerializer
transformFieldName?: FieldNameTransformer
componentResolver: (
nodes: PrismicNode[],
) => React.ComponentType | undefined | null
dataResolver?: (
nodes: PrismicNode[],
data: Record<string, unknown>,
) => Record<string, unknown>
},
}[],
): React.ComponentType
```

Expand All @@ -46,9 +43,6 @@ function withPrismicUnpublishedPreview(
- **`repositoryConfigs`**<br/>A set of configuration values for each Prismic
repository used in your app.

- **`config`**<br/>A set of configuration values that determine how the
unpublished preview data is prepared and provided.

The following configuration should be provided for each Prismic repository used
in your app:

Expand All @@ -60,7 +54,11 @@ in your app:
for the configured Prismic repository. This should be the same HTML Serializer
provided to [`gatsby-source-prismic`][gsp] in your app's `gatsby-config.js`.

Configuration values:
- **`transformFieldName`**<br/>The optional field name transformer for the
configured Prismic repository. This should be the same `transformFieldName`
function provided to [`gatsby-source-prismic`][gsp] in your app's
`gatsby-config.js` if used. Most projects will not need to provide a value for
this option.

- **`componentResolver`**<br/>A function that determines the component to render
during an unpublished preview. This function will be provided a list of nodes
Expand Down Expand Up @@ -122,9 +120,10 @@ import {
componentResolverFromMap,
} from 'gatsby-plugin-prismic-previews'

import { PageTemplate } from './PageTemplate'
import { linkResolver } from '../linkResolver'

import PageTemplate from './PageTemplate'

const NotFoundPage = ({ data }) => {
const page = data.prismicPage

Expand All @@ -135,15 +134,15 @@ const NotFoundPage = ({ data }) => {
)
}

export default withPrismicUnpublishedPreview(
PageTemplate,
{ 'my-repository-name': { linkResolver } },
export default withPrismicUnpublishedPreview(PageTemplate, [
{
repositoryName: 'my-repository-name',
linkResolver,
componentResolver: componentResolverFromMap({
page: PageTemplate,
}),
},
)
])

export const query = graphql`
query NotFoundPage {
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-prismic-previews/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-plugin-prismic-previews",
"version": "4.0.0-alpha.4",
"version": "4.0.0-alpha.5",
"description": "Gatsby plugin for integrating client-side Prismic previews support",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
proxyDocumentSubtree,
ProxyDocumentSubtreeEnv,
} from '../lib/proxyDocumentSubtree'
import { mapRecordIndices } from '../lib/mapRecordIndices'

export const valueRefinement = (value: unknown): value is UnknownRecord[] =>
Array.isArray(value) &&
Expand All @@ -18,15 +19,21 @@ export const proxyValue = (
fieldValue: UnknownRecord[],
): RE.ReaderEither<ProxyDocumentSubtreeEnv, Error, unknown> =>
pipe(
fieldValue,
A.map((fieldValueElement) =>
RE.ask<ProxyDocumentSubtreeEnv>(),
RE.chain((env) =>
pipe(
fieldValueElement,
R.mapWithIndex((fieldName, value) =>
proxyDocumentSubtree([...path, fieldName], value),
fieldValue,
A.map((fieldValueElement) =>
pipe(
fieldValueElement,
mapRecordIndices(env.transformFieldName),
R.mapWithIndex((fieldName, value) =>
proxyDocumentSubtree([...path, fieldName], value),
),
R.sequence(RE.Applicative),
),
),
R.sequence(RE.Applicative),
RE.sequenceArray,
),
),
RE.sequenceArray,
)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
proxyDocumentSubtree,
ProxyDocumentSubtreeEnv,
} from '../lib/proxyDocumentSubtree'
import { mapRecordIndices } from '../lib/mapRecordIndices'

export const valueRefinement = (
value: unknown,
Expand All @@ -20,21 +21,23 @@ export const proxyValue = (
): RE.ReaderEither<ProxyDocumentSubtreeEnv, Error, unknown> =>
pipe(
RE.ask<ProxyDocumentSubtreeEnv>(),
RE.bind('primary', () =>
RE.bind('primary', (env) =>
pipe(
fieldValue.primary ?? {},
mapRecordIndices(env.transformFieldName),
R.mapWithIndex((fieldName, value) =>
proxyDocumentSubtree([...path, 'primary', fieldName], value),
),
R.sequence(RE.Applicative),
),
),
RE.bind('items', () =>
RE.bind('items', (env) =>
pipe(
fieldValue.items ?? [],
A.map((item) =>
pipe(
item,
mapRecordIndices(env.transformFieldName),
R.mapWithIndex((fieldName, value) =>
proxyDocumentSubtree([...path, 'items', fieldName], value),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { pipe } from 'fp-ts/function'
import { NodeHelpers } from 'gatsby-node-helpers'

import {
FieldNameTransformer,
HTMLSerializer,
LinkResolver,
PluginOptions,
Expand Down Expand Up @@ -33,7 +34,7 @@ export interface ProxyDocumentSubtreeEnv {
imagePlaceholderImgixParams: PluginOptions['imagePlaceholderImgixParams']
nodeHelpers: NodeHelpers
createContentDigest(input: string | UnknownRecord): string
transformFieldName(fieldName: string): string
transformFieldName: FieldNameTransformer
}

export const proxyDocumentSubtree = (
Expand Down
Loading