summaryrefslogtreecommitdiff
path: root/app/client/auth/signup.component.js
blob: 4882681c7b1a3b2004ead4b629b050c0dd10225f (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { h, Component } from 'preact';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import { Redirect } from 'react-router-dom';
import * as actions from './auth.actions';

import { Group, Param, TextInput, Button } from '../common';

class Signup extends Component {
  state = {
    username: '',
    password: '',
    password2: '',
  }
  constructor() {
    super()
    this.handleChange = this.handleChange.bind(this)
    this.handleSubmit = this.handleSubmit.bind(this)
  }
  handleChange(value, name) {
    this.setState({
      [name]: value,
    })
  }
  validate(){
    if (!this.state.password || this.state.password !== this.state.password2) {
      return false
    }
    return true
  }
  handleSubmit(e) {
    e.preventDefault()
    if (this.props.auth.loading) return
    if (!this.validate()) {
      return this.props.actions.setError('bad password')
    }
    let { ...user } = this.state
    this.props.actions.signup(user)
  }
  render(){
    if (this.props.auth.isAuthenticated) {
      let { returnTo } = this.props.auth
      if (!returnTo || returnTo.match(/(api|login|logout|signup)/i)) {
        returnTo = '/'
      }
      return <Redirect to={returnTo} />
    }
    return (
      <form onSubmit={this.handleSubmit}>
        <h1>New account</h1><br />
        <Group>
          <TextInput
            autofocus
            autocapitalize="off"
            autocomplete="off"
            title="Username"
            name="username"
            type="text"
            value={this.state.username}
            onInput={this.handleChange}
          />
          <TextInput
            title="Password"
            name="password"
            type="password"
            value={this.state.password}
            onInput={this.handleChange}
          />
          <TextInput
            title="Password again :)"
            name="password2"
            type="password"
            value={this.state.password2}
            onInput={this.handleChange}
          />
          <Button
            loading={this.props.auth.loading}
          >
            Sign up
          </Button>
          {this.renderAuthError()}
        </Group>
      </form>
    )
  }
  renderAuthError(){
    if (this.props.auth.error) {
      return (
        <div className='form-input-hint'>{"Please doublecheck the form (o=_o~~)"}</div>
      )
    }
    return <div className='form-input-hint'></div>
  }
}

const mapStateToProps = (state) => ({
  auth: state.auth,
});

const mapDispatchToProps = (dispatch) => ({
  actions: bindActionCreators({ ...actions }, dispatch)
});

export default connect(mapStateToProps, mapDispatchToProps)(Signup);