fix(db-postgres): nested localized arrays (#7962)
## Description Fixes a bug with nested arrays within either localized blocks or arrays.
This commit is contained in:
@@ -177,7 +177,7 @@ export const traverseFields = <T extends Record<string, unknown>>({
|
||||
return arrayResult
|
||||
}, {})
|
||||
} else {
|
||||
result[field.name] = fieldData.map((row, i) => {
|
||||
result[field.name] = fieldData.reduce((acc, row, i) => {
|
||||
if (row._uuid) {
|
||||
row.id = row._uuid
|
||||
delete row._uuid
|
||||
@@ -187,22 +187,35 @@ export const traverseFields = <T extends Record<string, unknown>>({
|
||||
delete row._order
|
||||
}
|
||||
|
||||
return traverseFields<T>({
|
||||
adapter,
|
||||
blocks,
|
||||
config,
|
||||
dataRef: row,
|
||||
deletions,
|
||||
fieldPrefix: '',
|
||||
fields: field.fields,
|
||||
numbers,
|
||||
path: `${sanitizedPath}${field.name}.${i}`,
|
||||
relationships,
|
||||
table: row,
|
||||
texts,
|
||||
withinArrayOrBlockLocale,
|
||||
})
|
||||
})
|
||||
if (
|
||||
!withinArrayOrBlockLocale ||
|
||||
(withinArrayOrBlockLocale && withinArrayOrBlockLocale === row._locale)
|
||||
) {
|
||||
if (row._locale) {
|
||||
delete row._locale
|
||||
}
|
||||
|
||||
acc.push(
|
||||
traverseFields<T>({
|
||||
adapter,
|
||||
blocks,
|
||||
config,
|
||||
dataRef: row,
|
||||
deletions,
|
||||
fieldPrefix: '',
|
||||
fields: field.fields,
|
||||
numbers,
|
||||
path: `${sanitizedPath}${field.name}.${i}`,
|
||||
relationships,
|
||||
table: row,
|
||||
texts,
|
||||
withinArrayOrBlockLocale,
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
return acc
|
||||
}, [])
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,22 @@ export const BlocksCollection: CollectionConfig = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'array',
|
||||
type: 'array',
|
||||
fields: [
|
||||
{
|
||||
name: 'link',
|
||||
type: 'group',
|
||||
fields: [
|
||||
{
|
||||
name: 'label',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
|
||||
@@ -2,22 +2,57 @@ import type { CollectionConfig } from 'payload'
|
||||
|
||||
export const NestedArray: CollectionConfig = {
|
||||
slug: 'nested-arrays',
|
||||
versions: {
|
||||
drafts: true,
|
||||
},
|
||||
fields: [
|
||||
{
|
||||
name: 'arrayWithBlocks',
|
||||
type: 'array',
|
||||
localized: true,
|
||||
fields: [
|
||||
type: 'tabs',
|
||||
tabs: [
|
||||
{
|
||||
name: 'blocksWithinArray',
|
||||
type: 'blocks',
|
||||
blocks: [
|
||||
label: 'My Tab',
|
||||
fields: [
|
||||
{
|
||||
slug: 'someBlock',
|
||||
name: 'arrayWithBlocks',
|
||||
type: 'array',
|
||||
localized: true,
|
||||
fields: [
|
||||
{
|
||||
name: 'relationWithinBlock',
|
||||
name: 'blocksWithinArray',
|
||||
type: 'blocks',
|
||||
blocks: [
|
||||
{
|
||||
slug: 'someBlock',
|
||||
fields: [
|
||||
{
|
||||
name: 'relationWithinBlock',
|
||||
type: 'relationship',
|
||||
relationTo: 'localized-posts',
|
||||
},
|
||||
{
|
||||
name: 'myGroup',
|
||||
type: 'group',
|
||||
fields: [
|
||||
{
|
||||
name: 'text',
|
||||
type: 'text',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'arrayWithLocalizedRelation',
|
||||
type: 'array',
|
||||
fields: [
|
||||
{
|
||||
name: 'localizedRelation',
|
||||
type: 'relationship',
|
||||
localized: true,
|
||||
relationTo: 'localized-posts',
|
||||
},
|
||||
],
|
||||
@@ -26,17 +61,5 @@ export const NestedArray: CollectionConfig = {
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
name: 'arrayWithLocalizedRelation',
|
||||
type: 'array',
|
||||
fields: [
|
||||
{
|
||||
name: 'localizedRelation',
|
||||
type: 'relationship',
|
||||
localized: true,
|
||||
relationTo: 'localized-posts',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
@@ -1287,6 +1287,7 @@ describe('Localization', () => {
|
||||
locale: englishLocale,
|
||||
id: result.id,
|
||||
})
|
||||
|
||||
const docEs = await payload.findByID({
|
||||
collection: tabSlug,
|
||||
locale: spanishLocale,
|
||||
@@ -1406,7 +1407,7 @@ describe('Localization', () => {
|
||||
expect(collection.fields[3].fields[0].localized).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
describe('nested blocks', () => {
|
||||
let id
|
||||
it('should allow creating nested blocks per locale', async () => {
|
||||
@@ -1416,6 +1417,18 @@ describe('Localization', () => {
|
||||
content: [
|
||||
{
|
||||
blockType: 'blockInsideBlock',
|
||||
array: [
|
||||
{
|
||||
link: {
|
||||
label: 'English 1',
|
||||
},
|
||||
},
|
||||
{
|
||||
link: {
|
||||
label: 'English 2',
|
||||
},
|
||||
},
|
||||
],
|
||||
content: [
|
||||
{
|
||||
blockType: 'textBlock',
|
||||
@@ -1429,6 +1442,11 @@ describe('Localization', () => {
|
||||
|
||||
id = doc.id
|
||||
|
||||
const retrievedInEN = await payload.findByID({
|
||||
collection: 'blocks-fields',
|
||||
id,
|
||||
})
|
||||
|
||||
await payload.update({
|
||||
collection: 'blocks-fields',
|
||||
id,
|
||||
@@ -1437,6 +1455,18 @@ describe('Localization', () => {
|
||||
content: [
|
||||
{
|
||||
blockType: 'blockInsideBlock',
|
||||
array: [
|
||||
{
|
||||
link: {
|
||||
label: 'Spanish 1',
|
||||
},
|
||||
},
|
||||
{
|
||||
link: {
|
||||
label: 'Spanish 2',
|
||||
},
|
||||
},
|
||||
],
|
||||
content: [
|
||||
{
|
||||
blockType: 'textBlock',
|
||||
@@ -1456,6 +1486,12 @@ describe('Localization', () => {
|
||||
|
||||
expect(retrieved.content.en[0].content).toHaveLength(1)
|
||||
expect(retrieved.content.es[0].content).toHaveLength(1)
|
||||
|
||||
expect(retrieved.content.en[0].array[0].link.label).toStrictEqual('English 1')
|
||||
expect(retrieved.content.en[0].array[1].link.label).toStrictEqual('English 2')
|
||||
|
||||
expect(retrieved.content.es[0].array[0].link.label).toStrictEqual('Spanish 1')
|
||||
expect(retrieved.content.es[0].array[1].link.label).toStrictEqual('Spanish 2')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1480,16 +1516,25 @@ describe('Localization', () => {
|
||||
blockName: '1',
|
||||
blockType: 'someBlock',
|
||||
relationWithinBlock: randomDoc.id,
|
||||
myGroup: {
|
||||
text: 'hello in english 1',
|
||||
},
|
||||
},
|
||||
{
|
||||
blockName: '2',
|
||||
blockType: 'someBlock',
|
||||
relationWithinBlock: randomDoc.id,
|
||||
myGroup: {
|
||||
text: 'hello in english 2',
|
||||
},
|
||||
},
|
||||
{
|
||||
blockName: '3',
|
||||
blockType: 'someBlock',
|
||||
relationWithinBlock: randomDoc.id,
|
||||
myGroup: {
|
||||
text: 'hello in english 3',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1498,16 +1543,25 @@ describe('Localization', () => {
|
||||
blockName: '1',
|
||||
blockType: 'someBlock',
|
||||
relationWithinBlock: randomDoc2.id,
|
||||
myGroup: {
|
||||
text: 'hello in spanish 1',
|
||||
},
|
||||
},
|
||||
{
|
||||
blockName: '2',
|
||||
blockType: 'someBlock',
|
||||
relationWithinBlock: randomDoc2.id,
|
||||
myGroup: {
|
||||
text: 'hello in spanish 2',
|
||||
},
|
||||
},
|
||||
{
|
||||
blockName: '3',
|
||||
blockType: 'someBlock',
|
||||
relationWithinBlock: randomDoc2.id,
|
||||
myGroup: {
|
||||
text: 'hello in spanish 3',
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user