client/src/components/Login.js
changeset 44 3b20e2b584fe
parent 12 48ddaa42b810
child 62 b2514a9bcd49
equal deleted inserted replaced
43:3c9d3c8f41d1 44:3b20e2b584fe
     1 import React, { Component } from 'react';
     1 import React, { Component } from 'react';
     2 import { connect } from 'react-redux';
     2 import { connect } from 'react-redux';
     3 import { bindActionCreators } from 'redux';
     3 import { bindActionCreators } from 'redux';
     4 import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl, Button } from 'react-bootstrap';
     4 import { Grid, Row, Col, Panel, FormGroup, ControlLabel, FormControl, Button, Alert } from 'react-bootstrap';
     5 import '../App.css';
     5 import '../App.css';
     6 import Navbar from './Navbar';
     6 import Navbar from './Navbar';
     7 import * as sessionsActions from '../actions/sessionsActions';
     7 import * as authActions from '../actions/authActions';
     8 
     8 
     9 class Login extends Component {
     9 class Login extends Component {
    10 
    10 
    11   login = () => {
    11   login = () => {
    12     const username = this.username.value;
    12     const username = this.username.value;
    13     const password = this.password.value;
    13     const password = this.password.value;
    14 
    14 
    15     console.log(username, password);
    15     this.props.authActions.loginSubmit(username, password);
       
    16   }
    16 
    17 
       
    18   renderError() {
       
    19     return (
       
    20       <Alert bsStyle="danger">Bad credentials</Alert>
       
    21     )
    17   }
    22   }
    18 
    23 
    19   render() {
    24   render() {
    20     return (
    25     return (
    21       <div>
    26       <div>
    31                   </FormGroup>
    36                   </FormGroup>
    32                   <FormGroup>
    37                   <FormGroup>
    33                     <ControlLabel>Password</ControlLabel>
    38                     <ControlLabel>Password</ControlLabel>
    34                     <FormControl componentClass="input" type="password" inputRef={ref => { this.password = ref; }} />
    39                     <FormControl componentClass="input" type="password" inputRef={ref => { this.password = ref; }} />
    35                   </FormGroup>
    40                   </FormGroup>
       
    41                   { this.props.login.error && this.renderError() }
    36                   <Button block bsStyle="primary" onClick={this.login}>Login</Button>
    42                   <Button block bsStyle="primary" onClick={this.login}>Login</Button>
    37                 </form>
    43                 </form>
    38               </Panel>
    44               </Panel>
    39             </Col>
    45             </Col>
    40           </Row>
    46           </Row>
    44   }
    50   }
    45 }
    51 }
    46 
    52 
    47 function mapStateToProps(state, props) {
    53 function mapStateToProps(state, props) {
    48   return {
    54   return {
    49     currentSession: state.get('currentSession'),
    55     currentUser: state.get('currentUser'),
    50     sessions: state.get('sessions')
    56     login: state.get('login')
    51   };
    57   };
    52 }
    58 }
    53 
    59 
    54 function mapDispatchToProps(dispatch) {
    60 function mapDispatchToProps(dispatch) {
    55   return {
    61   return {
    56     actions: bindActionCreators(sessionsActions, dispatch)
    62     authActions: bindActionCreators(authActions, dispatch)
    57   }
    63   }
    58 }
    64 }
    59 
    65 
    60 export default connect(mapStateToProps, mapDispatchToProps)(Login);
    66 export default connect(mapStateToProps, mapDispatchToProps)(Login);