2019-11-07 14:06:50 +01:00
|
|
|
import { get } from 'lodash';
|
|
|
|
|
|
|
|
|
|
const createDefaultForm = (attributes, allComponentsSchema) => {
|
|
|
|
|
return Object.keys(attributes).reduce((acc, current) => {
|
|
|
|
|
const attribute = get(attributes, [current], {});
|
|
|
|
|
const {
|
|
|
|
|
default: defaultValue,
|
|
|
|
|
component,
|
|
|
|
|
type,
|
|
|
|
|
required,
|
|
|
|
|
min,
|
|
|
|
|
repeatable,
|
|
|
|
|
} = attribute;
|
|
|
|
|
|
|
|
|
|
if (type === 'json') {
|
|
|
|
|
acc[current] = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'json' && required === true) {
|
|
|
|
|
acc[current] = {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (defaultValue !== undefined) {
|
|
|
|
|
acc[current] = defaultValue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === 'component') {
|
|
|
|
|
const currentComponentSchema = get(
|
|
|
|
|
allComponentsSchema,
|
|
|
|
|
[component, 'schema', 'attributes'],
|
|
|
|
|
{}
|
|
|
|
|
);
|
|
|
|
|
const currentComponentDefaultForm = createDefaultForm(
|
|
|
|
|
currentComponentSchema,
|
|
|
|
|
allComponentsSchema
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (required === true) {
|
|
|
|
|
acc[current] = repeatable === true ? [] : {};
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 15:54:31 +01:00
|
|
|
if (min && repeatable === true && required) {
|
2019-11-07 14:06:50 +01:00
|
|
|
acc[current] = [];
|
|
|
|
|
|
2020-01-21 16:03:31 +01:00
|
|
|
for (let i = 0; i < min; i += 1) {
|
2019-11-07 14:06:50 +01:00
|
|
|
acc[current].push(currentComponentDefaultForm);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-20 17:33:49 +01:00
|
|
|
if (type === 'dynamiczone') {
|
|
|
|
|
if (required === true) {
|
|
|
|
|
acc[current] = [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-07 14:06:50 +01:00
|
|
|
return acc;
|
|
|
|
|
}, {});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default createDefaultForm;
|