Skip to content

Commit

Permalink
fix(plugin-nested-docs): breadcrumbsFieldSlug used in resaveSelfAfter…
Browse files Browse the repository at this point in the history
…Create hook
  • Loading branch information
DanRibbens committed Dec 28, 2023
1 parent 7db58b4 commit a5a91c0
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
13 changes: 5 additions & 8 deletions packages/plugin-nested-docs/src/hooks/resaveSelfAfterCreate.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import type { CollectionAfterChangeHook, CollectionConfig } from 'payload/types'

import type { Breadcrumb } from '../types'

interface DocWithBreadcrumbs {
breadcrumbs: Breadcrumb[]
}
import type { PluginConfig } from '../types'

// This hook automatically re-saves a document after it is created
// so that we can build its breadcrumbs with the newly created document's ID.

const resaveSelfAfterCreate =
(collection: CollectionConfig): CollectionAfterChangeHook =>
(pluginConfig: PluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>
async ({ doc, operation, req }) => {
const { locale, payload } = req
const { breadcrumbs = [] } = doc as DocWithBreadcrumbs
const breadcrumbSlug = pluginConfig.breadcrumbsFieldSlug || 'breadcrumbs'
const breadcrumbs = doc[breadcrumbSlug]

if (operation === 'create') {
const originalDocWithDepth0 = await payload.findByID({
Expand All @@ -34,7 +31,7 @@ const resaveSelfAfterCreate =
collection: collection.slug,
data: {
...originalDocWithDepth0,
breadcrumbs:
[breadcrumbSlug]:
breadcrumbs?.map((crumb, i) => ({
...crumb,
doc: breadcrumbs.length === i + 1 ? doc.id : crumb.doc,
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-nested-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const nestedDocs =
...(collection.hooks || {}),
afterChange: [
resaveChildren(pluginConfig, collection),
resaveSelfAfterCreate(collection),
resaveSelfAfterCreate(pluginConfig, collection),
...(collection?.hooks?.afterChange || []),
],
beforeChange: [
Expand Down

0 comments on commit a5a91c0

Please sign in to comment.