Skip to content

Commit

Permalink
Refactor ModelMetadata
Browse files Browse the repository at this point in the history
Use getFieldHash() for getting the prop types as well, passing in convertToPropType.
  • Loading branch information
fwextensions committed Jul 21, 2022
1 parent 6ffd9fb commit 01afec4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 41 deletions.
3 changes: 2 additions & 1 deletion src/Models/Ringdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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),
Expand Down
47 changes: 7 additions & 40 deletions src/metadata/ModelMetadata.js
Original file line number Diff line number Diff line change
@@ -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) => {
this.fields.reduce((result, field) => {
const [name, convertedField] = convertField(field);

hash[name] = convertedField;
});
if (name && convertedField) {
result[name] = convertedField;
}

return hash;
return result;
}, {});
}

getParams() {
Expand All @@ -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;
32 changes: 32 additions & 0 deletions src/metadata/convertToPropType.js
Original file line number Diff line number Diff line change
@@ -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;
};

0 comments on commit 01afec4

Please sign in to comment.