Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React from 'react';
- import { connect } from 'react-redux';
- import ListErrors from './ListErrors';
- import agent from '../agent';
- // extract data from auth reducer
- const mapStateToProps = state => ({ ...state.auth });
- const mapDispatchToProps = dispatch => ({
- // update form values => reducer puts values into state => redux puts state into props and we re-render
- onChangeEmail: value =>
- dispatch({ type: 'UPDATE_FIELD_AUTH', key: 'email', value }),
- onChangePassword: value =>
- dispatch({ type: 'UPDATE_FIELD_AUTH', key: 'password', value }),
- // action with login request
- onSubmit: (email, password) =>
- dispatch({ type: 'LOGIN', payload: agent.Auth.login(email, password) })
- });
- class Login extends React.Component {
- constructor() {
- super();
- this.changeEmail = event => this.props.onChangeEmail(event.target.value);
- this.changePassword = event => this.props.onChangePassword(event.target.value);
- this.submitForm = (email, password) => event => {
- event.preventDefault();
- this.props.onSubmit(email, password);
- };
- }
- render() {
- const { email, password } = this.props;
- return (
- <div className="auth-page">
- {/* fixed width responsive container */}
- <div className="container page">
- {/* row to wrap column */}
- <div className="row">
- {/* add bootstrap column classes to align form elements and
- use offset to center the column */}
- <div className="col-md-6 offset-md-3 col-xs-12">
- <h1 className="text-xs-center">Sign In</h1>
- <p className="text-xs-center">
- <a>
- Need an account?
- </a>
- </p>
- <ListErrors errors={this.props.errors} />
- {/* login form */}
- <form> onSubmit={this.submitForm(email, password)}>
- <fieldset>
- {/* email input */}
- <fieldset className="form-group">
- {/* on change update email in state */}
- <input
- className="form-control form-control-lg"
- type="email"
- placeholder="Email"
- value={email}
- onChange={this.changeEmail} />
- </fieldset>
- {/* on change update password in state */}
- <fieldset className="form-group">
- <input
- className="form-control form-control-lg"
- type="password"
- placeholder="Password"
- value={password}
- onChange={this.changePassword} />
- </fieldset>
- {/* submit button */}
- <button
- className="btn btn-lg btn-primary pull-xs-right"
- type="submit"
- disabled={this.props.inProgress}>
- Sign in
- </button>
- </fieldset>
- </form>
- </div>
- </div>
- </div>
- </div>
- );
- }
- }
- export default connect(mapStateToProps, mapDispatchToProps)(Login);
Advertisement
Add Comment
Please, Sign In to add comment