Skip to content

Commit

Permalink
Feature/form builder identifier schema (#188)
Browse files Browse the repository at this point in the history
Enhancement use new createIdentifier to append schema name
Enhancement Add schema name to identifier using new createIdentifier function
  • Loading branch information
Aaron Carlino authored and flamerohr committed Aug 14, 2017
1 parent 0f16818 commit c32673a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

43 changes: 35 additions & 8 deletions client/src/containers/FormBuilderLoader/FormBuilderLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ import FormBuilder, { basePropTypes, schemaPropType } from 'components/FormBuild
import getIn from 'redux-form/lib/structure/plain/getIn';
import { inject } from 'lib/Injector';

/**
* Creates a dot-separated identifier for forms generated
* with schemas (e.g. FormBuilderLoader)
*
* @param {string} identifier
* @param {object} schema
* @returns {string}
*/
function createFormIdentifierFromProps({ identifier, schema = {} }) {
return [
identifier,
schema.schema && schema.schema.name,
].filter(id => id).join('.');
}

class FormBuilderLoader extends Component {

constructor(props) {
Expand Down Expand Up @@ -55,6 +70,10 @@ class FormBuilderLoader extends Component {
return messages;
}

getIdentifier(props = this.props) {
return createFormIdentifierFromProps(props);
}

/**
* Handles updating the schema after response is received and gathering server-side validation
* messages.
Expand All @@ -79,13 +98,13 @@ class FormBuilderLoader extends Component {
this.props.actions.schema.setSchema(
this.props.schemaUrl,
schema,
this.props.identifier
this.getIdentifier()
);

const schemaRef = schema.schema || this.props.schema.schema;
if (schema.state) {
const formData = schemaFieldValues(schemaRef, schema.state);
this.props.actions.reduxForm.initialize(this.props.identifier, formData);
this.props.actions.reduxForm.initialize(this.getIdentifier(), formData);
}
}
return schema;
Expand Down Expand Up @@ -250,10 +269,18 @@ class FormBuilderLoader extends Component {
state: this.overrideStateData(formSchema.state),
}
);

this.props.actions.schema.setSchema(
this.props.schemaUrl,
overriddenSchema,
this.props.identifier
// Mock the will-be shape of the props so that the identifier is right
createFormIdentifierFromProps({
...this.props,
schema: {
...this.props.schema,
...overriddenSchema,
},
})
);

const schemaData = formSchema.schema || this.props.schema.schema;
Expand All @@ -262,7 +289,7 @@ class FormBuilderLoader extends Component {
// need to initialize the form again in case it was loaded before
// this will re-trigger Injector.form APIs, reset values and reset pristine state as well
this.props.actions.reduxForm.initialize(
this.props.identifier,
this.getIdentifier(),
formData,
false,
{ keepSubmitSucceeded: true }
Expand Down Expand Up @@ -299,7 +326,7 @@ class FormBuilderLoader extends Component {
* @param value
*/
handleAutofill(field, value) {
this.props.actions.reduxForm.autofill(this.props.identifier, field, value);
this.props.actions.reduxForm.autofill(this.getIdentifier(), field, value);
}

render() {
Expand All @@ -310,7 +337,7 @@ class FormBuilderLoader extends Component {
}

const props = Object.assign({}, this.props, {
form: this.props.identifier,
form: this.getIdentifier(),
onSubmitSuccess: this.props.onSubmitSuccess,
handleSubmit: this.handleSubmit,
onAutofill: this.handleAutofill,
Expand All @@ -335,11 +362,11 @@ FormBuilderLoader.propTypes = Object.assign({}, basePropTypes, {

function mapStateToProps(state, ownProps) {
const schema = state.form.formSchemas[ownProps.schemaUrl];

const identifier = createFormIdentifierFromProps(ownProps);
const reduxFormState =
state.form &&
state.form.formState &&
getIn(state.form.formState, ownProps.identifier);
getIn(state.form.formState, identifier);
const submitting = reduxFormState && reduxFormState.submitting;
const values = reduxFormState && reduxFormState.values;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* global jest, describe, beforeEach, it, expect */

jest.unmock('../FormBuilderLoader');

import { FormBuilderLoader } from '../FormBuilderLoader';

describe('createIdentifier', () => {
it('concatenates the identifier prop with the schema name prop', () => {
FormBuilderLoader.propTypes = {};
const form = new FormBuilderLoader();
let props = {};
expect(form.getIdentifier(props)).toBe('');
props = { identifier: 'one' };
expect(form.getIdentifier(props)).toBe('one');
props = {
identifier: 'one.two',
schema: 'three',
};
expect(form.getIdentifier(props)).toBe('one.two');
props = {
identifier: 'one.two',
schema: {
schema: 'three',
},
};
expect(form.getIdentifier(props)).toBe('one.two');
props = {
identifier: 'one.two',
schema: {
schema: {
name: 'three',
},
},
};
expect(form.getIdentifier(props)).toBe('one.two.three');
props = {
identifier: null,
schema: {
schema: {
name: 'three',
},
},
};
expect(form.getIdentifier(props)).toBe('three');
});
});

0 comments on commit c32673a

Please sign in to comment.