Skip to content

Commit f108e3e

Browse files
committed
adding global login state
1 parent 74b0855 commit f108e3e

File tree

9 files changed

+33
-102
lines changed

9 files changed

+33
-102
lines changed

src/actionTypes/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,4 @@ export const UPDATE_CHILDREN_SORT: string = 'UPDATE_CHILDREN_SORT';
4141
export const UPDATE_CODE: string = 'UPDATE_CODE';
4242
export const UPDATE_HTML_ATTR: string = 'UPDATE_HTML_ATTR';
4343

44-
export const SET_USERNAME: string = 'SET_USERNAME';
45-
export const SET_PASSWORD: string = 'SET_PASSWORD';
46-
export const LOGIN: string = 'LOGIN';
47-
export const SIGNUP: string = 'SIGNUP';
44+
export const SET_LOGIN_STATE: string = 'SET_LOGIN_STATE';

src/actions/actionCreators.ts

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ import {
3939
UNDO,
4040
UPDATE_HTML_ATTR,
4141
UPDATE_CODE,
42-
SET_USERNAME,
43-
SET_PASSWORD,
44-
LOGIN,
45-
SIGNUP
42+
SET_LOGIN_STATE
4643
} from '../actionTypes/index';
4744
import { loadState } from '../localStorage';
4845
import createFiles from '../utils/createFiles.util';
@@ -333,28 +330,6 @@ export const updateHtmlAttr = ({
333330
payload: { attr, value }
334331
});
335332

336-
export const setUsername = (username: string): Action => ({
337-
type: SET_USERNAME,
338-
payload: username
339-
});
340-
341-
export const setPassword = (password: string): Action => ({
342-
type: SET_PASSWORD,
343-
payload: password
344-
})
345-
346-
// export const login = (username: string, password: string) => ({
347-
// return (dispatch: any) => {
348-
// fetch('/login', {
349-
// method: 'POST',
350-
// body: JSON.stringify({ username, password })
351-
// })
352-
// .then(response => response.json())
353-
// .then(data => console.log(data))
354-
// }
355-
// })
356-
357-
// export const signup = (username: string, password: string): Action => ({
358-
// type: SIGNUP,
359-
// payload: { username, password }
360-
// })
333+
export const setLoginState = (): Action => ({
334+
type: SET_LOGIN_STATE
335+
})

src/components/login/SignIn.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React, { Component, useState } from 'react';
22
//import { connect } from 'react';
33
import { LoginInt } from '../../interfaces/Interfaces';
4-
import { setUsername, setPassword } from '../../actions/actionCreators';
5-
//import { useSelector } from 'react-redux';
4+
import { setLoginState } from '../../actions/actionCreators';
5+
import { useSelector, useDispatch } from 'react-redux';
66
import { Link as RouteLink, withRouter, useHistory } from 'react-router-dom';
77

88
import Avatar from '@material-ui/core/Avatar';
@@ -20,6 +20,7 @@ import { makeStyles, withStyles } from '@material-ui/core/styles';
2020
import Container from '@material-ui/core/Container';
2121
import { render } from 'enzyme';
2222

23+
/*
2324
const mapStateToProps = (store: any) => ({
2425
username: store.credentials.username,
2526
password: store.credentials.password
@@ -36,6 +37,7 @@ interface LoginProps extends LoginInt {
3637
setUsername(username: string): void;
3738
setPassword(username: string): void;
3839
}
40+
*/
3941

4042
function Copyright() {
4143
return (
@@ -70,15 +72,18 @@ const useStyles = makeStyles(theme => ({
7072
}
7173
}));
7274

73-
const SignIn: React.FC<LoginProps> = props => {
75+
const SignIn: React.FC<LoginInt> = props => {
7476
const classes = useStyles();
75-
//const count = useSelector(state => state);
77+
const s = useSelector(state => state);
78+
const dispatch = useDispatch();
7679

7780
const [username, setUsername] = useState('');
7881
const [password, setPassword] = useState('');
7982

8083
const history = useHistory();
8184

85+
console.log('state on load: ', s.auth)
86+
8287
const handleChange = e => {
8388
let inputVal = e.target.value;
8489
switch (e.target.name) {
@@ -93,6 +98,9 @@ const SignIn: React.FC<LoginProps> = props => {
9398

9499
const handleLogin = e => {
95100
console.log('click fired on handleLogin');
101+
console.log('state: ', s);
102+
dispatch(setLoginState());
103+
/*
96104
e.preventDefault();
97105
const body = JSON.stringify({
98106
username,
@@ -119,6 +127,7 @@ const SignIn: React.FC<LoginProps> = props => {
119127
}
120128
})
121129
.catch(err => console.log(err));
130+
*/
122131
};
123132

124133
return (

src/containers/LoginContainer.tsx

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ ReactDOM.render(
1818
);
1919
*/
2020

21+
/*
22+
The first file that loads
23+
If cookie is valid, send the user to app and set the login boolean to true
24+
If the cookie has expired, send the user back to login
25+
*/
26+
2127
ReactDOM.render(
2228
<Provider store={store}>
2329
<Router>
2430
<Switch>
2531
{/* change route to signin later for official release */}
26-
<Route exact path="/" component={App} />
32+
<Route exact path="/" component={SignIn} />
2733
<Route exact path="/signup" component={SignUp} />
2834
<Route exact path="/app" component={App} />
35+
{/* <PrivateRoute path='/protected' component={Protected} /> */}
2936
</Switch>
3037
</Router>
3138
</Provider>,

src/interfaces/Interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,7 @@ export interface ApplicationStateInt {
8585
}
8686

8787
export interface LoginInt {
88-
username: string;
89-
password: string;
88+
isLoggedIn: boolean;
9089
}
9190

9291
//Global Action interface \

src/reducers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import loginReducer from './loginReducer';
55

66
const reducers = combineReducers({
77
workspace: componentReducer,
8-
credentials: loginReducer
8+
auth: loginReducer
99
});
1010

1111
export default reducers;

src/reducers/initialState.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ export const initialComponentState: ComponentInt = {
112112
};
113113

114114
export const initialLoginState: LoginInt = {
115-
username: '',
116-
password: ''
115+
isLoggedIn: false
117116
};
118117

119118
export const nativeComponentTypes = [

src/reducers/loginReducer.ts

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,13 @@
11
import { initialLoginState } from './initialState';
22
import { Action } from '../interfaces/Interfaces';
3-
import { SET_USERNAME, SET_PASSWORD } from '../actionTypes/index';
4-
/*
5-
export interface LoginInt {
6-
username: string;
7-
password: string;
8-
}
9-
10-
export const initialLoginState: LoginInt = {
11-
username: '',
12-
password: ''
13-
};
14-
15-
export interface Action {
16-
type: string;
17-
payload?: any;
18-
}
19-
20-
export const setUsername = (username: string): Action => ({
21-
type: SET_USERNAME,
22-
payload: username
23-
});
24-
25-
export const setPassword = (password: string): Action => ({
26-
type: SET_PASSWORD,
27-
payload: password
28-
})
29-
*/
3+
import { SET_LOGIN_STATE } from '../actionTypes/index';
304

315
const loginReducer = (state = initialLoginState, action: Action) => {
326
switch(action.type) {
33-
case SET_USERNAME:
34-
return {
35-
...state,
36-
username: action.payload
37-
}
38-
39-
case SET_PASSWORD:
7+
case SET_LOGIN_STATE:
8+
const status = !state.isLoggedIn
409
return {
41-
...state,
42-
password: action.payload
10+
isLoggedIn: status
4311
}
4412

4513
default:

0 commit comments

Comments
 (0)