Created
November 12, 2015 20:28
-
-
Save alexbeletsky/8e199b8c3f43939e2628 to your computer and use it in GitHub Desktop.
React Redux SignUp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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