import React, { Component } from 'react';
import {BrowserRouter as Router, Route, Link, Redirect, withRouter} from "react-router-dom";
import TextField from 'material-ui/TextField';
import RaisedButton from 'material-ui/RaisedButton';
import { getTranslate, getActiveLanguage } from 'react-localize-redux'
import { connect } from 'react-redux';
import { authenticated } from '../reducers/login-reducer';
import { auth } from '../reducers/login-reducer';
import { login } from '../reducers/login-reducer';
import { browserHistory } from 'react-router'
class Login extends Component {
constructor(props) {
super(props);
this.state = {
username:'',
password:''
};
this.onSubmit = this.onSubmit.bind(this);
};
onSubmit =(e) =>{
e.preventDefault();
const { username, password } = this.state;
this.props.login(username,password);
this.props.authenticated(username,password);
this.setState({
username: '',
password: ''
});
};
render(){
const { username, password } = this.state;
????
const { from } = this.props.location.state || { from: { pathname: "/" } };
const { isAuth } =this.state;
if ( this.props.authenticated()) {return <Redirect to={ from } />;}
????
return (
<div className="form-login" >
<h1><b>Sisäänkirjautuminen</b></h1>
<h3>Kirjaudu sisään antamalla käyttäjätunnus ja salasana.</h3>
<form name ="loginForm" onSubmit={this.onSubmit}>
<TextField
name="username"
type="text"
onChange={e =>this.setState({username: e.target.value})}
value={username}
fullWidth={true} />
<TextField
name="password"
type="password"
onChange={e =>this.setState({password: e.target.value})}
value={password}
fullWidth={true}/>
<br/>
<RaisedButton label="Login" type="submit"/>
</form>
</div>
)
}
}
const mapStateToProps = (state)=>{
return {
isLoginPending: state.isLoginPending,
isLoginSuccess: state.isLoginSuccess,
isLoginError: state.isLoginError,
isAuth: state.isAuth
};
};
const mapDispatchToProps = (dispatch) => {
return {
login: (username, password) => dispatch(login(username,password)),
authenticated: (username, password) => dispatch(authenticated(username,password))
}
};
export default connect(mapStateToProps, mapDispatchToProps)(Login);