1
1
import React , { PureComponent } from 'react' ;
2
2
import { connect } from 'react-redux' ;
3
3
import {
4
- Grid , Row , Col , Form , FormGroup , FormControl , ControlLabel , Button
4
+ Grid , Row , Col , Form , FormGroup , FormControl , ControlLabel , Button , HelpBlock
5
5
} from 'react-bootstrap' ;
6
6
7
7
import * as actionCreators from '../actions/index.js' ;
8
- import Logo from './Logo.js' ;
9
- import logoSvg from '../images/logo.svg' ;
10
8
11
9
/**
12
10
This component is responsible for showing the login form.
@@ -22,79 +20,118 @@ class LoginForm extends PureComponent {
22
20
this . handleSubmission = this . handleSubmission . bind ( this ) ;
23
21
}
24
22
23
+ state = {
24
+ errors : {
25
+ email : null ,
26
+ password : null
27
+ }
28
+ }
29
+
25
30
render ( ) {
26
31
return (
27
32
< Grid fluid >
28
- < Row >
29
- < Logo logo = { logoSvg } />
30
- </ Row >
31
-
32
33
< Form horizontal onReset = { this . handleReset } onSubmit = { this . handleSubmission } >
33
- < FormGroup
34
- controlId = "formHorizontalEmail"
35
- validationState = { this . props . emailValidationState }
36
- >
37
- < Col lgOffset = { 4 } componentClass = { ControlLabel } lg = { 1 } >
38
- Email
39
- </ Col >
40
- < Col lg = { 3 } >
41
- < FormControl
42
- type = "email"
43
- placeholder = "e.g. someone@example.com"
44
- autoFocus
45
- name = "email"
46
- value = { this . props . email }
47
- onChange = { this . updateEmail }
48
- />
49
- </ Col >
50
- </ FormGroup >
51
-
52
- < FormGroup
53
- controlId = "formHorizontalPassword"
54
- validationState = { this . props . passwordValidationState }
55
- >
56
- < Col lgOffset = { 4 } componentClass = { ControlLabel } lg = { 1 } >
57
- Password
34
+ < Row >
35
+ < Col lgOffset = { 4 } lg = { 3 } >
36
+ < FormGroup
37
+ controlId = "formHorizontalEmail"
38
+ validationState = { this . props . emailValidationState }
39
+ >
40
+ < ControlLabel > Email</ ControlLabel >
41
+ < FormControl
42
+ type = "email"
43
+ placeholder = "e.g. someone@example.com"
44
+ autoFocus
45
+ name = "email"
46
+ value = { this . props . email }
47
+ onChange = { this . updateEmail }
48
+ />
49
+ < HelpBlock > { this . state . errors . email } </ HelpBlock >
50
+ </ FormGroup >
58
51
</ Col >
59
- < Col lg = { 3 } >
60
- < FormControl
61
- type = "password"
62
- placeholder = "e.g. password"
63
- name = "password"
64
- value = { this . props . password }
65
- onChange = { this . updatePassword }
66
- />
52
+ </ Row >
53
+
54
+ < Row >
55
+ < Col lgOffset = { 4 } lg = { 3 } >
56
+ < FormGroup
57
+ controlId = "formHorizontalPassword"
58
+ validationState = { this . props . passwordValidationState }
59
+ >
60
+ < ControlLabel > Password</ ControlLabel >
61
+ < FormControl
62
+ type = "password"
63
+ placeholder = "e.g. password"
64
+ name = "password"
65
+ value = { this . props . password }
66
+ onChange = { this . updatePassword }
67
+ />
68
+ < HelpBlock > { this . state . errors . password } </ HelpBlock >
69
+ </ FormGroup >
67
70
</ Col >
68
- </ FormGroup >
69
-
70
- < FormGroup >
71
- < Col lgOffset = { 5 } lg = { 2 } >
72
- < Button type = "reset" > Cancel </ Button >
73
-
74
- < Button
75
- type = "submit"
76
- bsStyle = "primary"
77
- disabled = { ! this . props . allowSubmission }
78
- > Sign in </ Button >
71
+ </ Row >
72
+
73
+ < Row >
74
+ < Col lgOffset = { 4 } lg = { 3 } >
75
+ < FormGroup >
76
+ < Button
77
+ type = "submit"
78
+ bsStyle = "primary"
79
+ block
80
+ disabled = { ! this . props . allowSubmission }
81
+ > Sign in </ Button >
82
+ </ FormGroup >
79
83
</ Col >
80
- </ FormGroup >
84
+ </ Row >
81
85
</ Form >
82
86
</ Grid >
83
87
) ;
84
88
}
85
89
86
90
updateEmail ( event ) {
87
- this . props . handleChange ( event . target . value , this . props . password ) ;
91
+ let trimmedEmail = event . target . value . trim ( ) ;
92
+ let emailRegex = / \w + @ \w + \. \w + / m;
93
+ let errorMsg = null ;
94
+ let updatedErrors = null ;
95
+
96
+ if ( trimmedEmail === '' ) {
97
+ errorMsg = "Email can't be left blank" ;
98
+ } else if ( ! emailRegex . test ( trimmedEmail ) ) {
99
+ errorMsg = "Email format is invalid!" ;
100
+ } else {
101
+ errorMsg = null ;
102
+ }
103
+
104
+ updatedErrors = this . state . errors ;
105
+ updatedErrors . email = errorMsg ;
106
+
107
+ this . setState ( updatedErrors ) ;
108
+ this . props . handleChange ( { email : event . target . value } ) ;
88
109
}
89
110
90
111
updatePassword ( event ) {
91
- this . props . handleChange ( this . props . email , event . target . value ) ;
112
+ let password = event . target . value ;
113
+ let errorMsg = null ;
114
+ let updatedErrors = null ;
115
+
116
+ if ( password === '' ) {
117
+ errorMsg = "Password can't be left blank" ;
118
+ } else if ( password . length < 8 ) {
119
+ errorMsg = "Password must be at least 8 characters long!" ;
120
+ } else {
121
+ errorMsg = null ;
122
+ }
123
+
124
+ updatedErrors = this . state . errors ;
125
+ updatedErrors . password = errorMsg ;
126
+
127
+ this . setState ( updatedErrors ) ;
128
+ this . props . handleChange ( { password : event . target . value } ) ;
92
129
}
93
130
94
131
handleSubmission ( event ) {
95
132
event . preventDefault ( ) ;
96
133
97
- this . props . handleLogin ( this . props . email , this . props . password ) ;
134
+ this . props . handleLogin ( this . props . password ) ;
98
135
}
99
136
100
137
componentWillReceiveProps ( nextProps ) {
0 commit comments