fix: preserve unknown fields value (#3314)

This commit is contained in:
Bartholomew 2020-02-26 16:58:46 +01:00 committed by GitHub
parent dd94a8426e
commit 908c42fb58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 9 deletions

View File

@ -203,7 +203,7 @@ describe('Editor', () => {
<Editor
{...props}
entryDraft={fromJS({ entry: { mediaFiles: [{ id: '1' }] } })}
entry={fromJS({ isFetching: false })}
entry={fromJS({ isFetching: false, data: {} })}
hasChanged={true}
/>,
);
@ -232,7 +232,7 @@ describe('Editor', () => {
entry: {},
fieldsMetaData: {},
})}
entry={fromJS({ isFetching: false, mediaFiles: [{ id: '1' }] })}
entry={fromJS({ isFetching: false, mediaFiles: [{ id: '1' }], data: {} })}
/>,
);

View File

@ -1,12 +1,7 @@
export const sortKeys = (sortedKeys = []) => (a, b) => {
const idxA = sortedKeys.indexOf(a);
const idxB = sortedKeys.indexOf(b);
if (idxA === -1 || idxB === -1) {
if (a > b) return 1;
if (a < b) return -1;
return 0;
}
if (idxA === -1 || idxB === -1) return 0;
if (idxA > idxB) return 1;
if (idxA < idxB) return -1;
return 0;

View File

@ -0,0 +1,21 @@
import { serializeValues, deserializeValues } from '../serializeEntryValues';
import { fromJS } from 'immutable';
const values = fromJS({ title: 'New Post', unknown: 'Unknown Field' });
const fields = fromJS([{ name: 'title', widget: 'string' }]);
describe('serializeValues', () => {
it('should retain unknown fields', () => {
expect(serializeValues(values, fields)).toEqual(
fromJS({ title: 'New Post', unknown: 'Unknown Field' }),
);
});
});
describe('deserializeValues', () => {
it('should retain unknown fields', () => {
expect(deserializeValues(values, fields)).toEqual(
fromJS({ title: 'New Post', unknown: 'Unknown Field' }),
);
});
});

View File

@ -27,7 +27,7 @@ const runSerializer = (values, fields, method) => {
* registered serializers. If the field is a list or object, call recursively
* for nested fields.
*/
return fields.reduce((acc, field) => {
let serializedData = fields.reduce((acc, field) => {
const fieldName = field.get('name');
const value = values.get(fieldName);
const serializer = getWidgetValueSerializer(field.get('widget'));
@ -58,6 +58,11 @@ const runSerializer = (values, fields, method) => {
return acc;
}, Map());
//preserve unknown fields value
serializedData = values.mergeDeep(serializedData);
return serializedData;
};
export const serializeValues = (values, fields) => {