client/src/components/Login.js
author ymh <ymh.work@gmail.com>
Tue, 20 Jun 2017 19:04:42 +0200
changeset 65 14989b339e5d
parent 62 b2514a9bcd49
child 89 06f609adfbf8
permissions -rw-r--r--
Use a fonctionnal component for the login part of the navbar

import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl, Button, Alert } from 'react-bootstrap';
import '../App.css';
import Navbar from './Navbar';
import * as authActions from '../actions/authActions';

class Login extends Component {

  login = () => {
    const username = this.username.value;
    const password = this.password.value;

    this.props.authActions.loginSubmit(username, password);
  }

  renderError() {
    return (
      <Alert bsStyle="danger">Bad credentials</Alert>
    )
  }

  render() {
    return (
      <div>
        <Navbar history={this.props.history} />
        <Grid fluid>
          <Row>
            <Col md={6} mdOffset={3}>
              <Panel>
                <form>
                  <FormGroup>
                    <ControlLabel>Username</ControlLabel>
                    <FormControl componentClass="input" type="text" inputRef={ref => { this.username = ref; }} />
                  </FormGroup>
                  <FormGroup>
                    <ControlLabel>Password</ControlLabel>
                    <FormControl componentClass="input" type="password" inputRef={ref => { this.password = ref; }} />
                  </FormGroup>
                  { this.props.login.error && this.renderError() }
                  <Button block bsStyle="primary" onClick={this.login}>Login</Button>
                </form>
              </Panel>
            </Col>
          </Row>
        </Grid>
      </div>
    );
  }
}

function mapStateToProps(state, props) {
  return {
    currentUser: state['currentUser'],
    login: state['login']
  };
}

function mapDispatchToProps(dispatch) {
  return {
    authActions: bindActionCreators(authActions, dispatch)
  }
}

export default connect(mapStateToProps, mapDispatchToProps)(Login);