Skip to content

Commit 1355209

Browse files
committed
refactored signin component to include ts types
1 parent e5d3f67 commit 1355209

File tree

3 files changed

+8
-91
lines changed

3 files changed

+8
-91
lines changed

src/components/login/SignIn.tsx

Lines changed: 6 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import React, { Component, useState, useEffect } from 'react';
2-
//import { connect } from 'react';
32
import { LoginInt } from '../../interfaces/Interfaces';
43
import { setLoginState } from '../../actions/actionCreators';
54
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';
76

87
import Avatar from '@material-ui/core/Avatar';
98
import Button from '@material-ui/core/Button';
@@ -18,28 +17,7 @@ import LockOutlinedIcon from '@material-ui/icons/LockOutlined';
1817
import Typography from '@material-ui/core/Typography';
1918
import { makeStyles, withStyles } from '@material-ui/core/styles';
2019
import Container from '@material-ui/core/Container';
21-
import { render } from 'enzyme';
22-
23-
import Cookies from 'js-cookie';
2420
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-
*/
4321

4422
function Copyright() {
4523
return (
@@ -74,25 +52,16 @@ const useStyles = makeStyles(theme => ({
7452
}
7553
}));
7654

77-
const SignIn: React.FC<LoginInt> = props => {
55+
const SignIn: React.FC<LoginInt & RouteComponentProps> = props => {
7856
const classes = useStyles();
7957
const s = useSelector(state => state);
80-
const history = useHistory();
8158

8259
const dispatch = useDispatch();
8360

8461
const [username, setUsername] = useState('');
8562
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-
*/
9463

95-
const handleChange = e => {
64+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
9665
let inputVal = e.target.value;
9766
switch (e.target.name) {
9867
case 'username':
@@ -104,47 +73,18 @@ const SignIn: React.FC<LoginInt> = props => {
10473
}
10574
};
10675

107-
const handleLogin = e => {
76+
const handleLogin = (e: React.MouseEvent<HTMLButtonElement, MouseEvent>) => {
10877
e.preventDefault();
10978
console.log('click fired on handleLogin');
11079
sessionIsCreated(username, password).then(isLoggedIn => {
11180
if(isLoggedIn) {
11281
console.log('session created')
11382
dispatch(setLoginState()); // changes login state to true
114-
props.history.push('/signup');
83+
props.history.push('/');
11584
} else {
11685
console.log('invalid login')
11786
}
11887
});
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-
*/
14888
};
14989

15090
return (
@@ -193,7 +133,7 @@ const SignIn: React.FC<LoginInt> = props => {
193133
variant="contained"
194134
color="primary"
195135
className={classes.submit}
196-
onClick={handleLogin}
136+
onClick={e => handleLogin(e)}
197137
>
198138
Sign In
199139
</Button>

src/helperFunctions/auth.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
import Cookies from 'js-cookie';
2-
3-
// check for a cookie when and redirect to main app when first loaded
4-
export const hasValidSession = () => {
5-
// Cookies.get('ssid');
6-
}
7-
81
export const sessionIsCreated = (username: string, password: string): Promise<boolean> => {
92
const body = JSON.stringify({
103
username,

src/index.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ReactDOM from 'react-dom';
44
import { Provider } from 'react-redux';
55
import App from './components/App.tsx';
66
import store from './store';
7+
import Cookies from 'js-cookie';
78

89
import SignIn from './components/login/SignIn.tsx';
910
import SignUp from './components/login/SignUp.tsx';
@@ -13,24 +14,9 @@ import {
1314
Redirect,
1415
Switch } from 'react-router-dom';
1516

16-
/*
17-
ReactDOM.render(
18-
<Provider store={store}>
19-
<App />
20-
</Provider>,
21-
document.getElementById('app'),
22-
);
23-
*/
24-
25-
/*
26-
The first file that loads
27-
If cookie is valid, send the user to app and set the login boolean to true
28-
If the cookie has expired, send the user back to login
29-
*/
30-
3117
const PrivateRoute = ({ component: Component, ...rest }) => (
3218
<Route {...rest} render={(props) => (
33-
false
19+
Cookies.get('ssid')
3420
? <Component {...props} />
3521
: <Redirect to='/login' />
3622
)} />
@@ -40,10 +26,8 @@ ReactDOM.render(
4026
<Provider store={store}>
4127
<Router>
4228
<Switch>
43-
{/* change route to signin later for official release */}
4429
<Route exact path="/login" component={SignIn} />
4530
<Route exact path="/signup" component={SignUp} />
46-
{/* <Route exact path="/app" component={App} /> */}
4731
<PrivateRoute path="/" component={App} />
4832
</Switch>
4933
</Router>

0 commit comments

Comments
 (0)