Skip to content

Commit 1e654c0

Browse files
authored
fix(db-mongodb): localized blocks with fallback and versions (#13974)
Fixes #13663 The issue appears to be reproducible only with 3 or more locales
1 parent 4b193da commit 1e654c0

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

packages/db-mongodb/src/utilities/transform.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,9 @@ const stripFields = ({
319319
const localeData = fieldData[localeKey]
320320

321321
if (!localeData || typeof localeData !== 'object') {
322+
if (field.type === 'blocks') {
323+
fieldData[localeKey] = []
324+
}
322325
continue
323326
}
324327

test/localization/collections/Blocks/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const blocksCollectionSlug = 'blocks-fields'
44

55
export const BlocksCollection: CollectionConfig = {
66
slug: blocksCollectionSlug,
7-
versions: { drafts: true },
7+
versions: { drafts: { autosave: true } },
88
fields: [
99
{
1010
type: 'tabs',
@@ -37,6 +37,10 @@ export const BlocksCollection: CollectionConfig = {
3737
{
3838
slug: 'blockInsideBlock',
3939
fields: [
40+
{
41+
name: 'text',
42+
type: 'text',
43+
},
4044
{
4145
name: 'content',
4246
type: 'blocks',

test/localization/e2e.spec.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import type { GeneratedTypes } from 'helpers/sdk/types.js'
33

44
import { expect, test } from '@playwright/test'
55
import { addArrayRow } from 'helpers/e2e/fields/array/index.js'
6+
import { addBlock } from 'helpers/e2e/fields/blocks/addBlock.js'
67
import { navigateToDoc } from 'helpers/e2e/navigateToDoc.js'
78
import { openDocControls } from 'helpers/e2e/openDocControls.js'
89
import { upsertPreferences } from 'helpers/e2e/preferences.js'
910
import { openDocDrawer } from 'helpers/e2e/toggleDocDrawer.js'
11+
import { waitForAutoSaveToRunAndComplete } from 'helpers/e2e/waitForAutoSaveToRunAndComplete.js'
1012
import { RESTClient } from 'helpers/rest.js'
1113
import path from 'path'
1214
import { fileURLToPath } from 'url'
@@ -28,6 +30,7 @@ import { AdminUrlUtil } from '../helpers/adminUrlUtil.js'
2830
import { initPayloadE2ENoConfig } from '../helpers/initPayloadE2ENoConfig.js'
2931
import { POLL_TOPASS_TIMEOUT, TEST_TIMEOUT_LONG } from '../playwright.config.js'
3032
import { arrayCollectionSlug } from './collections/Array/index.js'
33+
import { blocksCollectionSlug } from './collections/Blocks/index.js'
3134
import { nestedToArrayAndBlockCollectionSlug } from './collections/NestedToArrayAndBlock/index.js'
3235
import { noLocalizedFieldsCollectionSlug } from './collections/NoLocalizedFields/index.js'
3336
import { richTextSlug } from './collections/RichText/index.js'
@@ -63,6 +66,7 @@ let urlPostsWithDrafts: AdminUrlUtil
6366
let urlArray: AdminUrlUtil
6467
let arrayWithFallbackURL: AdminUrlUtil
6568
let noLocalizedFieldsURL: AdminUrlUtil
69+
let urlBlocks: AdminUrlUtil
6670

6771
const title = 'english title'
6872
const spanishTitle = 'spanish title'
@@ -90,6 +94,7 @@ describe('Localization', () => {
9094
urlArray = new AdminUrlUtil(serverURL, arrayCollectionSlug)
9195
arrayWithFallbackURL = new AdminUrlUtil(serverURL, arrayWithFallbackCollectionSlug)
9296
noLocalizedFieldsURL = new AdminUrlUtil(serverURL, noLocalizedFieldsCollectionSlug)
97+
urlBlocks = new AdminUrlUtil(serverURL, blocksCollectionSlug)
9398

9499
context = await browser.newContext()
95100
page = await context.newPage()
@@ -651,6 +656,41 @@ describe('Localization', () => {
651656
})
652657
.toBeTruthy()
653658
})
659+
660+
test('blocks - should show fallback checkbox for non-default locale', async () => {
661+
await page.goto(urlBlocks.create)
662+
await addBlock({ page, blockToSelect: 'Block Inside Block', fieldName: 'content' })
663+
const rowTextInput = page.locator(`#field-content__0__text`)
664+
await rowTextInput.fill('text')
665+
await saveDocAndAssert(page)
666+
await changeLocale(page, 'pt')
667+
const fallbackCheckbox = page.locator('#field-content', {
668+
hasText: 'Fallback to default locale',
669+
})
670+
671+
await expect(fallbackCheckbox).toBeVisible()
672+
})
673+
674+
test('blocks - should successfully save with the fallback', async () => {
675+
await page.goto(urlBlocks.create)
676+
await addBlock({ page, blockToSelect: 'Block Inside Block', fieldName: 'content' })
677+
const rowTextInput = page.locator(`#field-content__0__text`)
678+
await rowTextInput.fill('text')
679+
await saveDocAndAssert(page)
680+
await changeLocale(page, 'pt')
681+
await rowTextInput.fill('changed')
682+
await waitForAutoSaveToRunAndComplete(page)
683+
await saveDocAndAssert(page)
684+
const docID = page.url().split('/').pop()?.split('?').shift()
685+
686+
const doc = await payload.find({
687+
collection: 'blocks-fields',
688+
where: { id: { equals: docID } },
689+
locale: 'all',
690+
})
691+
// eslint-disable-next-line payload/no-flaky-assertions
692+
expect(doc.docs).toHaveLength(1)
693+
})
654694
})
655695

656696
test('should use label in search filter when string or object', async () => {

test/localization/payload-types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export interface RichText {
172172
root: {
173173
type: string;
174174
children: {
175-
type: string;
175+
type: any;
176176
version: number;
177177
[k: string]: unknown;
178178
}[];
@@ -202,6 +202,7 @@ export interface BlocksField {
202202
| null;
203203
content?:
204204
| {
205+
text?: string | null;
205206
content?:
206207
| {
207208
text?: string | null;
@@ -390,6 +391,7 @@ export interface NoLocalizedField {
390391
text?: string | null;
391392
updatedAt: string;
392393
createdAt: string;
394+
_status?: ('draft' | 'published') | null;
393395
}
394396
/**
395397
* This interface was referenced by `Config`'s JSON-Schema
@@ -903,6 +905,7 @@ export interface BlocksFieldsSelect<T extends boolean = true> {
903905
blockInsideBlock?:
904906
| T
905907
| {
908+
text?: T;
906909
content?:
907910
| T
908911
| {
@@ -1086,6 +1089,7 @@ export interface NoLocalizedFieldsSelect<T extends boolean = true> {
10861089
text?: T;
10871090
updatedAt?: T;
10881091
createdAt?: T;
1092+
_status?: T;
10891093
}
10901094
/**
10911095
* This interface was referenced by `Config`'s JSON-Schema

0 commit comments

Comments
 (0)