/
LoginView.jsx
111 lines (103 loc) · 2.8 KB
/
LoginView.jsx
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
105
106
107
108
109
110
111
import React, { Component } from 'react';
import { Button, FormControl, FormGroup, ControlLabel, Col, Row, Panel } from 'react-bootstrap';
import Login from '../api/Login.jsx';
export default class LoginView extends Component {
constructor(props){
super(props);
this.state = {
username: undefined,
password: undefined,
error_state: null,
error: undefined
}
}
onSubmit(){
if(!this.state.username || !this.state.password) {
this.setState({
error_state: 'error',
error: "You must enter a username and password"
})
return(false)
}
const login = Login(this.state.username, this.state.password)
.then(function(response){
this.setState({
error_state: null,
error: undefined
})
// Have to force a window redirect here to ensure good login state display
window.location.assign('/');
}.bind(this))
.catch(function(response){
this.setState({
error_state: 'error',
error: 'Invalid login credentials, please check and try again.'
})
}.bind(this))
}
handleFormKey(event){
if(event.key == 'Enter'){
this.onSubmit()
}
}
handleUsername(event){
this.setState({
username: event.target.value
})
}
handlePassword(event){
this.setState({
password: event.target.value
})
}
displayError(){
if(this.state.error){
return(
<Panel header="Error" bsStyle="danger">
{this.state.error}
</Panel>
)
}
}
render() {
return (
<div onKeyPress={(event) => this.handleFormKey(event)}>
<h1>Login to Docker Registry</h1>
{this.displayError()}
<Row>
<form>
<FormGroup
validationState={this.state.error_state}
>
<Col md={4}>
<Row style={{paddingBottom: "1em"}}>
<Col sm={12}>
<FormControl type="text"
placeholder="Username"
onChange={(event) => this.handleUsername(event)}
/>
</Col>
</Row>
<Row>
<Col sm={12}>
<FormControl type="password"
placeholder="Password"
onChange={(event) => this.handlePassword(event)}
/>
</Col>
</Row>
<Row style={{paddingBottom: "1em", paddingTop: "1em"}}>
<Col md={4}>
<Button bsStyle="primary" onClick={() => this.onSubmit()}>
Login
</Button>
</Col>
</Row>
</Col>
</FormGroup>
</form>
</Row>
</div>
);
}
}