Skip to content

Commit

Permalink
fix(plugin-nested-docs): parent filterOptions errors when specifying …
Browse files Browse the repository at this point in the history
…breadcrumbsFieldSlug
  • Loading branch information
DanRibbens committed Dec 28, 2023
1 parent a5a91c0 commit c4a4678
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 15 deletions.
18 changes: 5 additions & 13 deletions packages/plugin-nested-docs/src/fields/parent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,15 @@ export const createParentField = (
>,
): SingleRelationshipField => ({
name: 'parent',
relationTo,
type: 'relationship',
maxDepth: 1,
filterOptions: ({ id }) => {
if (id) {
return {
id: { not_equals: id },
'breadcrumbs.doc': { not_in: [id] },
}
}

return null
},
admin: {
position: 'sidebar',
...(overrides?.admin || {}),
},
// filterOptions are assigned dynamically based on the pluginConfig
// filterOptions: parentFilterOptions(),
maxDepth: 1,
relationTo,
type: 'relationship',
...(overrides || {}),
})

Expand Down
16 changes: 16 additions & 0 deletions packages/plugin-nested-docs/src/fields/parentFilterOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { FilterOptions } from 'payload/dist/fields/config/types'

export const parentFilterOptions: (breadcrumbsFieldSlug?: string) => FilterOptions =
(breadcrumbsFieldSlug = 'breadcrumbs') =>
({ id }) => {
if (id) {
return {
id: { not_equals: id },
[`${breadcrumbsFieldSlug}.doc`]: { not_in: [id] },
}
}

return null
}

export default parentFilterOptions
16 changes: 14 additions & 2 deletions packages/plugin-nested-docs/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import type { Plugin } from 'payload/config'
import type { SingleRelationshipField } from 'payload/dist/fields/config/types'

import type { PluginConfig } from './types'

import createBreadcrumbsField from './fields/breadcrumbs'
import createParentField from './fields/parent'
import parentFilterOptions from './fields/parentFilterOptions'
import resaveChildren from './hooks/resaveChildren'
import resaveSelfAfterCreate from './hooks/resaveSelfAfterCreate'
import populateBreadcrumbs from './utilities/populateBreadcrumbs'
Expand All @@ -23,10 +25,20 @@ const nestedDocs =

const existingParentField = collection.fields.find(
(field) => 'name' in field && field.name === (pluginConfig?.parentFieldSlug || 'parent'),
)
) as SingleRelationshipField

const defaultFilterOptions = parentFilterOptions(pluginConfig?.breadcrumbsFieldSlug)

if (existingParentField) {
if (!existingParentField.filterOptions) {
existingParentField.filterOptions = defaultFilterOptions
}
}

if (!existingParentField && !pluginConfig.parentFieldSlug) {
fields.push(createParentField(collection.slug))
const defaultParentField = createParentField(collection.slug)
defaultParentField.filterOptions = defaultFilterOptions
fields.push(defaultParentField)
}

if (!existingBreadcrumbField && !pluginConfig.breadcrumbsFieldSlug) {
Expand Down
29 changes: 29 additions & 0 deletions test/plugin-nested-docs/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,34 @@ describe('Nested Docs', () => {

expect(parentField.admin.description).toStrictEqual('custom')
})

it('should allow custom breadcrumb slugs', async () => {
const parent = await payload.create({
collection: 'categories',
data: {
name: 'parent',
},
})
const child = await payload.create({
collection: 'categories',
data: {
name: 'child',
parent: parent.id,
},
})
const grandchild = await payload.create({
collection: 'categories',
data: {
name: 'grandchild',
parent: child.id,
},
})

expect(grandchild.categorization[0].doc).toStrictEqual(parent.id)
expect(grandchild.categorization[0].label).toStrictEqual('parent')
expect(grandchild.categorization[1].doc).toStrictEqual(child.id)
expect(grandchild.categorization[1].label).toStrictEqual('child')
expect(grandchild.categorization[2].label).toStrictEqual('grandchild')
})
})
})

0 comments on commit c4a4678

Please sign in to comment.