From 3de27cb6ec6f67a7cca9b30b4f8b875b07051ba8 Mon Sep 17 00:00:00 2001 From: Tim Leslie Date: Thu, 29 Nov 2018 17:14:16 +1100 Subject: [PATCH] WIP --- packages/core/List/index.js | 124 ++++++++++++++---------------------- 1 file changed, 48 insertions(+), 76 deletions(-) diff --git a/packages/core/List/index.js b/packages/core/List/index.js index 1c8f8319fcc..35c106e272f 100644 --- a/packages/core/List/index.js +++ b/packages/core/List/index.js @@ -742,23 +742,28 @@ module.exports = class List { }); } + async _mapToFields(fields, action) { + return await resolveAllKeys(arrayToObject(fields, 'path', action)); + } + + _fieldsFromObject(obj) { + return Object.keys(obj).map(fieldPath => this.fieldsByPath[fieldPath]); + } + async _resolveRelationship(data, existingItem, context, getItem) { - return await resolveAllKeys( - mapKeys(data, (value, fieldPath) => - this.fieldsByPath[fieldPath].isRelationship - ? this.fieldsByPath[fieldPath].resolveRelationship(value, existingItem, context, getItem) - : value - ) - ); + const fields = this._fieldsFromObject(data).filter(field => field.isRelationship); + return { + ...data, + ...(await this._mapToFields(fields, async field => + field.resolveRelationship(data[field.path], existingItem, context, getItem) + )), + }; } async _resolveDefaults(data) { + const fields = this.fields.filter(field => field.getDefaultValue() !== undefined); return { - ...arrayToObject( - this.fields.filter(field => field.getDefaultValue() !== undefined), - 'path', - field => field.getDefaultValue() - ), + ...(await this._mapToFields(fields, field => field.getDefaultValue())), ...data, }; } @@ -771,23 +776,20 @@ module.exports = class List { adapter: this.adapter, originalInput, }; + const fields = this._fieldsFromObject(resolvedData); // Built in field hooks - resolvedData = await resolveAllKeys( - mapKeys(resolvedData, (value, fieldPath) => - this.fieldsByPath[fieldPath].resolveInput({ resolvedData, ...resolveArgs }) - ) + resolvedData = await this._mapToFields(fields, field => + field.resolveInput({ resolvedData, ...resolveArgs }) ); // Custom field hooks - resolvedData = await resolveAllKeys( - mapKeys(resolvedData, (value, fieldPath) => { - const field = this.fieldsByPath[fieldPath]; - if (field.config.hooks.resolveInput) { - return field.config.hooks.resolveInput({ resolvedData, ...resolveArgs }); - } else return value; - }) - ); + resolvedData = { + ...resolvedData, + ...(await this._mapToFields(fields.filter(field => field.config.hooks.resolveInput), field => + field.config.hooks.resolveInput({ resolvedData, ...resolveArgs }) + )), + }; // Custom list hooks if (this.config.hooks.resolveInput) { @@ -806,29 +808,16 @@ module.exports = class List { originalInput, }; + const fields = this._fieldsFromObject(resolvedData); + // fields const fieldValidationErrors = []; - const addFieldValidationError = (msg, _data = {}, internalData = {}) => + validationArgs.addFieldValidationError = (msg, _data = {}, internalData = {}) => fieldValidationErrors.push({ msg, data: _data, internalData }); - await Promise.all( - Object.keys(resolvedData).map(async fieldPath => { - const field = this.fieldsByPath[fieldPath]; - if (field) { - // Built in field hooks - await field.validateInput({ - ...validationArgs, - addValidationError: addFieldValidationError, - }); - // Custom field hooks - if (field.config.hooks.validateInput) { - await field.config.hooks.validateInput({ - ...validationArgs, - addValidationError: addFieldValidationError, - }); - } - } - }) + await this._mapToFields(fields, field => field.validateInput(validationArgs)); + await this._mapToFields(fields.filter(field => field.config.hooks.validateInput), field => + field.config.hooks.validateInput.validateInput(validationArgs) ); if (fieldValidationErrors.length) { this._throwValidationFailure(fieldValidationErrors, operation, originalInput); @@ -855,30 +844,16 @@ module.exports = class List { context, adapter: this.adapter, }; - const fieldValidationErrors = []; - const addFieldValidationError = (msg, _data = {}, internalData = {}) => + const fields = this.fields; + validationArgs.addValidationError = (msg, _data = {}, internalData = {}) => fieldValidationErrors.push({ msg, data: _data, internalData }); - await Promise.all( - Object.keys(existingItem).map(async fieldPath => { - const field = this.fieldsByPath[fieldPath]; - if (field) { - // Built in field hooks - await field.validateDelete({ - ...validationArgs, - addValidationError: addFieldValidationError, - }); - // Custom field hooks - if (field.config.hooks.validateInput) { - await field.config.hooks.validateDelete({ - ...validationArgs, - addValidationError: addFieldValidationError, - }); - } - } - }) + await this._mapToFields(fields, field => field.validateDelete(validationArgs)); + await this._mapToFields(fields.filter(field => field.config.hooks.validateDelete), field => + field.config.hooks.validateInput.validateDelete(validationArgs) ); + if (fieldValidationErrors.length) { this._throwValidationFailure(fieldValidationErrors, operation); } @@ -900,20 +875,11 @@ module.exports = class List { async _beforeChange(resolvedData, existingItem, context, originalInput) { const args = { resolvedData, existingItem, context, adapter: this.adapter, originalInput }; + const fields = this._fieldsFromObject(resolvedData); - await Promise.all( - Object.keys(resolvedData).map(async fieldPath => { - const field = this.fieldsByPath[fieldPath]; - if (field) { - // Built in field hooks - await field.beforeChange(args); - - // Custom field hooks - if (field.config.hooks.beforeChange) { - await field.config.hooks.beforeChange(args); - } - } - }) + await this._mapToFields(fields, field => field.beforeChange(args)); + await this._mapToFields(fields.filter(field => field.config.hooks.beforeChange), field => + field.config.hooks.beforeChange(args) ); // Custom list hooks @@ -924,6 +890,12 @@ module.exports = class List { async _beforeDelete(existingItem, context) { const args = { existingItem, context, adapter: this.adapter }; + const fields = this._fieldsFromObject(existingItem); + + // await this._mapToFields(fields, field => field.beforeDelete(args)); + // await this._mapToFields(fields.filter(field => field.config.hooks.beforeDelete), field => + // field.config.hooks.beforeDelete(args) + // ); await Promise.all( Object.keys(existingItem).map(async fieldPath => {