Handle added upload relations

This commit is contained in:
soupette 2018-07-31 14:54:52 +02:00
parent 2d4dded100
commit 043838ba3f
3 changed files with 30 additions and 2 deletions

View File

@ -615,7 +615,7 @@ class SettingPage extends React.PureComponent {
const {
onSubmit,
} = this.props;
// console.log(this.props.schema);
console.log(this.props.schema);
// TODO: Add loader!
return (

View File

@ -229,12 +229,21 @@ module.exports = async cb => {
return acc;
}, []);
const getApisKeys = (data, sameArray) => sameArray.map(apiPath => {
const fields = Object.keys(_.get(data.models, apiPath.concat(['fields'])));
return fields.map(field => `${apiPath.join('.')}.fields.${field}`);
});
const getApisUploadRelations = (data, sameArray) => sameArray.map(apiPath => {
const relationPath = [...apiPath, 'relations'];
const relationsObject = _.get(data.models, relationPath, {});
const relations = Object.keys(relationsObject)
.filter(relationName => {
return _.get(data.models, [...relationPath, relationName, 'plugin' ]) === 'upload';
});
return relations.map(relation => `${apiPath.join('.')}.editDisplay.availableFields.${relation}`);
});
const getEditDisplayAvailableFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'availableFields', attrPath[attrPath.length - 1]];
const getEditDisplayFieldsPath = attrPath => [..._.take(attrPath, attrPath.length -2), 'editDisplay', 'fields'];
@ -259,6 +268,9 @@ module.exports = async cb => {
const schemaSameApisKeys = _.flattenDeep(getApisKeys(schema, sameApis));
const prevSchemaSameApisKeys = _.flattenDeep(getApisKeys(prevSchema, sameApis));
const sameApisAttrToAdd = schemaSameApisKeys.filter(attr => prevSchemaSameApisKeys.indexOf(attr) === -1).map(splitted);
const prevSchemaSameApisUploadRelations = _.flattenDeep(getApisUploadRelations(prevSchema, sameApis));
const schemaSameApisUploadRelations = _.flattenDeep(getApisUploadRelations(schema, sameApis));
const sameApisUploadRelationsToAdd = schemaSameApisUploadRelations.filter(attr => prevSchemaSameApisUploadRelations.indexOf(attr) === -1).map(splitted);
const sameApisAttrToRemove = prevSchemaSameApisKeys.filter(attr => schemaSameApisKeys.indexOf(attr) === -1).map(splitted);
// Remove api
@ -337,6 +349,17 @@ module.exports = async cb => {
});
});
// Special handler for the upload relations
sameApisUploadRelationsToAdd.forEach(attrPath => {
const attr = _.get(schema.models, attrPath);
_.set(prevSchema.models, attrPath, attr);
const fieldsPath = [..._.take(attrPath, attrPath.length -2), 'fields'];
const currentFields = _.get(prevSchema.models, fieldsPath, []);
currentFields.push(attr.name);
_.set(prevSchema.models, fieldsPath, currentFields);
});
await pluginStore.set({ key: 'schema', value: prevSchema });
} catch(err) {
console.log('error', err);

View File

@ -117,6 +117,11 @@ module.exports = {
});
});
// Delete them from the available fields
fieldsToRemove.forEach(field => {
_.unset(schema, [...schemaPath, 'editDisplay', 'availableFields', field]);
});
_.set(schema, [...schemaPath, 'editDisplay', 'fields'], newList.toJS());
}