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