1
1
import React , { Component , useState } from 'react' ;
2
- import { connect } from 'react' ;
2
+ // import { connect } from 'react';
3
3
import { LoginInt } from '../../interfaces/Interfaces' ;
4
4
import { setUsername , setPassword } from '../../actions/actionCreators' ;
5
- import { useSelector } from 'react-redux' ;
5
+ //import { useSelector } from 'react-redux';
6
+ import {
7
+ Link as RouteLink ,
8
+ withRouter ,
9
+ useHistory ,
10
+ Redirect
11
+ } from 'react-router-dom' ;
6
12
7
13
import Avatar from '@material-ui/core/Avatar' ;
8
14
import Button from '@material-ui/core/Button' ;
@@ -22,18 +28,18 @@ import { render } from 'enzyme';
22
28
const mapStateToProps = ( store : any ) => ( {
23
29
username : store . credentials . username ,
24
30
password : store . credentials . password
25
- } )
31
+ } ) ;
26
32
27
33
const mapDispatchToProps = ( dispatch : any ) => ( {
28
34
setUsername : ( username : string ) => dispatch ( setUsername ( username ) ) ,
29
- setPassword : ( password : string ) => dispatch ( setPassword ( password ) ) ,
35
+ setPassword : ( password : string ) => dispatch ( setPassword ( password ) )
30
36
// login: (username: string, password: string) => dispatch(login(username, password)),
31
37
// signup: (username: string, password: string) => dispatch(signup(username, password)),
32
- } )
38
+ } ) ;
33
39
34
40
interface LoginProps extends LoginInt {
35
- setUsername ( username : string ) : void ,
36
- setPassword ( username : string ) : void
41
+ setUsername ( username : string ) : void ;
42
+ setPassword ( username : string ) : void ;
37
43
}
38
44
39
45
function Copyright ( ) {
@@ -49,34 +55,36 @@ function Copyright() {
49
55
) ;
50
56
}
51
57
52
- const useStyles = makeStyles ( ( theme ) => ( {
58
+ const useStyles = makeStyles ( theme => ( {
53
59
paper : {
54
60
marginTop : theme . spacing ( 8 ) ,
55
61
display : 'flex' ,
56
62
flexDirection : 'column' ,
57
- alignItems : 'center' ,
63
+ alignItems : 'center'
58
64
} ,
59
65
avatar : {
60
66
margin : theme . spacing ( 1 ) ,
61
- backgroundColor : theme . palette . secondary . main ,
67
+ backgroundColor : theme . palette . secondary . main
62
68
} ,
63
69
form : {
64
70
width : '100%' , // Fix IE 11 issue.
65
- marginTop : theme . spacing ( 1 ) ,
71
+ marginTop : theme . spacing ( 1 )
66
72
} ,
67
73
submit : {
68
- margin : theme . spacing ( 3 , 0 , 2 ) ,
69
- } ,
74
+ margin : theme . spacing ( 3 , 0 , 2 )
75
+ }
70
76
} ) ) ;
71
77
72
- const SignIn : React . FC < LoginProps > = ( props ) => {
78
+ const SignIn : React . FC < LoginProps > = props => {
73
79
const classes = useStyles ( ) ;
74
80
//const count = useSelector(state => state);
75
81
76
82
const [ username , setUsername ] = useState ( '' ) ;
77
83
const [ password , setPassword ] = useState ( '' ) ;
78
84
79
- const handleChange = ( e ) => {
85
+ const history = useHistory ( ) ;
86
+
87
+ const handleChange = e => {
80
88
let inputVal = e . target . value ;
81
89
switch ( e . target . name ) {
82
90
case 'username' :
@@ -88,17 +96,33 @@ const SignIn: React.FC<LoginProps> = (props) => {
88
96
}
89
97
} ;
90
98
91
- const handleLogin = ( e ) => {
92
- console . log ( 'click fired on handleLogin' )
99
+ const handleLogin = e => {
100
+ console . log ( 'click fired on handleLogin' ) ;
93
101
e . preventDefault ( ) ;
94
- fetch ( '/login' , {
102
+ const body = JSON . stringify ( {
103
+ username,
104
+ password
105
+ } ) ;
106
+ console . log ( body ) ;
107
+ fetch ( 'http://localhost:8080/login' , {
95
108
method : 'POST' ,
96
- body : JSON . stringify ( { username, password } )
109
+ mode : 'cors' ,
110
+ headers : {
111
+ 'Content-Type' : 'application/json'
112
+ } ,
113
+ body
97
114
} )
98
- . then ( res => res . json ( ) )
99
- . then ( data => console . log ( data ) )
100
- . catch ( err => console . log ( err ) )
101
- }
115
+ . then ( res => res . json ( ) )
116
+ . then ( data => {
117
+ if ( typeof data === 'string' ) {
118
+ alert ( data ) ;
119
+ } else {
120
+ alert ( 'Login successful!' ) ;
121
+ props . history . push ( '/app' ) ;
122
+ }
123
+ } )
124
+ . catch ( err => console . log ( err ) ) ;
125
+ } ;
102
126
103
127
return (
104
128
< Container component = "main" maxWidth = "xs" >
@@ -110,66 +134,63 @@ const SignIn: React.FC<LoginProps> = (props) => {
110
134
< Typography component = "h1" variant = "h5" >
111
135
Sign in
112
136
</ Typography >
113
- < form className = { classes . form } noValidate >
114
- < TextField
115
- variant = "outlined"
116
- margin = "normal"
117
- required
118
- fullWidth
119
- id = "username"
120
- label = "Username"
121
- name = "username"
122
- autoComplete = "username"
123
- autoFocus
124
- value = { password }
125
- onChange = { handleChange }
126
- />
127
- < TextField
128
- variant = "outlined"
129
- margin = "normal"
130
- required
131
- fullWidth
132
- name = "password"
133
- label = "Password"
134
- type = "password"
135
- id = "password"
136
- autoComplete = "current-password"
137
- value = { password }
138
- onChange = { handleChange }
139
- />
140
- < FormControlLabel
141
- control = { < Checkbox value = "remember" color = "primary" /> }
142
- label = "Remember me"
143
- />
144
- < Button
145
- type = "submit"
146
- fullWidth
147
- variant = "contained"
148
- color = "primary"
149
- className = { classes . submit }
150
- onClick = { handleLogin }
151
- >
152
- Sign In
153
- </ Button >
154
- < Grid container >
155
- < Grid item xs >
156
- < Link href = "#" variant = "body2" >
157
- Forgot password?
158
- </ Link >
159
- </ Grid >
160
- < Grid item >
161
- < Link href = "#" variant = "body2" >
162
- { "Don't have an account? Sign Up" }
163
- </ Link >
164
- </ Grid >
137
+ < TextField
138
+ variant = "outlined"
139
+ margin = "normal"
140
+ required
141
+ fullWidth
142
+ id = "username"
143
+ label = "Username"
144
+ name = "username"
145
+ autoComplete = "username"
146
+ autoFocus
147
+ value = { username }
148
+ onChange = { handleChange }
149
+ />
150
+ < TextField
151
+ variant = "outlined"
152
+ margin = "normal"
153
+ required
154
+ fullWidth
155
+ name = "password"
156
+ label = "Password"
157
+ type = "password"
158
+ id = "password"
159
+ autoComplete = "current-password"
160
+ value = { password }
161
+ onChange = { handleChange }
162
+ />
163
+ < FormControlLabel
164
+ control = { < Checkbox value = "remember" color = "primary" /> }
165
+ label = "Remember me"
166
+ />
167
+
168
+ < Button
169
+ fullWidth
170
+ variant = "contained"
171
+ color = "primary"
172
+ className = { classes . submit }
173
+ onClick = { handleLogin }
174
+ >
175
+ Sign In
176
+ </ Button >
177
+
178
+ < Grid container >
179
+ < Grid item xs >
180
+ < Link href = "#" variant = "body2" >
181
+ Forgot password?
182
+ </ Link >
165
183
</ Grid >
166
- </ form >
184
+ < Grid item >
185
+ < RouteLink to = { `/signup` } > Don't have an account? Sign Up</ RouteLink >
186
+ </ Grid >
187
+ </ Grid >
167
188
</ div >
168
189
< Box mt = { 8 } >
169
190
< Copyright />
170
191
</ Box >
171
192
</ Container >
172
193
) ;
173
- }
194
+ } ;
174
195
175
- export default SignIn ;
196
+ export default withRouter ( SignIn ) ;
0 commit comments