Skip to content

Commit

Permalink
fix(ui): error in version view if document contains localized arrays …
Browse files Browse the repository at this point in the history
…or blocks (#10893)

Fixes #10884
  • Loading branch information
AlessioGr authored Jan 30, 2025
1 parent 2b9ee62 commit 85c0842
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ export const RenderVersionFieldsToDiff = ({
const LocaleComponents: React.ReactNode[] = []
for (const [locale, baseField] of Object.entries(field.fieldByLocale)) {
LocaleComponents.push(
<div className={`${baseClass}__locale`} key={[locale, fieldIndex].join('-')}>
<div
className={`${baseClass}__locale`}
data-field-path={baseField.path}
data-locale={locale}
key={[locale, fieldIndex].join('-')}
>
<div className={`${baseClass}__locale-value`}>{baseField.CustomComponent}</div>
</div>,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,15 +282,13 @@ const buildVersionField = ({
}
} // At this point, we are dealing with a `row`, etc
else if ('fields' in field) {
if (field.type === 'array') {
if (!Array.isArray(versionValue)) {
throw new Error('Expected versionValue to be an array')
}
if (field.type === 'array' && versionValue) {
const arrayValue = Array.isArray(versionValue) ? versionValue : []
baseVersionField.rows = []

for (let i = 0; i < versionValue.length; i++) {
for (let i = 0; i < arrayValue.length; i++) {
const comparisonRow = comparisonValue?.[i] || {}
const versionRow = versionValue?.[i] || {}
const versionRow = arrayValue?.[i] || {}
baseVersionField.rows[i] = buildVersionFields({
clientSchemaMap,
comparisonSiblingData: comparisonRow,
Expand Down Expand Up @@ -329,13 +327,11 @@ const buildVersionField = ({
} else if (field.type === 'blocks') {
baseVersionField.rows = []

if (!Array.isArray(versionValue)) {
throw new Error('Expected versionValue to be an array')
}
const blocksValue = Array.isArray(versionValue) ? versionValue : []

for (let i = 0; i < versionValue.length; i++) {
for (let i = 0; i < blocksValue.length; i++) {
const comparisonRow = comparisonValue?.[i] || {}
const versionRow = versionValue[i] || {}
const versionRow = blocksValue[i] || {}
const versionBlock = field.blocks.find((block) => block.slug === versionRow.blockType)

let fields = []
Expand Down
11 changes: 11 additions & 0 deletions test/versions/collections/Diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ export const Diff: CollectionConfig = {
},
],
},
{
name: 'arrayLocalized',
type: 'array',
localized: true,
fields: [
{
name: 'textInArrayLocalized',
type: 'text',
},
],
},
{
name: 'blocks',
type: 'blocks',
Expand Down
15 changes: 15 additions & 0 deletions test/versions/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,21 @@ describe('Versions', () => {
await expect(textInArray.locator('tr').nth(1).locator('td').nth(3)).toHaveText('textInArray2')
})

test('correctly renders diff for localized array fields', async () => {
await navigateToVersionFieldsDiff()

const textInArray = page
.locator('[data-field-path="arrayLocalized"][data-locale="en"]')
.locator('[data-field-path="arrayLocalized.0.textInArrayLocalized"]')

await expect(textInArray.locator('tr').nth(1).locator('td').nth(1)).toHaveText(
'textInArrayLocalized',
)
await expect(textInArray.locator('tr').nth(1).locator('td').nth(3)).toHaveText(
'textInArrayLocalized2',
)
})

test('correctly renders diff for block fields', async () => {
await navigateToVersionFieldsDiff()

Expand Down
12 changes: 12 additions & 0 deletions test/versions/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,12 @@ export interface Diff {
id?: string | null;
}[]
| null;
arrayLocalized?:
| {
textInArrayLocalized?: string | null;
id?: string | null;
}[]
| null;
blocks?:
| {
textInBlock?: string | null;
Expand Down Expand Up @@ -641,6 +647,12 @@ export interface DiffSelect<T extends boolean = true> {
textInArray?: T;
id?: T;
};
arrayLocalized?:
| T
| {
textInArrayLocalized?: T;
id?: T;
};
blocks?:
| T
| {
Expand Down
12 changes: 12 additions & 0 deletions test/versions/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,18 @@ export async function seed(_payload: Payload, parallel: boolean = false) {

const diffDoc = await _payload.create({
collection: diffCollectionSlug,
locale: 'en',
data: {
array: [
{
textInArray: 'textInArray',
},
],
arrayLocalized: [
{
textInArrayLocalized: 'textInArrayLocalized',
},
],
blocks: [
{
blockType: 'TextBlock',
Expand Down Expand Up @@ -164,12 +170,18 @@ export async function seed(_payload: Payload, parallel: boolean = false) {
const updatedDiffDoc = await _payload.update({
id: diffDoc.id,
collection: diffCollectionSlug,
locale: 'en',
data: {
array: [
{
textInArray: 'textInArray2',
},
],
arrayLocalized: [
{
textInArrayLocalized: 'textInArrayLocalized2',
},
],
blocks: [
{
blockType: 'TextBlock',
Expand Down

0 comments on commit 85c0842

Please sign in to comment.