2020-10-30 15:25:57 +01:00
|
|
|
// NOTE: this function is for adding a __temp_key__ key to each object of a repeatable component
|
|
|
|
// in order to have a unique identifier for the DnD
|
|
|
|
|
|
|
|
import { get } from 'lodash';
|
2020-11-02 09:44:04 +01:00
|
|
|
import { getType, getOtherInfos } from './getAttributeInfos';
|
2020-10-30 15:25:57 +01:00
|
|
|
|
|
|
|
const formatComponentData = (data, ct, composSchema) => {
|
|
|
|
const recursiveFormatData = (data, schema) => {
|
|
|
|
return Object.keys(data).reduce((acc, current) => {
|
2020-11-06 15:51:29 +01:00
|
|
|
const type = getType(schema, current);
|
2020-10-30 15:25:57 +01:00
|
|
|
const value = get(data, current);
|
2020-11-06 15:51:29 +01:00
|
|
|
const compoUid = getOtherInfos(schema, [current, 'component']);
|
|
|
|
const isRepeatable = getOtherInfos(schema, [current, 'repeatable']);
|
2020-10-30 15:25:57 +01:00
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
acc[current] = value;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'dynamiczone') {
|
|
|
|
acc[current] = value.map(componentValue => {
|
|
|
|
const formattedData = recursiveFormatData(
|
|
|
|
componentValue,
|
|
|
|
composSchema[componentValue.__component]
|
|
|
|
);
|
|
|
|
|
|
|
|
return formattedData;
|
|
|
|
});
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'component') {
|
|
|
|
let formattedValue;
|
|
|
|
|
|
|
|
if (isRepeatable) {
|
|
|
|
formattedValue = value.map((obj, i) => {
|
|
|
|
const newObj = { ...obj, __temp_key__: i };
|
|
|
|
|
|
|
|
return recursiveFormatData(newObj, composSchema[compoUid]);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
formattedValue = recursiveFormatData(value, composSchema[compoUid]);
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[current] = formattedValue;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}
|
|
|
|
|
|
|
|
acc[current] = value;
|
|
|
|
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
|
|
|
return recursiveFormatData(data, ct);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default formatComponentData;
|