From 1b6026304fbce84f1acbada04eb8829b1b32067b Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Fri, 5 Apr 2024 12:18:40 -0400 Subject: [PATCH] fix(next): adjusts args sent through live preview url --- packages/next/src/views/LivePreview/index.tsx | 28 ++++++++++++++++--- packages/payload/src/config/types.ts | 3 +- .../utilities/formatLivePreviewURL.ts | 9 ++++-- tsconfig.json | 2 +- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/packages/next/src/views/LivePreview/index.tsx b/packages/next/src/views/LivePreview/index.tsx index ba4689253..cf165c652 100644 --- a/packages/next/src/views/LivePreview/index.tsx +++ b/packages/next/src/views/LivePreview/index.tsx @@ -1,5 +1,5 @@ import type { LivePreviewConfig } from 'payload/config' -import type { EditViewComponent } from 'payload/types' +import type { EditViewComponent, TypeWithID } from 'payload/types' import React from 'react' @@ -11,6 +11,7 @@ export const LivePreviewView: EditViewComponent = async (props) => { const { collectionConfig, + docID, globalConfig, locale, req: { @@ -22,8 +23,26 @@ export const LivePreviewView: EditViewComponent = async (props) => { } = {}, } = initPageResult - // TODO(JAKE): not sure what `data` is or what it should be - const data = 'data' in props ? props.data : {} + let data: TypeWithID + + if (collectionConfig) { + data = await initPageResult.req.payload.findByID({ + id: docID, + collection: collectionConfig.slug, + depth: 0, + draft: true, + fallbackLocale: null, + }) + } + + if (globalConfig) { + data = await initPageResult.req.payload.findGlobal({ + slug: globalConfig.slug, + depth: 0, + draft: true, + fallbackLocale: null, + }) + } let livePreviewConfig: LivePreviewConfig = topLevelLivePreviewConfig @@ -54,8 +73,9 @@ export const LivePreviewView: EditViewComponent = async (props) => { const url = typeof livePreviewConfig?.url === 'function' ? await livePreviewConfig.url({ + collectionConfig, data, - documentInfo: {}, // TODO: recreate this object server-side, see `useDocumentInfo` + globalConfig, locale, }) : livePreviewConfig?.url diff --git a/packages/payload/src/config/types.ts b/packages/payload/src/config/types.ts index 2acaca22c..bae863336 100644 --- a/packages/payload/src/config/types.ts +++ b/packages/payload/src/config/types.ts @@ -63,8 +63,9 @@ export type LivePreviewConfig = { */ url?: | ((args: { + collectionConfig?: SanitizedCollectionConfig data: Record - documentInfo: any // TODO: remove or populate this + globalConfig?: SanitizedGlobalConfig locale: Locale }) => Promise | string) | string diff --git a/test/live-preview/utilities/formatLivePreviewURL.ts b/test/live-preview/utilities/formatLivePreviewURL.ts index 68bc956a7..2814653ae 100644 --- a/test/live-preview/utilities/formatLivePreviewURL.ts +++ b/test/live-preview/utilities/formatLivePreviewURL.ts @@ -1,4 +1,9 @@ -export const formatLivePreviewURL = async ({ data, documentInfo }) => { +import type { LivePreviewConfig } from 'payload/config' + +export const formatLivePreviewURL: LivePreviewConfig['url'] = async ({ + data, + collectionConfig, +}) => { let baseURL = 'http://localhost:3000/live-preview' // You can run async requests here, if needed @@ -21,6 +26,6 @@ export const formatLivePreviewURL = async ({ data, documentInfo }) => { // I.e. append '/posts' to the URL if the document is a post // You can also do this on individual collection or global config, if preferred return `${baseURL}${ - documentInfo?.slug && documentInfo.slug !== 'pages' ? `/${documentInfo.slug}` : '' + collectionConfig && collectionConfig.slug !== 'pages' ? `/${collectionConfig.slug}` : '' }${data?.slug && data.slug !== 'home' ? `/${data.slug}` : ''}` } diff --git a/tsconfig.json b/tsconfig.json index 217f2bd79..39750511a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -37,7 +37,7 @@ ], "paths": { "@payload-config": [ - "./test/_community/config.ts" + "./test/live-preview/config.ts" ], "@payloadcms/live-preview": [ "./packages/live-preview/src"