Merge branch 'ErikDakoda-SubSchemaFields' into devel

This commit is contained in:
SachaG 2018-05-24 11:30:23 +09:00
commit cbdafef7a2
4 changed files with 55 additions and 13 deletions

View file

@ -44,6 +44,7 @@ import compact from 'lodash/compact';
import update from 'lodash/update';
import merge from 'lodash/merge';
import find from 'lodash/find';
import pick from 'lodash/pick';
import isEqualWith from 'lodash/isEqualWith';
import { convertSchema, formProperties } from '../modules/schema_utils';
@ -158,7 +159,7 @@ class Form extends Component {
// only keep relevant fields
// for intl fields, make sure we look in foo_intl and not foo
const fields = this.getFieldNames({ excludeHiddenFields: false, replaceIntlFields: true });
let data = cloneDeep(_.pick(this.getDocument(), ...fields));
let data = pick(this.getDocument(), ...fields);
// remove any deleted values
// (deleted nested fields cannot be added to $unset, instead we need to modify their value directly)
@ -724,6 +725,8 @@ class Form extends Component {
// only keep unset keys that correspond to a field (get rid of nested keys)
unsetKeys = _.intersection(unsetKeys, this.getFieldNames());
unsetKeys = unsetKeys.filter(key => !key.includes('.'));
// build mutation arguments object
const args = { documentId: document._id, set: set, unset: {} };
if (unsetKeys.length > 0) {

View file

@ -32,7 +32,7 @@ class FormComponent extends Component {
const { currentValues, deletedValues, errors } = nextProps;
const { path } = this.props;
const valueChanged = currentValues[path] !== this.props.currentValues[path];
const valueChanged = get(currentValues, path) !== get(this.props.currentValues, path);
const errorChanged = !isEqual(this.getErrors(errors), this.getErrors());
const deleteChanged = deletedValues.includes(path) !== this.props.deletedValues.includes(path);
const charsChanged = nextState.charsRemaining !== this.state.charsRemaining;
@ -124,7 +124,7 @@ class FormComponent extends Component {
// for intl field fetch the actual field value by adding .value to the path
const path = p.locale ? `${this.getPath(p)}.value` : this.getPath(p);
const documentValue = get(document, path);
const currentValue = currentValues[path];
const currentValue = get(currentValues, path);
const isDeleted = p.deletedValues.includes(path);
if (isDeleted) {

View file

@ -1,3 +1,6 @@
import SimpleSchema from 'simpl-schema';
/*
Convert a nested SimpleSchema schema into a JSON object
@ -18,16 +21,25 @@ export const convertSchema = (schema, flatten = false) => {
jsonSchema[fieldName] = getFieldSchema(fieldName, schema);
// check for existence of nested schema on corresponding array field
const subSchema = getNestedSchema(fieldName, schema);
const arraySubSchema = getArraySubSchema(fieldName, schema);
// if nested schema exists, call convertSchema recursively
if (subSchema) {
const convertedSubSchema = convertSchema(subSchema);
if (arraySubSchema) {
const convertedArraySubSchema = convertSchema(arraySubSchema);
if (flatten) {
jsonSchema = { ...jsonSchema, ...convertedSubSchema };
jsonSchema = { ...jsonSchema, ...convertedArraySubSchema };
} else {
jsonSchema[fieldName].schema = convertedSubSchema;
jsonSchema[fieldName].schema = convertedArraySubSchema;
}
}
// check if the type of this field is a nested schema
const objectSubSchema = getObjectSubSchema(fieldName, schema);
if (objectSubSchema) {
let convertedObjectSubSchema = convertSchema(objectSubSchema);
const mergedSubSchema = mergeSubSchemaWithParent(convertedObjectSubSchema, jsonSchema, fieldName);
jsonSchema = { ...jsonSchema, ...mergedSubSchema };
delete jsonSchema[fieldName];
}
});
return jsonSchema;
} else {
@ -56,10 +68,36 @@ export const getFieldSchema = (fieldName, schema) => {
Given an array field, get its nested schema
*/
export const getNestedSchema = (fieldName, schema) => {
export const getArraySubSchema = (fieldName, schema) => {
const arrayItemSchema = schema._schema[`${fieldName}.$`];
const nestedSchema = arrayItemSchema && arrayItemSchema.type.definitions[0].type;
return nestedSchema;
return arrayItemSchema && arrayItemSchema.type.definitions[0].type;
};
/*
Given an object field, get its sub schema
*/
export const getObjectSubSchema = (fieldName, schema) => {
const objectItemSchema = schema._schema[fieldName].type.definitions[0].type;
if (SimpleSchema.isSimpleSchema(objectItemSchema)) {
return objectItemSchema;
}
};
/*
Given a subschema, prefixes the field names with the parent's, and merges the parent's values
into each child (this way field properties like 'editableBy' and 'group' can be set on the parent
for all children, and can still be overridden by children.
*/
export const mergeSubSchemaWithParent = (schema, jsonSchema, parentName) => {
const merged = {};
for (const key in schema) {
merged[`${parentName}.${key}`] = Object.assign({}, jsonSchema[parentName], schema[key]);
}
return merged;
};
export const schemaProperties = [

View file

@ -331,9 +331,10 @@ export const createCollection = options => {
}
}
// limit number of items to 200 by default
// limit number of items to 1000 by default
const maxDocuments = getSetting('maxDocumentsPerRequest', 1000);
parameters.options.limit = (!terms.limit || terms.limit < 1 || terms.limit > maxDocuments) ? maxDocuments : terms.limit;
const limit = terms.limit || parameters.options.limit;
parameters.options.limit = (!limit || limit < 1 || limit > maxDocuments) ? maxDocuments : limit;
// console.log(parameters);