Skip to content

Commit

Permalink
fix(utilities): exclude id properties from arrays (#64)
Browse files Browse the repository at this point in the history
  • Loading branch information
thompsonsj authored Jul 4, 2023
1 parent 57eb12c commit 48390bb
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 21 deletions.
44 changes: 41 additions & 3 deletions dev/src/collections/NestedFieldCollection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -27,14 +53,15 @@ const NestedFieldCollection: CollectionConfig = {
type: 'blocks', // required
blocks: [
LocalizedBlock,
TestBlockArrayOfRichText,
],
},
// collapsible
{
/*{
label: 'Collapsible',
type: 'collapsible',
fields: basicLocalizedFields,
},
},*/
// group
{
name: "group", // required
Expand Down Expand Up @@ -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,
},
],
},
],
},
Expand Down
49 changes: 37 additions & 12 deletions dev/src/tests/files.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.<anonymous>.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,
Expand All @@ -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({
Expand Down Expand Up @@ -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',
}
]
}
)
})
})
});
9 changes: 7 additions & 2 deletions src/utilities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!"
})
Expand Down

0 comments on commit 48390bb

Please sign in to comment.