1
1
import React , { Component , useState , useEffect } from 'react' ;
2
- //import { connect } from 'react';
3
2
import { LoginInt } from '../../interfaces/Interfaces' ;
4
3
import { setLoginState } from '../../actions/actionCreators' ;
5
4
import { useSelector , useDispatch } from 'react-redux' ;
6
- import { Link as RouteLink , withRouter , useHistory } from 'react-router-dom' ;
5
+ import { Link as RouteLink , withRouter , useHistory , RouteComponentProps } from 'react-router-dom' ;
7
6
8
7
import Avatar from '@material-ui/core/Avatar' ;
9
8
import Button from '@material-ui/core/Button' ;
@@ -18,28 +17,7 @@ import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
18
17
import Typography from '@material-ui/core/Typography' ;
19
18
import { makeStyles , withStyles } from '@material-ui/core/styles' ;
20
19
import Container from '@material-ui/core/Container' ;
21
- import { render } from 'enzyme' ;
22
-
23
- import Cookies from 'js-cookie' ;
24
20
import { sessionIsCreated } from '../../helperFunctions/auth' ;
25
- /*
26
- const mapStateToProps = (store: any) => ({
27
- username: store.credentials.username,
28
- password: store.credentials.password
29
- });
30
-
31
- const mapDispatchToProps = (dispatch: any) => ({
32
- setUsername: (username: string) => dispatch(setUsername(username)),
33
- setPassword: (password: string) => dispatch(setPassword(password))
34
- // login: (username: string, password: string) => dispatch(login(username, password)),
35
- // signup: (username: string, password: string) => dispatch(signup(username, password)),
36
- });
37
-
38
- interface LoginProps extends LoginInt {
39
- setUsername(username: string): void;
40
- setPassword(username: string): void;
41
- }
42
- */
43
21
44
22
function Copyright ( ) {
45
23
return (
@@ -74,25 +52,16 @@ const useStyles = makeStyles(theme => ({
74
52
}
75
53
} ) ) ;
76
54
77
- const SignIn : React . FC < LoginInt > = props => {
55
+ const SignIn : React . FC < LoginInt & RouteComponentProps > = props => {
78
56
const classes = useStyles ( ) ;
79
57
const s = useSelector ( state => state ) ;
80
- const history = useHistory ( ) ;
81
58
82
59
const dispatch = useDispatch ( ) ;
83
60
84
61
const [ username , setUsername ] = useState ( '' ) ;
85
62
const [ password , setPassword ] = useState ( '' ) ;
86
- console . log ( 'state on load: ' , s . auth )
87
-
88
- /*
89
- useEffect(() => {
90
- console.log('triggered')
91
- if(s.auth) history.push('/signup'); // push to the main app
92
- }, []);
93
- */
94
63
95
- const handleChange = e => {
64
+ const handleChange = ( e : React . ChangeEvent < HTMLInputElement > ) => {
96
65
let inputVal = e . target . value ;
97
66
switch ( e . target . name ) {
98
67
case 'username' :
@@ -104,47 +73,18 @@ const SignIn: React.FC<LoginInt> = props => {
104
73
}
105
74
} ;
106
75
107
- const handleLogin = e => {
76
+ const handleLogin = ( e : React . MouseEvent < HTMLButtonElement , MouseEvent > ) => {
108
77
e . preventDefault ( ) ;
109
78
console . log ( 'click fired on handleLogin' ) ;
110
79
sessionIsCreated ( username , password ) . then ( isLoggedIn => {
111
80
if ( isLoggedIn ) {
112
81
console . log ( 'session created' )
113
82
dispatch ( setLoginState ( ) ) ; // changes login state to true
114
- props . history . push ( '/signup ' ) ;
83
+ props . history . push ( '/' ) ;
115
84
} else {
116
85
console . log ( 'invalid login' )
117
86
}
118
87
} ) ;
119
- /*
120
- const body = JSON.stringify({
121
- username,
122
- password
123
- });
124
- fetch('http://localhost:8080/login', {
125
- method: 'POST',
126
- credentials: 'include',
127
- headers: {
128
- 'Content-Type': 'application/json'
129
- },
130
- body
131
- })
132
- .then(res => {
133
- return res.json();
134
- })
135
- .then(data => {
136
- console.log('the data', data);
137
- if (data.sessionId && typeof data.sessionId === 'string') {
138
- //alert(data); // these alerts cause the app to crash on mac
139
- console.log('success')
140
- dispatch(setLoginState()); // changes login state to true
141
- props.history.push('/signup');
142
- } else {
143
- console.log('err');
144
- }
145
- })
146
- .catch(err => console.log(err));
147
- */
148
88
} ;
149
89
150
90
return (
@@ -193,7 +133,7 @@ const SignIn: React.FC<LoginInt> = props => {
193
133
variant = "contained"
194
134
color = "primary"
195
135
className = { classes . submit }
196
- onClick = { handleLogin }
136
+ onClick = { e => handleLogin ( e ) }
197
137
>
198
138
Sign In
199
139
</ Button >
0 commit comments