2020-11-03 16:02:12 +01:00
|
|
|
import { get } from 'lodash';
|
|
|
|
|
import { getType, getOtherInfos } from './getAttributeInfos';
|
|
|
|
|
|
|
|
|
|
const defaultFields = [
|
|
|
|
|
'created_at',
|
|
|
|
|
'createdAt',
|
|
|
|
|
'created_by',
|
|
|
|
|
'createdBy',
|
|
|
|
|
'updated_at',
|
|
|
|
|
'updatedAt',
|
|
|
|
|
'updated_by',
|
|
|
|
|
'updatedBy',
|
|
|
|
|
'id',
|
|
|
|
|
'_id',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const removeFieldsFromClonedData = (
|
|
|
|
|
data,
|
|
|
|
|
contentTypeSchema,
|
|
|
|
|
componentSchema,
|
|
|
|
|
fields = defaultFields
|
|
|
|
|
) => {
|
|
|
|
|
const recursiveCleanData = (data, schema) => {
|
|
|
|
|
return Object.keys(data).reduce((acc, current) => {
|
|
|
|
|
const attrType = getType(schema.schema, current);
|
|
|
|
|
const value = get(data, current);
|
|
|
|
|
const component = getOtherInfos(schema.schema, [current, 'component']);
|
|
|
|
|
const isRepeatable = getOtherInfos(schema.schema, [current, 'repeatable']);
|
|
|
|
|
|
|
|
|
|
if (fields.indexOf(current) !== -1) {
|
|
|
|
|
delete acc[current];
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 12:34:06 +01:00
|
|
|
if (!value) {
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-03 16:02:12 +01:00
|
|
|
if (attrType === 'dynamiczone') {
|
|
|
|
|
acc[current] = value.map(componentValue => {
|
|
|
|
|
const subCleanedData = recursiveCleanData(
|
|
|
|
|
componentValue,
|
|
|
|
|
componentSchema[componentValue.__component]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return subCleanedData;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (attrType === 'component') {
|
|
|
|
|
if (isRepeatable) {
|
2020-11-10 12:34:06 +01:00
|
|
|
acc[current] = value.map(compoData => {
|
|
|
|
|
const subCleanedData = recursiveCleanData(compoData, componentSchema[component]);
|
2020-11-03 16:02:12 +01:00
|
|
|
|
2020-11-10 12:34:06 +01:00
|
|
|
return subCleanedData;
|
|
|
|
|
});
|
2020-11-03 16:02:12 +01:00
|
|
|
} else {
|
2020-11-10 12:34:06 +01:00
|
|
|
acc[current] = recursiveCleanData(value, componentSchema[component]);
|
2020-11-03 16:02:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-10 12:34:06 +01:00
|
|
|
return Object.keys(data).reduce(acc => acc, data);
|
2020-11-03 16:02:12 +01:00
|
|
|
}, {});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return recursiveCleanData(data, contentTypeSchema);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default removeFieldsFromClonedData;
|