Skip to content
This repository was archived by the owner on Dec 13, 2023. It is now read-only.
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
17 changes: 16 additions & 1 deletion demo/src/collections/Pages.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// const payload = require('payload');
import type { CollectionConfig } from 'payload/types'

import populateFullTitle from './populateFullTitle'

export const Pages: CollectionConfig = {
slug: 'pages',
labels: {
singular: 'Page',
plural: 'Pages',
},
admin: {
useAsTitle: 'title',
useAsTitle: 'fullTitle',
},
access: {
read: () => true,
Expand All @@ -26,5 +28,18 @@ export const Pages: CollectionConfig = {
type: 'text',
required: true,
},
{
name: 'fullTitle',
type: 'text',
localized: true,
hooks: {
beforeChange: [populateFullTitle],
},
admin: {
components: {
Field: () => null,
},
},
},
],
}
17 changes: 17 additions & 0 deletions demo/src/collections/populateFullTitle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type { FieldHook } from 'payload/types'

export const generateFullTitle = (breadcrumbs: Array<{ label: string }>): string | undefined => {
if (Array.isArray(breadcrumbs)) {
return breadcrumbs.reduce((title, breadcrumb, i) => {
if (i === 0) return `${breadcrumb.label}`
return `${title} > ${breadcrumb.label}`
}, '')
}

return undefined
}

const populateFullTitle: FieldHook = async ({ data, originalDoc }) =>
generateFullTitle(data?.breadcrumbs || originalDoc?.breadcrumbs)

export default populateFullTitle
16 changes: 6 additions & 10 deletions demo/src/payload.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import { buildConfig } from 'payload/config'

// import nestedPages from '../../dist';
import nestedPages from '../../src'
import nestedPages from '../../src' // eslint-disable-line import/no-relative-packages
import { Pages } from './collections/Pages'
import { Users } from './collections/Users'

Expand All @@ -28,15 +28,11 @@ export default buildConfig({
},
},
collections: [Users, Pages],
// localization: {
// locales: [
// 'en',
// 'es',
// 'de',
// ],
// defaultLocale: 'en',
// fallback: true,
// },
localization: {
locales: ['en', 'es', 'de'],
defaultLocale: 'en',
fallback: true,
},
plugins: [
nestedPages({
collections: ['pages'],
Expand Down
3 changes: 2 additions & 1 deletion demo/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"jsx": "react",
},
"ts-node": {
"transpileOnly": true
"transpileOnly": true,
"swc": true
}
}
1 change: 1 addition & 0 deletions src/fields/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const createBreadcrumbsField = (
): Field => ({
name: 'breadcrumbs',
type: 'array',
localized: true,
fields: [
{
name: 'doc',
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/resaveChildren.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import populateBreadcrumbs from '../utilities/populateBreadcrumbs'

const resaveChildren =
(pluginConfig: PluginConfig, collection: CollectionConfig): CollectionAfterChangeHook =>
({ req: { payload }, req, doc }) => {
({ req: { payload, locale }, req, doc }) => {
const resaveChildrenAsync = async (): Promise<void> => {
const children = await payload.find({
collection: collection.slug,
Expand All @@ -15,6 +15,7 @@ const resaveChildren =
},
},
depth: 0,
locale,
})

try {
Expand All @@ -33,6 +34,7 @@ const resaveChildren =
breadcrumbs: populateBreadcrumbs(req, pluginConfig, collection, child),
},
depth: 0,
locale,
})
})
} catch (err: unknown) {
Expand Down
3 changes: 2 additions & 1 deletion src/hooks/resaveSelfAfterCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface DocWithBreadcrumbs {

const resaveSelfAfterCreate =
(collection: CollectionConfig): CollectionAfterChangeHook =>
async ({ req: { payload }, doc, operation }) => {
async ({ req: { payload, locale }, doc, operation }) => {
const { breadcrumbs = [] } = doc as DocWithBreadcrumbs

if (operation === 'create') {
Expand All @@ -30,6 +30,7 @@ const resaveSelfAfterCreate =
await payload.update({
collection: collection.slug,
id: doc.id,
locale,
depth: 0,
draft: updateAsDraft,
data: {
Expand Down
6 changes: 1 addition & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ const nestedDocs =
}

if (!pluginConfig.breadcrumbsFieldSlug) {
fields.push(
createBreadcrumbsField(collection.slug, {
localized: Boolean(config.localization),
}),
)
fields.push(createBreadcrumbsField(collection.slug))
}

return {
Expand Down