2
2
import { Link , Redirect , RouteComponentProps } from 'react-router-dom' ;
3
3
import { Form , FormGroup , FormControl , ControlLabel , Button , Col , Grid , Row } from 'react-bootstrap' ;
4
4
import 'isomorphic-fetch' ;
5
- import index from "popper.js" ;
6
5
7
6
interface HandleNameChangeInterface {
8
7
target : HTMLInputElement ;
9
8
}
10
9
11
- interface RegisterState {
12
- userName : string ,
13
- fullName : string ,
14
- email : string ,
15
- password : string ,
16
- confirmPassword : string
17
- } ;
18
-
19
10
export class Register extends React . Component < any , any > {
20
- constructor ( props : any ) {
21
- super ( props ) ;
11
+ constructor ( ) {
12
+ super ( ) ;
22
13
this . state = {
23
14
userName : '' ,
24
15
fullName : '' ,
25
16
email : '' ,
26
17
password : '' ,
27
- confirmPassword : ''
18
+ confirmPassword : '' ,
19
+ errors : [ ]
28
20
} ;
29
21
this . handleOnChange = this . handleOnChange . bind ( this ) ;
22
+ this . prepareFormData = this . prepareFormData . bind ( this ) ;
23
+ this . registerUser = this . registerUser . bind ( this ) ;
24
+ this . checkStatus = this . checkStatus . bind ( this ) ;
25
+ }
26
+
27
+ handleOnChange ( event : any ) : void {
28
+ this . setState ( { [ event . target . id ] : event . target . value , errors : [ ] } ) ;
30
29
}
31
30
32
- public handleOnChange ( event : any ) : void {
33
- this . setState ( { [ event . target . id ] : event . target . value } ) ;
34
- }
31
+ prepareFormData ( data = this . state ) {
32
+ return {
33
+ UserName : data . userName . trim ( ) ,
34
+ FullName : data . fullName . trim ( ) ,
35
+ Email : data . email . trim ( ) ,
36
+ Password : data . password . trim ( ) ,
37
+ ConfirmPassword : data . confirmPassword . trim ( )
38
+ } ;
39
+ }
35
40
36
- private registerUser ( event : React . FormEvent < HTMLFormElement > ) {
41
+ registerUser ( event : React . FormEvent < HTMLFormElement > ) {
37
42
// When pressing Enter, the page shouldn't reload
38
43
event . preventDefault ( ) ;
39
44
40
- var data = {
41
- UserName : this . state . userName ,
42
- FullName : this . state . fullName ,
43
- Email : this . state . email ,
44
- Password : this . state . password ,
45
- ConfirmPassword : this . state . confirmPassword
46
- } ;
45
+ var data = JSON . stringify ( this . prepareFormData ( ) ) ;
47
46
48
- fetch ( 'api/users/register' , {
49
- method : 'post' ,
50
- body : JSON . stringify ( data ) , // post body
47
+ fetch ( '/api/users/register' , {
48
+ method : 'POST' ,
51
49
headers : {
52
- 'Accept' : 'application/json'
53
- }
50
+ 'Accept' : 'application/json, application/xml, text/plain, text/html, *.*' ,
51
+ 'Content-Type' : 'application/json; charset=UTF-8'
52
+ } ,
53
+ body : data
54
54
} )
55
- . then ( ( ) => this . props . history . push ( "/login" ) )
56
- . catch ( error => console . error ( error ) ) ;
55
+ . then ( this . checkStatus ) ;
56
+ }
57
+
58
+ checkStatus ( res : any ) : void {
59
+ if ( res . status >= 200 && res . status < 300 ) {
60
+ this . props . history . push ( '/login' ) ;
61
+ } else {
62
+ let error = new Error ( res . statusTest ) ;
63
+ console . log ( error ) ;
64
+ this . props . history . push ( '/error' ) ;
65
+ }
57
66
}
58
67
59
- public render ( ) {
68
+ render ( ) {
60
69
return (
61
70
< Grid >
62
71
< Row className = "show-grid" >
63
72
< Col xs = { 12 } md = { 6 } >
64
73
< form onSubmit = { this . registerUser } >
65
- < Form horizontal >
66
- < FormGroup >
67
- < Col componentClass = { ControlLabel } sm = { 2 } > Username</ Col >
68
- < Col sm = { 5 } >
69
- < FormControl type = "text" placeholder = "Username" id = "userName" onChange = { this . handleOnChange } />
70
- </ Col >
71
- </ FormGroup >
72
- < FormGroup >
73
- < Col componentClass = { ControlLabel } sm = { 2 } > Full Name</ Col >
74
- < Col sm = { 5 } >
75
- < FormControl type = "text" placeholder = "Full Name" id = "fullName" onChange = { this . handleOnChange } />
76
- </ Col >
77
- </ FormGroup >
78
- < FormGroup >
79
- < Col componentClass = { ControlLabel } sm = { 2 } > Email</ Col >
80
- < Col sm = { 5 } >
81
- < FormControl type = "email" placeholder = "Email" id = "email" onChange = { this . handleOnChange } />
82
- </ Col >
83
- </ FormGroup >
84
- < FormGroup >
85
- < Col componentClass = { ControlLabel } sm = { 2 } > Password</ Col >
86
- < Col sm = { 5 } >
87
- < FormControl type = "password" placeholder = "Password" id = "password" onChange = { this . handleOnChange } />
88
- </ Col >
89
- </ FormGroup >
90
- < FormGroup >
91
- < Col componentClass = { ControlLabel } sm = { 2 } > Confirm Password</ Col >
92
- < Col sm = { 5 } >
93
- < FormControl type = "password" pattern = { this . state . password } placeholder = "Confirm Password" id = "confirmPassword" onChange = { this . handleOnChange } />
94
- </ Col >
95
- </ FormGroup >
96
- < FormGroup >
97
- < Col smOffset = { 2 } sm = { 10 } >
98
- < Button type = "submit" > Register</ Button >
99
- </ Col >
100
- </ FormGroup >
74
+ < Form horizontal >
75
+ < FormGroup >
76
+ < Col componentClass = { ControlLabel } sm = { 2 } > Username</ Col >
77
+ < Col sm = { 5 } >
78
+ < FormControl type = "text" placeholder = "Username" id = "userName" onChange = { this . handleOnChange } />
79
+ </ Col >
80
+ </ FormGroup >
81
+ < FormGroup >
82
+ < Col componentClass = { ControlLabel } sm = { 2 } > Full Name</ Col >
83
+ < Col sm = { 5 } >
84
+ < FormControl type = "text" placeholder = "Full Name" id = "fullName" onChange = { this . handleOnChange } />
85
+ </ Col >
86
+ </ FormGroup >
87
+ < FormGroup >
88
+ < Col componentClass = { ControlLabel } sm = { 2 } > Email</ Col >
89
+ < Col sm = { 5 } >
90
+ < FormControl type = "email" placeholder = "Email" id = "email" onChange = { this . handleOnChange } />
91
+ </ Col >
92
+ </ FormGroup >
93
+ < FormGroup >
94
+ < Col componentClass = { ControlLabel } sm = { 2 } > Password</ Col >
95
+ < Col sm = { 5 } >
96
+ < FormControl type = "password" placeholder = "Password" id = "password" onChange = { this . handleOnChange } />
97
+ </ Col >
98
+ </ FormGroup >
99
+ < FormGroup >
100
+ < Col componentClass = { ControlLabel } sm = { 2 } > Confirm Password</ Col >
101
+ < Col sm = { 5 } >
102
+ < FormControl type = "password" pattern = { this . state . password } placeholder = "Confirm Password" id = "confirmPassword" onChange = { this . handleOnChange } />
103
+ </ Col >
104
+ </ FormGroup >
105
+ < FormGroup >
106
+ < Col smOffset = { 2 } sm = { 10 } >
107
+ < Button type = "submit" > Register</ Button >
108
+ </ Col >
109
+ </ FormGroup >
101
110
</ Form >
102
111
</ form >
103
112
</ Col >
104
113
< Col xs = { 6 } md = { 4 } >
105
- < FormGroup >
106
- < ControlLabel > Hello, stranger! Are you ok with this info:</ ControlLabel >
107
- < FormControl . Static > Username: { this . state . userName } </ FormControl . Static >
108
- < FormControl . Static > Full Name: { this . state . fullName } </ FormControl . Static >
109
- < FormControl . Static > Email: { this . state . email } </ FormControl . Static >
114
+ < FormGroup >
115
+ < ControlLabel > Hello, stranger! Are you ok with this info:</ ControlLabel >
116
+ < FormControl . Static > Username: { this . state . userName } </ FormControl . Static >
117
+ < FormControl . Static > Full Name: { this . state . fullName } </ FormControl . Static >
118
+ < FormControl . Static > Email: { this . state . email } </ FormControl . Static >
110
119
</ FormGroup >
111
120
</ Col >
112
121
</ Row >
113
122
</ Grid > ) ;
114
-
115
- //
116
-
117
- //public render() {
118
-
119
- // return (
120
- // <Form horizontal>
121
- // <FormGroup controlId="formHorizontalUserName">
122
- // <Col componentClass={ControlLabel} sm={2}>Username</Col>
123
- // <Col sm={2}>
124
- // <input type="text" placeholder="Username" onChange={this.changeUserName} />
125
- // </Col>
126
- // </FormGroup>
127
- // <FormGroup controlId="formHorizontalFullName">
128
- // <Col componentClass={ControlLabel} sm={2}>Full Name</Col>
129
- // <Col sm={2}>
130
- // <input type="text" placeholder="Full Name" onChange={this.changeFullName} />
131
- // </Col>
132
- // </FormGroup>
133
- // <FormGroup controlId="formHorizontalEmail">
134
- // <Col componentClass={ControlLabel} sm={2}>Email</Col>
135
- // <Col sm={2}>
136
- // <input type="email" placeholder="Email" onChange={this.changeEmail} />
137
- // </Col>
138
- // </FormGroup>
139
- // <FormGroup controlId="formHorizontalPassword">
140
- // <Col componentClass={ControlLabel} sm={2}>Password</Col>
141
- // <Col sm={2}>
142
- // <input type="password" placeholder="Password" onChange={this.changePassword} />
143
- // </Col>
144
- // </FormGroup>
145
- // <FormGroup controlId="formHorizontalPassword">
146
- // <Col componentClass={ControlLabel} sm={2}>Confirm Password</Col>
147
- // <Col sm={2}>
148
- // <input type="password" placeholder="Confirm Password" onChange={this.changeConfirmPassword} />
149
- // </Col>
150
- // </FormGroup>
151
- // <FormGroup>
152
- // <Col smOffset={2} sm={10}>
153
- // <Button type="submit">Register</Button>
154
- // </Col>
155
- // </FormGroup>
156
- // </Form>
157
- // );
158
123
}
159
124
}
0 commit comments