|
@@ -1,26 +1,118 @@
|
|
|
import React, { Component } from 'react'
|
|
|
+import { connect } from 'react-redux'
|
|
|
+import { Redirect } from 'react-router-dom'
|
|
|
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 { AUTH } from '../../constants/ResourceNames'
|
|
|
+import { post } from '../../actions'
|
|
|
+import { getToken } from '../../utils/auth'
|
|
|
import { withStyles } from 'material-ui/styles'
|
|
|
|
|
|
const styles = theme => ({
|
|
|
root: {
|
|
|
+ position: 'absolute',
|
|
|
+ width: '100%',
|
|
|
+ height: '100%',
|
|
|
display: 'flex',
|
|
|
alignItems: 'center',
|
|
|
justifyContent: 'center'
|
|
|
+ },
|
|
|
+ paper: {
|
|
|
+ padding: theme.spacing.unit * 3
|
|
|
+ },
|
|
|
+ formHeading: {
|
|
|
+ marginTop: theme.spacing.unit * 3,
|
|
|
+ marginBottom: theme.spacing.unit * 2
|
|
|
+ },
|
|
|
+ form: {
|
|
|
+ display: 'flex',
|
|
|
+ flexDirection: 'column',
|
|
|
+ width: 350,
|
|
|
+ height: 230
|
|
|
+ },
|
|
|
+ txtField: {
|
|
|
+ margin: theme.spacing.unit * 2
|
|
|
+ },
|
|
|
+ submitBtn: {
|
|
|
+ marginTop: theme.spacing.unit * 3
|
|
|
}
|
|
|
})
|
|
|
|
|
|
class Login extends Component {
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param {*} props
|
|
|
+ */
|
|
|
+ constructor(props) {
|
|
|
+ super(props)
|
|
|
+
|
|
|
+ this.state = {
|
|
|
+ username: '',
|
|
|
+ password: '',
|
|
|
+ hasError: false,
|
|
|
+ redirectToReferrer: false
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ componentWillMount() {
|
|
|
+ this.props.check()
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ handleChangeInput = e => {
|
|
|
+ this.setState({
|
|
|
+ [e.target.id]: e.target.value
|
|
|
+ }, this.handleChangeState)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
+ handleChangeState = () => {
|
|
|
+ // console.log(this.state)
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ */
|
|
|
render() {
|
|
|
- const { classes } = this.props
|
|
|
+ const { from } = this.props.location.state || { from: { pathname: '/' }}
|
|
|
+ const { isAuthenticated, submit, classes } = this.props
|
|
|
+ const { username, password, hasError } = this.state
|
|
|
+
|
|
|
+ if (isAuthenticated) {
|
|
|
+ return <Redirect to={from} />
|
|
|
+ }
|
|
|
|
|
|
return (
|
|
|
<div className={classes.root}>
|
|
|
- <Paper className={classes.root} elevation={4}>
|
|
|
- <form>
|
|
|
- <TextField label='Usuario' autoComplete='off' />
|
|
|
- <TextField label='Contraseña' type='password' autoComplete='off' />
|
|
|
+ <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'>
|
|
|
+ <TextField
|
|
|
+ id='username'
|
|
|
+ className={classes.txtField}
|
|
|
+ error={hasError}
|
|
|
+ onKeyUp={this.handleChangeInput}
|
|
|
+ label='Usuario'
|
|
|
+ autoFocus
|
|
|
+ />
|
|
|
+ <TextField
|
|
|
+ id='password'
|
|
|
+ className={classes.txtField}
|
|
|
+ error={hasError}
|
|
|
+ onKeyUp={this.handleChangeInput}
|
|
|
+ label='Contraseña'
|
|
|
+ type='password'
|
|
|
+ />
|
|
|
+ <Button className={classes.submitBtn} variant='raised' color='primary' onClick={e => submit(e, username, password)}>Acceder</Button>
|
|
|
</form>
|
|
|
</Paper>
|
|
|
</div>
|
|
@@ -28,4 +120,42 @@ class Login extends Component {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export default withStyles(styles)(Login)
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} state
|
|
|
+ * @param {*} props
|
|
|
+ */
|
|
|
+const mapStateToProps = (state, props) => {
|
|
|
+ return {
|
|
|
+ isAuthenticated: state.app.isAuthenticated
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {*} dispatch
|
|
|
+ * @param {*} props
|
|
|
+ */
|
|
|
+const mapDispatchToProps = (dispatch, props) => ({
|
|
|
+ check() {
|
|
|
+ const token = getToken()
|
|
|
+
|
|
|
+ if (!token) {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ dispatch(post(`${AUTH}check_token/`, { token }))
|
|
|
+ },
|
|
|
+ submit(e, username, password) {
|
|
|
+ console.log(username, password)
|
|
|
+
|
|
|
+ dispatch(post(`${AUTH}get_token/`, {
|
|
|
+ username,
|
|
|
+ password
|
|
|
+ }))
|
|
|
+ }
|
|
|
+})
|
|
|
+
|
|
|
+Login = withStyles(styles)(Login)
|
|
|
+
|
|
|
+export default connect(mapStateToProps, mapDispatchToProps)(Login)
|