Skip to content

Commit ac10bad

Browse files
authored
fix(db-postgres): nested localized arrays (#7962)
## Description Fixes a bug with nested arrays within either localized blocks or arrays.
1 parent 142616e commit ac10bad

File tree

4 files changed

+145
-39
lines changed

4 files changed

+145
-39
lines changed

packages/drizzle/src/transform/read/traverseFields.ts

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export const traverseFields = <T extends Record<string, unknown>>({
177177
return arrayResult
178178
}, {})
179179
} else {
180-
result[field.name] = fieldData.map((row, i) => {
180+
result[field.name] = fieldData.reduce((acc, row, i) => {
181181
if (row._uuid) {
182182
row.id = row._uuid
183183
delete row._uuid
@@ -187,22 +187,35 @@ export const traverseFields = <T extends Record<string, unknown>>({
187187
delete row._order
188188
}
189189

190-
return traverseFields<T>({
191-
adapter,
192-
blocks,
193-
config,
194-
dataRef: row,
195-
deletions,
196-
fieldPrefix: '',
197-
fields: field.fields,
198-
numbers,
199-
path: `${sanitizedPath}${field.name}.${i}`,
200-
relationships,
201-
table: row,
202-
texts,
203-
withinArrayOrBlockLocale,
204-
})
205-
})
190+
if (
191+
!withinArrayOrBlockLocale ||
192+
(withinArrayOrBlockLocale && withinArrayOrBlockLocale === row._locale)
193+
) {
194+
if (row._locale) {
195+
delete row._locale
196+
}
197+
198+
acc.push(
199+
traverseFields<T>({
200+
adapter,
201+
blocks,
202+
config,
203+
dataRef: row,
204+
deletions,
205+
fieldPrefix: '',
206+
fields: field.fields,
207+
numbers,
208+
path: `${sanitizedPath}${field.name}.${i}`,
209+
relationships,
210+
table: row,
211+
texts,
212+
withinArrayOrBlockLocale,
213+
}),
214+
)
215+
}
216+
217+
return acc
218+
}, [])
206219
}
207220
}
208221

test/localization/collections/Blocks/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ export const BlocksCollection: CollectionConfig = {
2929
},
3030
],
3131
},
32+
{
33+
name: 'array',
34+
type: 'array',
35+
fields: [
36+
{
37+
name: 'link',
38+
type: 'group',
39+
fields: [
40+
{
41+
name: 'label',
42+
type: 'text',
43+
},
44+
],
45+
},
46+
],
47+
},
3248
],
3349
},
3450
],

test/localization/collections/NestedArray/index.ts

Lines changed: 44 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,57 @@ import type { CollectionConfig } from 'payload'
22

33
export const NestedArray: CollectionConfig = {
44
slug: 'nested-arrays',
5+
versions: {
6+
drafts: true,
7+
},
58
fields: [
69
{
7-
name: 'arrayWithBlocks',
8-
type: 'array',
9-
localized: true,
10-
fields: [
10+
type: 'tabs',
11+
tabs: [
1112
{
12-
name: 'blocksWithinArray',
13-
type: 'blocks',
14-
blocks: [
13+
label: 'My Tab',
14+
fields: [
1515
{
16-
slug: 'someBlock',
16+
name: 'arrayWithBlocks',
17+
type: 'array',
18+
localized: true,
1719
fields: [
1820
{
19-
name: 'relationWithinBlock',
21+
name: 'blocksWithinArray',
22+
type: 'blocks',
23+
blocks: [
24+
{
25+
slug: 'someBlock',
26+
fields: [
27+
{
28+
name: 'relationWithinBlock',
29+
type: 'relationship',
30+
relationTo: 'localized-posts',
31+
},
32+
{
33+
name: 'myGroup',
34+
type: 'group',
35+
fields: [
36+
{
37+
name: 'text',
38+
type: 'text',
39+
},
40+
],
41+
},
42+
],
43+
},
44+
],
45+
},
46+
],
47+
},
48+
{
49+
name: 'arrayWithLocalizedRelation',
50+
type: 'array',
51+
fields: [
52+
{
53+
name: 'localizedRelation',
2054
type: 'relationship',
55+
localized: true,
2156
relationTo: 'localized-posts',
2257
},
2358
],
@@ -26,17 +61,5 @@ export const NestedArray: CollectionConfig = {
2661
},
2762
],
2863
},
29-
{
30-
name: 'arrayWithLocalizedRelation',
31-
type: 'array',
32-
fields: [
33-
{
34-
name: 'localizedRelation',
35-
type: 'relationship',
36-
localized: true,
37-
relationTo: 'localized-posts',
38-
},
39-
],
40-
},
4164
],
4265
}

test/localization/int.spec.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1287,6 +1287,7 @@ describe('Localization', () => {
12871287
locale: englishLocale,
12881288
id: result.id,
12891289
})
1290+
12901291
const docEs = await payload.findByID({
12911292
collection: tabSlug,
12921293
locale: spanishLocale,
@@ -1406,7 +1407,7 @@ describe('Localization', () => {
14061407
expect(collection.fields[3].fields[0].localized).toBeUndefined()
14071408
})
14081409
})
1409-
1410+
14101411
describe('nested blocks', () => {
14111412
let id
14121413
it('should allow creating nested blocks per locale', async () => {
@@ -1416,6 +1417,18 @@ describe('Localization', () => {
14161417
content: [
14171418
{
14181419
blockType: 'blockInsideBlock',
1420+
array: [
1421+
{
1422+
link: {
1423+
label: 'English 1',
1424+
},
1425+
},
1426+
{
1427+
link: {
1428+
label: 'English 2',
1429+
},
1430+
},
1431+
],
14191432
content: [
14201433
{
14211434
blockType: 'textBlock',
@@ -1429,6 +1442,11 @@ describe('Localization', () => {
14291442

14301443
id = doc.id
14311444

1445+
const retrievedInEN = await payload.findByID({
1446+
collection: 'blocks-fields',
1447+
id,
1448+
})
1449+
14321450
await payload.update({
14331451
collection: 'blocks-fields',
14341452
id,
@@ -1437,6 +1455,18 @@ describe('Localization', () => {
14371455
content: [
14381456
{
14391457
blockType: 'blockInsideBlock',
1458+
array: [
1459+
{
1460+
link: {
1461+
label: 'Spanish 1',
1462+
},
1463+
},
1464+
{
1465+
link: {
1466+
label: 'Spanish 2',
1467+
},
1468+
},
1469+
],
14401470
content: [
14411471
{
14421472
blockType: 'textBlock',
@@ -1456,6 +1486,12 @@ describe('Localization', () => {
14561486

14571487
expect(retrieved.content.en[0].content).toHaveLength(1)
14581488
expect(retrieved.content.es[0].content).toHaveLength(1)
1489+
1490+
expect(retrieved.content.en[0].array[0].link.label).toStrictEqual('English 1')
1491+
expect(retrieved.content.en[0].array[1].link.label).toStrictEqual('English 2')
1492+
1493+
expect(retrieved.content.es[0].array[0].link.label).toStrictEqual('Spanish 1')
1494+
expect(retrieved.content.es[0].array[1].link.label).toStrictEqual('Spanish 2')
14591495
})
14601496
})
14611497

@@ -1480,16 +1516,25 @@ describe('Localization', () => {
14801516
blockName: '1',
14811517
blockType: 'someBlock',
14821518
relationWithinBlock: randomDoc.id,
1519+
myGroup: {
1520+
text: 'hello in english 1',
1521+
},
14831522
},
14841523
{
14851524
blockName: '2',
14861525
blockType: 'someBlock',
14871526
relationWithinBlock: randomDoc.id,
1527+
myGroup: {
1528+
text: 'hello in english 2',
1529+
},
14881530
},
14891531
{
14901532
blockName: '3',
14911533
blockType: 'someBlock',
14921534
relationWithinBlock: randomDoc.id,
1535+
myGroup: {
1536+
text: 'hello in english 3',
1537+
},
14931538
},
14941539
]
14951540

@@ -1498,16 +1543,25 @@ describe('Localization', () => {
14981543
blockName: '1',
14991544
blockType: 'someBlock',
15001545
relationWithinBlock: randomDoc2.id,
1546+
myGroup: {
1547+
text: 'hello in spanish 1',
1548+
},
15011549
},
15021550
{
15031551
blockName: '2',
15041552
blockType: 'someBlock',
15051553
relationWithinBlock: randomDoc2.id,
1554+
myGroup: {
1555+
text: 'hello in spanish 2',
1556+
},
15061557
},
15071558
{
15081559
blockName: '3',
15091560
blockType: 'someBlock',
15101561
relationWithinBlock: randomDoc2.id,
1562+
myGroup: {
1563+
text: 'hello in spanish 3',
1564+
},
15111565
},
15121566
]
15131567

0 commit comments

Comments
 (0)