From 6875f0febf413ecf99f15dc8ee373c13679b52e3 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Mon, 10 Jul 2023 16:04:19 -0400 Subject: [PATCH 1/2] fix: threads locale through resaveChildren --- demo/src/collections/Pages.ts | 17 ++++++++++++++++- demo/src/collections/populateFullTitle.ts | 17 +++++++++++++++++ demo/src/payload.config.ts | 16 ++++++---------- src/hooks/resaveChildren.ts | 4 +++- src/hooks/resaveSelfAfterCreate.ts | 3 ++- 5 files changed, 44 insertions(+), 13 deletions(-) create mode 100644 demo/src/collections/populateFullTitle.ts diff --git a/demo/src/collections/Pages.ts b/demo/src/collections/Pages.ts index 425beff..3735c1e 100644 --- a/demo/src/collections/Pages.ts +++ b/demo/src/collections/Pages.ts @@ -1,6 +1,8 @@ // const payload = require('payload'); import type { CollectionConfig } from 'payload/types' +import populateFullTitle from './populateFullTitle' + export const Pages: CollectionConfig = { slug: 'pages', labels: { @@ -8,7 +10,7 @@ export const Pages: CollectionConfig = { plural: 'Pages', }, admin: { - useAsTitle: 'title', + useAsTitle: 'fullTitle', }, access: { read: () => true, @@ -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, + }, + }, + }, ], } diff --git a/demo/src/collections/populateFullTitle.ts b/demo/src/collections/populateFullTitle.ts new file mode 100644 index 0000000..8fe1c48 --- /dev/null +++ b/demo/src/collections/populateFullTitle.ts @@ -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 diff --git a/demo/src/payload.config.ts b/demo/src/payload.config.ts index b7dd8e4..f548bf1 100644 --- a/demo/src/payload.config.ts +++ b/demo/src/payload.config.ts @@ -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' @@ -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'], diff --git a/src/hooks/resaveChildren.ts b/src/hooks/resaveChildren.ts index d97357e..5617ced 100644 --- a/src/hooks/resaveChildren.ts +++ b/src/hooks/resaveChildren.ts @@ -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 => { const children = await payload.find({ collection: collection.slug, @@ -15,6 +15,7 @@ const resaveChildren = }, }, depth: 0, + locale, }) try { @@ -33,6 +34,7 @@ const resaveChildren = breadcrumbs: populateBreadcrumbs(req, pluginConfig, collection, child), }, depth: 0, + locale, }) }) } catch (err: unknown) { diff --git a/src/hooks/resaveSelfAfterCreate.ts b/src/hooks/resaveSelfAfterCreate.ts index ce51352..e6dc133 100644 --- a/src/hooks/resaveSelfAfterCreate.ts +++ b/src/hooks/resaveSelfAfterCreate.ts @@ -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') { @@ -30,6 +30,7 @@ const resaveSelfAfterCreate = await payload.update({ collection: collection.slug, id: doc.id, + locale, depth: 0, draft: updateAsDraft, data: { From 134b3e12152a86501e7aab54e212c1b112ba3268 Mon Sep 17 00:00:00 2001 From: Jacob Fletcher Date: Mon, 10 Jul 2023 16:38:43 -0400 Subject: [PATCH 2/2] chore: simplifies breadcrumbs localization --- demo/tsconfig.json | 3 ++- src/fields/breadcrumbs.ts | 1 + src/index.ts | 6 +----- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/demo/tsconfig.json b/demo/tsconfig.json index 7b58db4..eaec351 100644 --- a/demo/tsconfig.json +++ b/demo/tsconfig.json @@ -14,6 +14,7 @@ "jsx": "react", }, "ts-node": { - "transpileOnly": true + "transpileOnly": true, + "swc": true } } diff --git a/src/fields/breadcrumbs.ts b/src/fields/breadcrumbs.ts index 03f1bdb..86bbac4 100644 --- a/src/fields/breadcrumbs.ts +++ b/src/fields/breadcrumbs.ts @@ -7,6 +7,7 @@ const createBreadcrumbsField = ( ): Field => ({ name: 'breadcrumbs', type: 'array', + localized: true, fields: [ { name: 'doc', diff --git a/src/index.ts b/src/index.ts index 46ff5e4..bf52e66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -20,11 +20,7 @@ const nestedDocs = } if (!pluginConfig.breadcrumbsFieldSlug) { - fields.push( - createBreadcrumbsField(collection.slug, { - localized: Boolean(config.localization), - }), - ) + fields.push(createBreadcrumbsField(collection.slug)) } return {