|
@@ -5,8 +5,9 @@ import Paper from 'material-ui/Paper'
|
|
|
import Typography from 'material-ui/Typography'
|
|
|
import TextField from 'material-ui/TextField'
|
|
|
import Button from 'material-ui/Button'
|
|
|
+import Snackbar from 'material-ui/Snackbar'
|
|
|
import { AUTH } from '../../constants/ResourceNames'
|
|
|
-import { post } from '../../actions'
|
|
|
+import { post, notify } from '../../actions'
|
|
|
import { getToken } from '../../utils/auth'
|
|
|
import { withStyles } from 'material-ui/styles'
|
|
|
|
|
@@ -69,14 +70,28 @@ class Login extends Component {
|
|
|
handleChangeInput = e => {
|
|
|
this.setState({
|
|
|
[e.target.id]: e.target.value
|
|
|
- }, this.handleChangeState)
|
|
|
+ })
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
*/
|
|
|
- handleChangeState = () => {
|
|
|
- // console.log(this.state)
|
|
|
+ handleSubmit = e => {
|
|
|
+ const { username, password } = this.state
|
|
|
+
|
|
|
+ this.props.submit(e, username, password)
|
|
|
+
|
|
|
+ this.setState({
|
|
|
+ username: '',
|
|
|
+ password: ''
|
|
|
+ }, this.handleFormReset)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ handleFormReset = () => {
|
|
|
+ this.authForm.reset()
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -84,8 +99,8 @@ class Login extends Component {
|
|
|
*/
|
|
|
render() {
|
|
|
const { from } = this.props.location.state || { from: { pathname: '/' }}
|
|
|
- const { isAuthenticated, submit, classes } = this.props
|
|
|
- const { username, password, hasError } = this.state
|
|
|
+ const { isAuthenticated, notification, onHideNotification, classes } = this.props
|
|
|
+ const { hasError } = this.state
|
|
|
|
|
|
if (isAuthenticated) {
|
|
|
return <Redirect to={from} />
|
|
@@ -95,7 +110,7 @@ class Login extends Component {
|
|
|
<div className={classes.root}>
|
|
|
<Paper className={classes.paper} elevation={4}>
|
|
|
<Typography className={classes.formHeading} variant='display1' color='primary' align='center'>Eiru Automation</Typography>
|
|
|
- <form className={classes.form} noValidate autoComplete='off'>
|
|
|
+ <form className={classes.form} noValidate autoComplete='off' ref={el => this.authForm = el}>
|
|
|
<TextField
|
|
|
id='username'
|
|
|
className={classes.txtField}
|
|
@@ -112,9 +127,15 @@ class Login extends Component {
|
|
|
label='Contraseña'
|
|
|
type='password'
|
|
|
/>
|
|
|
- <Button className={classes.submitBtn} variant='raised' color='primary' onClick={e => submit(e, username, password)}>Acceder</Button>
|
|
|
+ <Button className={classes.submitBtn} variant='raised' color='primary' onClick={this.handleSubmit}>Acceder</Button>
|
|
|
</form>
|
|
|
</Paper>
|
|
|
+ <Snackbar
|
|
|
+ open={notification.isVisible}
|
|
|
+ autoHideDuration={2000}
|
|
|
+ onClose={onHideNotification}
|
|
|
+ message={notification.message}
|
|
|
+ />
|
|
|
</div>
|
|
|
)
|
|
|
}
|
|
@@ -127,7 +148,8 @@ class Login extends Component {
|
|
|
*/
|
|
|
const mapStateToProps = (state, props) => {
|
|
|
return {
|
|
|
- isAuthenticated: state.app.isAuthenticated
|
|
|
+ isAuthenticated: state.auth.isAuthenticated,
|
|
|
+ notification: state.notification
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -137,6 +159,9 @@ const mapStateToProps = (state, props) => {
|
|
|
* @param {*} props
|
|
|
*/
|
|
|
const mapDispatchToProps = (dispatch, props) => ({
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
check() {
|
|
|
const token = getToken()
|
|
|
|
|
@@ -146,14 +171,21 @@ const mapDispatchToProps = (dispatch, props) => ({
|
|
|
|
|
|
dispatch(post(`${AUTH}check_token/`, { token }))
|
|
|
},
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param {*} e
|
|
|
+ * @param {*} username
|
|
|
+ * @param {*} password
|
|
|
+ */
|
|
|
submit(e, username, password) {
|
|
|
- console.log(username, password)
|
|
|
-
|
|
|
- dispatch(post(`${AUTH}get_token/`, {
|
|
|
- username,
|
|
|
- password
|
|
|
- }))
|
|
|
- }
|
|
|
+ dispatch(post(`${AUTH}get_token/`, { username, password }))
|
|
|
+ },
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ onHideNotification() {
|
|
|
+ dispatch(notify(null))
|
|
|
+ },
|
|
|
})
|
|
|
|
|
|
Login = withStyles(styles)(Login)
|