summaryrefslogtreecommitdiff
path: root/animism-align/frontend/app/views/auth/auth.login.js
blob: 6697901f7eaa3ebb0cc8aad4b84fd8250c79160f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React, { Component } from 'react'

import './auth.css'

import actions from 'app/actions'
import { TextInput, SubmitButton } from 'app/common'

export default class AuthLogin extends Component {
  state = {
    data: {
      username: "",
      password: "",
    },
    error: null,
  }

  constructor(props) {
    super(props)
    this.handleChange = this.handleChange.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }

  handleChange(e) {
    e && e.preventDefault()
    this.setState({
      data: {
        ...this.state.data,
        [e.target.name]: e.target.value
      }
    })
  }

  handleSubmit(e) {
    e && e.preventDefault()
    this.setState({ error: null })
    actions.auth.login(this.state.data)
    .then(this.props.onAuthenticate)
    .catch(error => {
      console.error(error)
      this.setState({ error: error.description || error.message })
    })
  }

  render() {
    return (
      <form className='login' onSubmit={this.handleSubmit}>
        <h6>
          {'Welcome to Animism '}{'🐍'}
        </h6>
        <TextInput
          title="Username"
          name="username"
          data={this.state.data}
          onChange={this.handleChange}
          autoComplete="off"
        />
        <TextInput
          type="password"
          title="Password"
          name="password"
          data={this.state.data}
          onChange={this.handleChange}
          autoComplete="off"
        />
        <SubmitButton
          title="Log in"
          after={this.state.error}
        />
      </form>
    )
  }
}