Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(plugin-seo): return empty if localized fields are only seo #30

Merged
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
74 changes: 74 additions & 0 deletions src/utilities/getLocalizedFields.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,78 @@ describe("fn: getLocalizedFields", () => {
})
*/
})

/**
* @see https://github.com/payloadcms/plugin-seo
*
* payloadcms/plugin-seo adds localized fields.
* If there are no other localized fields, we don't
* want to submit to CrowdIn.
*/
describe("payloadcms/plugin-seo tests", () => {
const seoFields: Field[] = [
{
"name": "meta",
"label": "SEO",
"type": "group",
"fields": [
/**{
"name": "overview",
"label": "Overview",
"type": "ui",
"admin": {
"components": {}
}
},*/
{
"name": "title",
"type": "text",
"localized": true,
"admin": {
"components": {}
}
},
{
"name": "description",
"type": "textarea",
"localized": true,
"admin": {
"components": {}
}
},
/**{
"name": "preview",
"label": "Preview",
"type": "ui",
"admin": {
"components": {}
}
}**/
]
}
]

it("excludes payloadcms/plugin-seo localized fields if there are no localized fields on the collection/global", () => {
const nonLocalizedFieldCollection: Field[] = [
{
name: 'textLocalizedField',
type: 'text',
},
{
name: 'richTextLocalizedField',
type: 'richText',
},
{
name: 'textareaLocalizedField',
type: 'text',
},
]

const fields: Field[] = [
...nonLocalizedFieldCollection,
...seoFields,
]
expect(getLocalizedFields({ fields })).toEqual([])
})
})
})
22 changes: 21 additions & 1 deletion src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import deepEqual from 'deep-equal'
import { FieldWithName } from '../types'
import { slateToHtml, payloadSlateToDomConfig } from 'slate-serializers'
import type { Descendant } from 'slate'
import { isEmpty, merge } from "lodash"
import { get, isEmpty, merge } from "lodash"

const localizedFieldTypes = [
'richText',
Expand All @@ -27,6 +27,26 @@ export const getLocalizedFields = ({
fields: Field[],
type?: 'json' | 'html',
localizedParent?: boolean
}): any[] => {
const localizedFields = getLocalizedFieldsRecursive({
fields,
type,
localizedParent,
})
if (localizedFields.length === 1 && get(localizedFields[0], 'name') === 'meta') {
return []
}
return localizedFields
}

export const getLocalizedFieldsRecursive = ({
fields,
type,
localizedParent = false,
}: {
fields: Field[],
type?: 'json' | 'html',
localizedParent?: boolean
}): any[] => ([
...fields
// localized or group fields only.
Expand Down