fix: #1808, arrays and blocks now save localized nested field data upon reordering rows
This commit is contained in:
2
.vscode/launch.json
vendored
2
.vscode/launch.json
vendored
@@ -39,7 +39,7 @@
|
||||
"--nolazy"
|
||||
],
|
||||
"args": [
|
||||
"fields"
|
||||
"localization"
|
||||
]
|
||||
},
|
||||
]
|
||||
|
||||
@@ -5,9 +5,17 @@
|
||||
* Otherwise, return an empty object.
|
||||
*/
|
||||
|
||||
export const getExistingRowDoc = (incomingRow: Record<string, unknown>, existingRow?: Record<string, unknown>): Record<string, unknown> => {
|
||||
if (incomingRow.id && incomingRow.id === existingRow?.id) {
|
||||
return existingRow;
|
||||
export const getExistingRowDoc = (incomingRow: Record<string, unknown>, existingRows?: unknown): Record<string, unknown> => {
|
||||
if (incomingRow.id && Array.isArray(existingRows)) {
|
||||
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 {};
|
||||
|
||||
@@ -223,8 +223,8 @@ export const promise = async ({
|
||||
path: `${path}${field.name}.${i}.`,
|
||||
req,
|
||||
siblingData: row,
|
||||
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]?.[i]),
|
||||
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]?.[i]),
|
||||
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]),
|
||||
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]),
|
||||
skipValidation: skipValidationFromHere,
|
||||
}));
|
||||
});
|
||||
@@ -257,8 +257,8 @@ export const promise = async ({
|
||||
path: `${path}${field.name}.${i}.`,
|
||||
req,
|
||||
siblingData: row,
|
||||
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]?.[i]),
|
||||
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]?.[i]),
|
||||
siblingDoc: getExistingRowDoc(row, siblingDoc[field.name]),
|
||||
siblingDocWithLocales: getExistingRowDoc(row, siblingDocWithLocales[field.name]),
|
||||
skipValidation: skipValidationFromHere,
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -282,6 +282,17 @@ export interface GroupField {
|
||||
potentiallyEmptyGroup: {
|
||||
text?: string;
|
||||
};
|
||||
groupInRow: {
|
||||
field?: string;
|
||||
secondField?: string;
|
||||
thirdField?: string;
|
||||
};
|
||||
secondGroupInRow: {
|
||||
field?: string;
|
||||
nestedGroup: {
|
||||
nestedField?: string;
|
||||
};
|
||||
};
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { buildConfig } from '../buildConfig';
|
||||
import { devUser } from '../credentials';
|
||||
import { GlobalArray } from './Array';
|
||||
import { LocalizedPost, RelationshipLocalized } from './payload-types';
|
||||
import {
|
||||
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) => {
|
||||
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}`,
|
||||
})),
|
||||
},
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
WithLocalizedRelationship,
|
||||
LocalizedRequired,
|
||||
RelationshipLocalized,
|
||||
GlobalArray,
|
||||
} from './payload-types';
|
||||
import type { LocalizedPostAllLocale } 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', () => {
|
||||
it('should update without passing all required fields', async () => {
|
||||
const newDoc = await payload.create({
|
||||
|
||||
@@ -6,6 +6,17 @@
|
||||
*/
|
||||
|
||||
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
|
||||
* via the `definition` "users".
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2019",
|
||||
"sourceMap": true,
|
||||
"module": "commonjs",
|
||||
"allowJs": true, /* Allow javascript files to be compiled. */
|
||||
"checkJs": false, /* Report errors in .js files. */
|
||||
|
||||
Reference in New Issue
Block a user