2020-11-03 16:02:12 +01:00
|
|
|
import { get } from 'lodash';
|
|
|
|
|
import { getType, getOtherInfos } from './getAttributeInfos';
|
|
|
|
|
|
2020-11-19 16:59:47 +01:00
|
|
|
const defaultFields = ['created_by', 'updated_by', 'published_at', 'id', '_id'];
|
2020-11-03 16:02:12 +01:00
|
|
|
|
|
|
|
|
const removeFieldsFromClonedData = (
|
|
|
|
|
data,
|
|
|
|
|
contentTypeSchema,
|
|
|
|
|
componentSchema,
|
|
|
|
|
fields = defaultFields
|
|
|
|
|
) => {
|
|
|
|
|
const recursiveCleanData = (data, schema) => {
|
|
|
|
|
return Object.keys(data).reduce((acc, current) => {
|
2020-11-19 18:04:02 +01:00
|
|
|
const attrType = getType(schema, current);
|
2020-11-03 16:02:12 +01:00
|
|
|
const value = get(data, current);
|
2020-11-19 18:04:02 +01:00
|
|
|
const component = getOtherInfos(schema, [current, 'component']);
|
|
|
|
|
const isRepeatable = getOtherInfos(schema, [current, 'repeatable']);
|
|
|
|
|
let timestamps = get(schema, ['options', 'timestamps']);
|
2020-11-03 16:02:12 +01:00
|
|
|
|
2020-11-19 16:59:47 +01:00
|
|
|
if (!Array.isArray(timestamps)) {
|
|
|
|
|
timestamps = [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ([...fields, ...timestamps].indexOf(current) !== -1) {
|
2020-11-03 16:02:12 +01:00
|
|
|
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-19 15:12:34 +01:00
|
|
|
return acc;
|
|
|
|
|
}, Object.assign({}, data));
|
2020-11-03 16:02:12 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return recursiveCleanData(data, contentTypeSchema);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default removeFieldsFromClonedData;
|