Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Nov 29, 2018
1 parent 378b82d commit 3de27cb
Showing 1 changed file with 48 additions and 76 deletions.
124 changes: 48 additions & 76 deletions packages/core/List/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}
Expand All @@ -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) {
Expand All @@ -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);
Expand All @@ -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);
}
Expand All @@ -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
Expand All @@ -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 => {
Expand Down

0 comments on commit 3de27cb

Please sign in to comment.