From 45c53b1a259880e8cd105c82821fce5e6aecf57a Mon Sep 17 00:00:00 2001 From: John Dunning Date: Thu, 21 Jul 2022 13:26:31 -0700 Subject: [PATCH] Refactor ModelMetadata Use getFieldHash() for getting the prop types as well, passing in convertToPropType. --- src/Models/Ringdown.js | 3 +- src/metadata/ModelMetadata.js | 47 +++++-------------------------- src/metadata/convertToPropType.js | 32 +++++++++++++++++++++ 3 files changed, 41 insertions(+), 41 deletions(-) create mode 100644 src/metadata/convertToPropType.js diff --git a/src/Models/Ringdown.js b/src/Models/Ringdown.js index 21e85b85..ab7ece21 100644 --- a/src/Models/Ringdown.js +++ b/src/Models/Ringdown.js @@ -2,6 +2,7 @@ import { DateTime } from 'luxon'; import PropTypes from 'prop-types'; import { PatientFieldData, ValidationState } from './PatientFieldData'; import patient from '../metadata/patient'; +import convertToPropType from '../metadata/convertToPropType'; import DeliveryStatus from '../constants/DeliveryStatus'; function attachFields(target, fields, data) { @@ -288,7 +289,7 @@ Ringdown.propTypes = { dispatchCallNumber: PropTypes.number.isRequired, hospitalId: PropTypes.string.isRequired, // Patient Info - ...patient.getPropTypes(PropTypes), + ...patient.getFieldHash(convertToPropType), // Status etaMinutes: PropTypes.number.isRequired, currentDeliveryStatus: PropTypes.oneOf(DeliveryStatus.ALL_STATUSES), diff --git a/src/metadata/ModelMetadata.js b/src/metadata/ModelMetadata.js index bdf11cf4..d1a002da 100644 --- a/src/metadata/ModelMetadata.js +++ b/src/metadata/ModelMetadata.js @@ -1,38 +1,25 @@ const FieldMetadata = require('./FieldMetadata'); -const PropTypeLookup = { - text: 'string', - integer: 'number', - decimal: 'number', - boolean: 'bool', -}; - const identity = (field) => [field.name, field]; -function createParamsList(fields) { - const params = fields.filter(({ isParam }) => isParam).map(({ name }) => name); - - return Object.freeze(params); -} - class ModelMetadata { constructor({ modelName, tableName = modelName.toLowerCase(), fields }) { this.modelName = modelName; this.tableName = tableName; this.fields = Object.freeze(fields.map((field) => new FieldMetadata(field))); - this.params = createParamsList(this.fields); + this.params = Object.freeze(this.fields.filter(({ isParam }) => isParam).map(({ name }) => name)); } getFieldHash(convertField = identity) { - const hash = {}; - - this.fields.forEach((field) => { + return this.fields.reduce((result, field) => { const [name, convertedField] = convertField(field); - hash[name] = convertedField; - }); + if (name && convertedField) { + result[name] = convertedField; + } - return hash; + return result; + }, {}); } getParams() { @@ -42,26 +29,6 @@ class ModelMetadata { getObjectFields() { return this.fields.filter(({ type, isParam }) => isParam || type === 'enum'); } - - getPropTypes(PropTypes) { - return this.getObjectFields().reduce((result, field) => { - const { name, type, enumValues, required } = field; - const reactType = PropTypeLookup[type]; - let propType = PropTypes[reactType]; - - if (type === 'enum') { - propType = PropTypes.oneOf(enumValues); - } - - if (required) { - propType = propType.isRequired; - } - - result[name] = propType; - - return result; - }, {}); - } } module.exports = ModelMetadata; diff --git a/src/metadata/convertToPropType.js b/src/metadata/convertToPropType.js new file mode 100644 index 00000000..e7669325 --- /dev/null +++ b/src/metadata/convertToPropType.js @@ -0,0 +1,32 @@ +const PropTypes = require('prop-types'); + +const PropTypeLookup = { + text: 'string', + integer: 'number', + decimal: 'number', + boolean: 'bool', +}; + +module.exports = function convertToPropType(field) { + const { name, type, isParam, enumValues, required } = field; + // if the field isn't something we want to convert to a propType, then return an empty array so + // getFieldHash() won't include this field + let result = []; + + if (isParam || type === 'enum') { + const reactType = PropTypeLookup[type]; + let propType = PropTypes[reactType]; + + if (type === 'enum') { + propType = PropTypes.oneOf(enumValues); + } + + if (required) { + propType = propType.isRequired; + } + + result = [name, propType]; + } + + return result; +};