xonecas.io
3517
Mt

meal_tracker / files

meal_tracker / bmi_ui / src / components / LoginForm.js

1.5 KB
1import React from 'react'
2import PropTypes from 'prop-types'
3import { Button, Form, FormGroup, Input, Label } from 'reactstrap'
4import axios from 'axios'
5import { API_URL } from '../constants'
6
7class UserForm extends React.Component {
8  constructor (props) {
9    super(props)
10
11    this.state = {
12      username: '',
13      password: ''
14    }
15  }
16
17  static get propTypes () {
18    return {
19      user: PropTypes.any,
20      toggle: PropTypes.func,
21      update: PropTypes.func
22    }
23  }
24
25  onChange (e) {
26    this.setState({ [e.target.name]: e.target.value })
27  }
28
29  login (e) {
30    e && e.preventDefault()
31
32    axios.post(API_URL + 'login/', this.state, { crossdomain: true }).then((response) => {
33      this.props.toggle()
34      this.props.update(response.data)
35    })
36  }
37
38  render () {
39    const boundChange = this.onChange.bind(this)
40
41    return (
42      <Form onSubmit={this.login.bind(this)}>
43        <FormGroup>
44          <Label for="username">Username:</Label>
45          <Input
46            type="text"
47            name="username"
48            onChange={boundChange}
49            value={this.state.name}
50          />
51        </FormGroup>
52        <FormGroup>
53          <Label for="examplePassword">Password</Label>
54          <Input
55            type="password"
56            name="password"
57            onChange={boundChange}
58            value={this.state.name}
59          />
60        </FormGroup>
61
62        <Button className="float-right" color="primary">Login</Button>
63      </Form>
64    )
65  }
66}
67
68export default UserForm