Skip to content

Commit

Permalink
Merge pull request #158 from jedwards1211/meatier-form
Browse files Browse the repository at this point in the history
create meatierForm to cut down on boilerplate
  • Loading branch information
mattkrick authored Jul 10, 2016
2 parents c6bb902 + c1c8738 commit d26bd46
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 31 deletions.
22 changes: 22 additions & 0 deletions src/universal/decorators/meatierForm/meatierForm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import {reduxForm} from 'redux-form';
import Joi from 'joi';
import {parsedJoiErrors} from 'universal/utils/schema';
import {getFormState} from 'universal/redux/helpers';

/**
* A small wrapper for reduxForm that can (optionally) inject a validate method based on the Joi `schema` prop in the
* options, and also injects the necessary `getFormState` for `redux-optimistic-ui`.
*/
export default function meatierForm(options) {
const {schema} = options
return reduxForm(Object.assign(
options,
{getFormState},
schema && {
validate(values) {
const results = Joi.validate(values, options.schema, {abortEarly: false});
return parsedJoiErrors(results.error);
}
}
));
}
12 changes: 2 additions & 10 deletions src/universal/modules/auth/components/LostPassword/LostPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import React, {PropTypes, Component} from 'react';
import TextField from 'material-ui/TextField';
import RaisedButton from 'material-ui/RaisedButton';
import styles from './LostPassword.css';
import {reduxForm} from 'redux-form';
import Joi from 'joi';
import meatierForm from 'universal/decorators/meatierForm/meatierForm'
import {emailPasswordReset} from '../../ducks/auth';
import {authSchemaEmail} from '../../schemas/auth';
import {parsedJoiErrors} from 'universal/utils/schema';
import {getFormState} from 'universal/redux/helpers';

@reduxForm({form: 'lostPasswordForm', fields: ['email'], validate, getFormState})
@meatierForm({form: 'lostPasswordForm', fields: ['email'], schema: authSchemaEmail})
export default class LostPassword extends Component {
static propTypes = {
fields: PropTypes.object,
Expand Down Expand Up @@ -59,8 +56,3 @@ export default class LostPassword extends Component {
);
}
}

function validate(values) {
const results = Joi.validate(values, authSchemaEmail, {abortEarly: false});
return parsedJoiErrors(results.error);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ import React, {PropTypes, Component} from 'react';
import TextField from 'material-ui/TextField';
import RaisedButton from 'material-ui/RaisedButton';
import styles from './ResetPassword.css';
import {reduxForm} from 'redux-form';
import Joi from 'joi';
import meatierForm from 'universal/decorators/meatierForm/meatierForm'
import {authSchemaPassword} from '../../schemas/auth';
import {resetPassword} from '../../ducks/auth';
import {parsedJoiErrors} from 'universal/utils/schema';
import {getFormState} from 'universal/redux/helpers';

@reduxForm({form: 'resetPasswordForm', fields: ['password'], validate, getFormState})
@meatierForm({form: 'resetPasswordForm', fields: ['password'], schema: authSchemaPassword})
export default class ResetPassword extends Component {
static propTypes = {
fields: PropTypes.any,
Expand Down Expand Up @@ -57,8 +54,3 @@ export default class ResetPassword extends Component {
return resetPassword(outData, dispatch);
};
}

function validate(values) {
const results = Joi.validate(values, authSchemaPassword, {abortEarly: false});
return parsedJoiErrors(results.error);
}
13 changes: 2 additions & 11 deletions src/universal/modules/auth/containers/Auth/AuthContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@ import React, {Component, PropTypes} from 'react';
import {authSchemaInsert} from '../../schemas/auth';
import {connect} from 'react-redux';
import Auth from '../../components/Auth/Auth';
import {reduxForm} from 'redux-form';
import Joi from 'joi';
import {parsedJoiErrors} from 'universal/utils/schema';
import {getFormState} from 'universal/redux/helpers';
import {ensureState} from 'redux-optimistic-ui';
import meatierForm from 'universal/decorators/meatierForm/meatierForm'

// use the same form to retain form values (there's really no difference between login and signup, it's just for show)
@connect(mapStateToProps)
// must come after connect to get the path field
@reduxForm({form: 'authForm', fields: ['email', 'password'], validate, getFormState})
@meatierForm({form: 'authForm', fields: ['email', 'password'], schema: authSchemaInsert})
export default class AuthContainer extends Component {
static propTypes = {
location: PropTypes.object,
Expand Down Expand Up @@ -42,9 +39,3 @@ function mapStateToProps(state, props) {
pathname: props.location.pathname
};
}

function validate(values) {
const results = Joi.validate(values, authSchemaInsert, {abortEarly: false});
return parsedJoiErrors(results.error);
}

0 comments on commit d26bd46

Please sign in to comment.