From 48390bbd8ae6156d4637906426df781f148825d6 Mon Sep 17 00:00:00 2001 From: Steven Thompson <44806974+thompsonsj@users.noreply.github.com> Date: Tue, 4 Jul 2023 15:44:18 +0100 Subject: [PATCH] fix(utilities): exclude id properties from arrays (#64) --- dev/src/collections/NestedFieldCollection.ts | 44 +++++++++++++++-- dev/src/tests/files.test.ts | 49 ++++++++++++++----- src/utilities/index.ts | 9 +++- .../combined-field-types.spec.ts | 4 -- 4 files changed, 85 insertions(+), 21 deletions(-) diff --git a/dev/src/collections/NestedFieldCollection.ts b/dev/src/collections/NestedFieldCollection.ts index dd2fc03..b52942a 100644 --- a/dev/src/collections/NestedFieldCollection.ts +++ b/dev/src/collections/NestedFieldCollection.ts @@ -6,6 +6,32 @@ const LocalizedBlock: Block = { fields: basicLocalizedFields, }; +const TestBlockArrayOfRichText: Block = { + slug: 'testBlockArrayOfRichText', + fields: [{ + name: 'title', + type: 'text', + localized: true, + }, + { + name: 'messages', + type: 'array', + localized: true, + maxRows: 3, + fields: [ + { + name: 'title', + type: 'text', + localized: true, + }, + { + name: 'message', + type: 'richText' + } + ] + },], +} + const NestedFieldCollection: CollectionConfig = { slug: 'nested-field-collection', access: { @@ -27,14 +53,15 @@ const NestedFieldCollection: CollectionConfig = { type: 'blocks', // required blocks: [ LocalizedBlock, + TestBlockArrayOfRichText, ], }, // collapsible - { + /*{ label: 'Collapsible', type: 'collapsible', fields: basicLocalizedFields, - }, + },*/ // group { name: "group", // required @@ -63,7 +90,18 @@ const NestedFieldCollection: CollectionConfig = { { name: "tabTwo", label: "Tab Two Label", - fields: basicLocalizedFields, + fields: [ + { + name: 'tabTwoTitle', + type: 'text', + localized: true, + }, + { + name: 'tabTwoContent', + type: 'richText', + localized: true, + }, + ], }, ], }, diff --git a/dev/src/tests/files.test.ts b/dev/src/tests/files.test.ts index ad6011b..8e088f3 100644 --- a/dev/src/tests/files.test.ts +++ b/dev/src/tests/files.test.ts @@ -66,7 +66,7 @@ describe(`CrowdIn file create, update and delete`, () => { * * ValidationError: nested-field-collection validation failed: title.en: Cast to string failed for value "{ en: 'Test title' }" (type Object) at path "en" * model.Object..Document.invalidate (dev/node_modules/mongoose/lib/document.js:3050:32) - * / + */ it('creates files containing fieldType content', async () => { const article = await payload.create({ collection: collections.nestedFields, @@ -90,7 +90,6 @@ describe(`CrowdIn file create, update and delete`, () => { expect(crowdInFiles.find((file) => file.name === 'content.html')).toBeDefined() expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() }) - */ it('creates files containing `array` fieldType content', async () => { const article = await payload.create({ @@ -153,27 +152,53 @@ describe(`CrowdIn file create, update and delete`, () => { blockType: 'basicBlock' }, { - title: 'Test title 2', - content: [ + messages: [ { - children: [ + message: [ { - text: "Test content 2" + children: [ + { + text: "Test content 1" + } + ] } - ] + ], + id: "64735620230d57bce946d370" + }, + { + message: [ + { + children: [ + { + text: "Test content 1" + } + ] + } + ], + id: "64735621230d57bce946d371" } ], - metaDescription: 'Test meta description 2', - blockType: 'basicBlock' + blockType: 'testBlockArrayOfRichText', }, ], }, }); const crowdInFiles = await getFilesByDocumentID(article.id, payload) - expect(crowdInFiles.length).toEqual(3) + expect(crowdInFiles.length).toEqual(4) + const jsonFile = crowdInFiles.find((file) => file.name === 'fields.json') expect(crowdInFiles.find((file) => file.name === 'layout[0].content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'layout[1].content.html')).toBeDefined() - expect(crowdInFiles.find((file) => file.name === 'fields.json')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'layout[1].messages[0].message.html')).toBeDefined() + expect(crowdInFiles.find((file) => file.name === 'layout[1].messages[1].message.html')).toBeDefined() + expect(jsonFile).toBeDefined() + expect(jsonFile.fileData.json).toEqual( + { + layout: [ + { + title: 'Test title 1', + } + ] + } + ) }) }) }); diff --git a/src/utilities/index.ts b/src/utilities/index.ts index c528040..e660e8e 100644 --- a/src/utilities/index.ts +++ b/src/utilities/index.ts @@ -143,7 +143,12 @@ export const getFieldSlugs = (fields: FieldWithName[]): string[] => fields.filte const hasLocalizedProp = (field: Field) => "localized" in field && field.localized -export const isLocalizedField = (field: Field, addLocalizedProp: boolean = false) => (hasLocalizedProp(field) || addLocalizedProp) && localizedFieldTypes.includes(field.type) && !excludeBasedOnDescription(field) +/** + * Is Localized Field + * + * Note that `id` should be excluded - it is a `text` field that is added by Payload CMS. + */ +export const isLocalizedField = (field: Field, addLocalizedProp: boolean = false) => (hasLocalizedProp(field) || addLocalizedProp) && localizedFieldTypes.includes(field.type) && !excludeBasedOnDescription(field) && (field as any).name !== 'id' const excludeBasedOnDescription = (field: Field) => { const description = get(field, 'admin.description', '') @@ -205,7 +210,7 @@ export const buildCrowdinJsonObject = ({ doc: item, fields: field.fields, topLevel: false, - })) + })).filter((item: any) => !isEmpty(item)) } else if (field.type === 'blocks') { response[field.name] = doc[field.name].map((item: any) => buildCrowdinJsonObject({ doc: item, diff --git a/src/utilities/tests/buildJsonCrowdinObject/combined-field-types.spec.ts b/src/utilities/tests/buildJsonCrowdinObject/combined-field-types.spec.ts index 54ae774..89eba78 100644 --- a/src/utilities/tests/buildJsonCrowdinObject/combined-field-types.spec.ts +++ b/src/utilities/tests/buildJsonCrowdinObject/combined-field-types.spec.ts @@ -167,10 +167,6 @@ describe("fn: buildCrowdinJsonObject: group nested in array", () => { }, fields: getLocalizedFields({ fields: Promos.fields }) })).toEqual({ - ctas: [ - {}, - {} - ], "text": "Get in touch with us or try it out yourself", "title": "Experience the magic of our product!" })