Skip to content

Instantly share code, notes, and snippets.

@alexbeletsky
Created November 12, 2015 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbeletsky/8e199b8c3f43939e2628 to your computer and use it in GitHub Desktop.
Save alexbeletsky/8e199b8c3f43939e2628 to your computer and use it in GitHub Desktop.
React Redux SignUp
export function signUpErrors(errors) {
return { type: 'SIGNUP_ERRORS', errors };
}
export function signUpClearError(name) {
return { type: 'SIGNUP_CLEAR_ERROR', name };
}
export function createAccount(account, accountCreated) {
return (dispatch) => {
dispatch({ type: 'CREATING_ACCOUNT', account });
setTimeout(() => {
dispatch({ type: 'CREATE_ACCOUNT', user: {} });
if (accountCreated) {
accountCreated();
}
}, 500);
};
}
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { signUpErrors, signUpClearError, createAccount } from '../../redux/actions';
import {
SignUpForm
} from '../../components';
class SignUp extends Component {
static propTypes = {
signup: PropTypes.object,
dispatch: PropTypes.func
}
static contextTypes = {
store: PropTypes.object.isRequired,
history: PropTypes.object.isRequired
}
onFormSubmitted(errors, account) {
const { dispatch } = this.props;
const { history } = this.context;
if (errors) {
return dispatch(signUpErrors(errors));
}
const accountCreated = () => {
history.pushState({}, '/welcome');
};
dispatch(createAccount(account, accountCreated));
}
onClearError(name) {
const { dispatch } = this.props;
dispatch(signUpClearError(name));
}
render() {
return (
<div className="sign-up">
<div className="container-fluid row">
<SignUpForm {...this.props.signup} onFormSubmitted={::this.onFormSubmitted} onClearError={::this.onClearError}/>
</div>
</div>
);
}
}
const stateToProps = (state) => {
return { signup: state.signup };
};
export default connect(stateToProps)(SignUp);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment