Skip to content

Commit

Permalink
fix(next): thread default ServerProps to view actions and other compo…
Browse files Browse the repository at this point in the history
…nents that were missing (#9868)

This PR threads default `serverProps` to Edit and List view action slots, as well as other various components that were missing them.

---------

Co-authored-by: Alessio Gravili <alessio@gravili.de>
  • Loading branch information
akhrarovsaid and AlessioGr authored Dec 11, 2024
1 parent a0f0316 commit 0303b78
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
42 changes: 21 additions & 21 deletions packages/next/src/templates/Default/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,20 @@ export const DefaultTemplate: React.FC<DefaultTemplateProps> = ({
} = {},
} = payload.config || {}

const serverProps = React.useMemo<ServerProps>(
() => ({
i18n,
locale,
params,
payload,
permissions,
searchParams,
user,
visibleEntities,
}),
[i18n, locale, params, payload, permissions, searchParams, user, visibleEntities],
)

const { Actions } = React.useMemo<{
Actions: Record<string, React.ReactNode>
}>(() => {
Expand All @@ -59,11 +73,13 @@ export const DefaultTemplate: React.FC<DefaultTemplateProps> = ({
acc[action.path] = RenderServerComponent({
Component: action,
importMap: payload.importMap,
serverProps,
})
} else {
acc[action] = RenderServerComponent({
Component: action,
importMap: payload.importMap,
serverProps,
})
}
}
Expand All @@ -72,23 +88,14 @@ export const DefaultTemplate: React.FC<DefaultTemplateProps> = ({
}, {})
: undefined,
}
}, [viewActions, payload])
}, [payload, serverProps, viewActions])

const NavComponent = RenderServerComponent({
clientProps: { clientProps: { visibleEntities } },
Component: CustomNav,
Fallback: DefaultNav,
importMap: payload.importMap,
serverProps: {
i18n,
locale,
params,
payload,
permissions,
searchParams,
user,
visibleEntities,
},
serverProps,
})

return (
Expand All @@ -99,16 +106,7 @@ export const DefaultTemplate: React.FC<DefaultTemplateProps> = ({
clientProps: { clientProps: { visibleEntities } },
Component: CustomHeader,
importMap: payload.importMap,
serverProps: {
i18n,
locale,
params,
payload,
permissions,
searchParams,
user,
visibleEntities,
},
serverProps,
})}
<div style={{ position: 'relative' }}>
<div className={`${baseClass}__nav-toggler-wrapper`} id="nav-toggler">
Expand All @@ -127,6 +125,7 @@ export const DefaultTemplate: React.FC<DefaultTemplateProps> = ({
? RenderServerComponent({
Component: avatar.Component,
importMap: payload.importMap,
serverProps,
})
: undefined
}
Expand All @@ -135,6 +134,7 @@ export const DefaultTemplate: React.FC<DefaultTemplateProps> = ({
? RenderServerComponent({
Component: components.graphics.Icon,
importMap: payload.importMap,
serverProps,
})
: undefined
}
Expand Down
14 changes: 13 additions & 1 deletion packages/next/src/views/Document/renderDocumentSlots.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import type {
SanitizedCollectionConfig,
SanitizedDocumentPermissions,
SanitizedGlobalConfig,
ServerProps,
StaticDescription,
} from 'payload'

import { ViewDescription } from '@payloadcms/ui'
import { RenderServerComponent } from '@payloadcms/ui/elements/RenderServerComponent'
import React from 'react'

import { getDocumentPermissions } from './getDocumentPermissions.js'

Expand All @@ -29,6 +29,13 @@ export const renderDocumentSlots: (args: {

const isPreviewEnabled = collectionConfig?.admin?.preview || globalConfig?.admin?.preview

const serverProps: ServerProps = {
i18n: req.i18n,
payload: req.payload,
user: req.user,
// TODO: Add remaining serverProps
}

const CustomPreviewButton =
collectionConfig?.admin?.components?.edit?.PreviewButton ||
globalConfig?.admin?.components?.elements?.PreviewButton
Expand All @@ -37,6 +44,7 @@ export const renderDocumentSlots: (args: {
components.PreviewButton = RenderServerComponent({
Component: CustomPreviewButton,
importMap: req.payload.importMap,
serverProps,
})
}

Expand All @@ -60,6 +68,7 @@ export const renderDocumentSlots: (args: {
Component: CustomDescription,
Fallback: ViewDescription,
importMap: req.payload.importMap,
serverProps,
})
}

Expand All @@ -73,6 +82,7 @@ export const renderDocumentSlots: (args: {
components.PublishButton = RenderServerComponent({
Component: CustomPublishButton,
importMap: req.payload.importMap,
serverProps,
})
}
const CustomSaveDraftButton =
Expand All @@ -87,6 +97,7 @@ export const renderDocumentSlots: (args: {
components.SaveDraftButton = RenderServerComponent({
Component: CustomSaveDraftButton,
importMap: req.payload.importMap,
serverProps,
})
}
} else {
Expand All @@ -98,6 +109,7 @@ export const renderDocumentSlots: (args: {
components.SaveButton = RenderServerComponent({
Component: CustomSaveButton,
importMap: req.payload.importMap,
serverProps,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/payload/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ export type Config = {
| 'default'
| 'gravatar'
| {
Component: PayloadComponent<never>
Component: PayloadComponent
}
/**
* Add extra and/or replace built-in components with custom components
Expand Down

0 comments on commit 0303b78

Please sign in to comment.