fix: #1808, arrays and blocks now save localized nested field data upon reordering rows

This commit is contained in:
James
2023-01-09 12:58:13 -05:00
parent 78af86f9d1
commit ee54c1481c
8 changed files with 105 additions and 8 deletions

2
.vscode/launch.json vendored
View File

@@ -39,7 +39,7 @@
"--nolazy" "--nolazy"
], ],
"args": [ "args": [
"fields" "localization"
] ]
}, },
] ]

View File

@@ -5,9 +5,17 @@
* Otherwise, return an empty object. * Otherwise, return an empty object.
*/ */
export const getExistingRowDoc = (incomingRow: Record<string, unknown>, existingRow?: Record<string, unknown>): Record<string, unknown> => { export const getExistingRowDoc = (incomingRow: Record<string, unknown>, existingRows?: unknown): Record<string, unknown> => {
if (incomingRow.id && incomingRow.id === existingRow?.id) { if (incomingRow.id && Array.isArray(existingRows)) {
return existingRow; const matchedExistingRow = existingRows.find((existingRow) => {
if (typeof existingRow === 'object' && 'id' in existingRow) {
if (existingRow.id === incomingRow.id) return existingRow;
}
return false;
});
if (matchedExistingRow) return matchedExistingRow;
} }
return {}; return {};

View File

@@ -223,8 +223,8 @@ export const promise = async ({
path: `${path}${field.name}.${i}.`, path: `${path}${field.name}.${i}.`,
req, req,
siblingData: row, siblingData: row,
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]?.[i]), siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]),
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]?.[i]), siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]),
skipValidation: skipValidationFromHere, skipValidation: skipValidationFromHere,
})); }));
}); });
@@ -257,8 +257,8 @@ export const promise = async ({
path: `${path}${field.name}.${i}.`, path: `${path}${field.name}.${i}.`,
req, req,
siblingData: row, siblingData: row,
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]?.[i]), siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]),
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]?.[i]), siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]),
skipValidation: skipValidationFromHere, skipValidation: skipValidationFromHere,
})); }));
} }

View File

@@ -282,6 +282,17 @@ export interface GroupField {
potentiallyEmptyGroup: { potentiallyEmptyGroup: {
text?: string; text?: string;
}; };
groupInRow: {
field?: string;
secondField?: string;
thirdField?: string;
};
secondGroupInRow: {
field?: string;
nestedGroup: {
nestedField?: string;
};
};
createdAt: string; createdAt: string;
updatedAt: string; updatedAt: string;
} }

View File

@@ -1,5 +1,6 @@
import { buildConfig } from '../buildConfig'; import { buildConfig } from '../buildConfig';
import { devUser } from '../credentials'; import { devUser } from '../credentials';
import { GlobalArray } from './Array';
import { LocalizedPost, RelationshipLocalized } from './payload-types'; import { LocalizedPost, RelationshipLocalized } from './payload-types';
import { import {
defaultLocale, defaultLocale,
@@ -175,6 +176,24 @@ export default buildConfig({
], ],
}, },
], ],
globals: [
{
slug: 'global-array',
fields: [
{
name: 'array',
type: 'array',
fields: [
{
name: 'text',
type: 'text',
localized: true,
},
],
},
],
},
],
onInit: async (payload) => { onInit: async (payload) => {
const collection = slug; const collection = slug;
@@ -266,5 +285,30 @@ export default buildConfig({
], ],
}, },
}); });
const globalArray = await payload.updateGlobal({
slug: 'global-array',
data: {
array: [
{
text: 'test en 1',
},
{
text: 'test en 2',
},
],
},
});
await payload.updateGlobal({
slug: 'global-array',
locale: 'es',
data: {
array: globalArray.array.map((row, i) => ({
...row,
text: `test es ${i + 1}`,
})),
},
});
}, },
}); });

View File

@@ -7,6 +7,7 @@ import type {
WithLocalizedRelationship, WithLocalizedRelationship,
LocalizedRequired, LocalizedRequired,
RelationshipLocalized, RelationshipLocalized,
GlobalArray,
} from './payload-types'; } from './payload-types';
import type { LocalizedPostAllLocale } from './config'; import type { LocalizedPostAllLocale } from './config';
import config, { relationshipLocalizedSlug, slug, withLocalizedRelSlug, withRequiredLocalizedFields } from './config'; import config, { relationshipLocalizedSlug, slug, withLocalizedRelSlug, withRequiredLocalizedFields } from './config';
@@ -477,6 +478,27 @@ describe('Localization', () => {
}); });
}); });
describe('Localized - arrays with nested localized fields', () => {
it('should allow moving rows and retain existing row locale data', async () => {
const globalArray = await payload.findGlobal<GlobalArray>({
slug: 'global-array',
});
const reversedArrayRows = [...globalArray.array].reverse();
const updatedGlobal = await payload.updateGlobal({
slug: 'global-array',
locale: 'all',
data: {
array: reversedArrayRows,
},
});
expect(updatedGlobal.array[0].text.en).toStrictEqual('test en 2');
expect(updatedGlobal.array[0].text.es).toStrictEqual('test es 2');
});
});
describe('Localized - required', () => { describe('Localized - required', () => {
it('should update without passing all required fields', async () => { it('should update without passing all required fields', async () => {
const newDoc = await payload.create({ const newDoc = await payload.create({

View File

@@ -6,6 +6,17 @@
*/ */
export interface Config {} export interface Config {}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "global-array".
*/
export interface GlobalArray {
id: string;
array: {
text?: string;
id?: string;
}[];
}
/** /**
* This interface was referenced by `Config`'s JSON-Schema * This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "users". * via the `definition` "users".

View File

@@ -1,6 +1,7 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es2019", "target": "es2019",
"sourceMap": true,
"module": "commonjs", "module": "commonjs",
"allowJs": true, /* Allow javascript files to be compiled. */ "allowJs": true, /* Allow javascript files to be compiled. */
"checkJs": false, /* Report errors in .js files. */ "checkJs": false, /* Report errors in .js files. */