Skip to content

Commit fb2b5b4

Browse files
committed
Fix error dispatch actions auths
1 parent 1e10316 commit fb2b5b4

File tree

20 files changed

+111
-129
lines changed

20 files changed

+111
-129
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
"@types/styled-components": "^5.1.4",
2020
"antd": "^4.9.2",
2121
"concurrently": "^5.3.0",
22-
"immer": "^8.0.0",
2322
"json-server": "^0.16.3",
2423
"react": "^17.0.1",
2524
"react-dom": "^17.0.1",

server/db/db.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
"username": "alan",
1818
"email": "alan@gmail.com",
1919
"password": "123456"
20+
},
21+
{
22+
"id": "38550566-e1c6-40c2-a616-8ea74189f677",
23+
"username": "felix",
24+
"email": "felix@gmail.com",
25+
"password": "123456"
2026
}
2127
],
2228
"products": []

src/@types/actions.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
interface ActionRedux {
2-
types: string;
2+
type: string;
33
payload?: any;
44
}

src/@types/user.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,8 @@ interface IUser {
1818
email?: string;
1919
password: string;
2020
}
21+
22+
interface DispatchAuth {
23+
type: string;
24+
payload?: any;
25+
}

src/App/App.actions.ts

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

src/App/App.constants.ts

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

src/App/App.reducer.ts

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

src/App/App.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,30 @@
11
import React, { useEffect } from 'react';
2+
import { connect, ConnectedProps } from 'react-redux';
23
import { loadUser, logout } from 'src/components/Auth/Auth.thunks';
34
import { Routes } from 'src/routes';
45

5-
function App() {
6+
const mapStateToProps = () => ({});
7+
const mapDispatchToProps = {
8+
loadUser,
9+
logout,
10+
};
11+
const connector = connect(mapStateToProps, mapDispatchToProps);
12+
interface Props extends ConnectedProps<typeof connector> {}
13+
14+
const _App = (props: Props) => {
15+
useEffect(() => {
16+
const { loadUser, logout } = props;
17+
// check for token in LS
18+
if (localStorage.token) {
19+
loadUser();
20+
}
21+
22+
// log user out from all tabs if they log out in one tab
23+
window.addEventListener('storage', () => {
24+
if (!localStorage.token) logout();
25+
});
26+
}, []);
627
return <Routes />;
7-
}
28+
};
829

9-
export default App;
30+
export const App = connector(_App);

src/components/Auth/Auth.actions.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import * as types from './Auth.constants';
22

3-
export const loginRequested = () => ({
4-
type: types.LOGIN_REQUESTED,
5-
});
6-
73
export const loginSuccess = (payload: IUser) => ({
84
type: types.LOGIN_SUCCESS,
95
payload,
@@ -22,7 +18,7 @@ export const authError = () => ({
2218
type: types.AUTH_ERROR,
2319
});
2420

25-
export const logout = () => ({
21+
export const logoutSuccess = () => ({
2622
type: types.LOGOUT,
2723
});
2824

src/components/Auth/Auth.constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export const LOGIN_SUCCESS = 'views/auth/LOGIN_SUCCESS';
22
export const LOGIN_FAILED = 'views/auth/LOGIN_FAILED';
3-
export const LOGIN_REQUESTED = 'views/auth/LOGIN_REQUESTED';
43
export const USER_LOADED = 'views/auth/USER_LOADED';
54
export const AUTH_ERROR = 'views/auth/AUTH_ERROR';
65
export const LOGOUT = 'views/auth/LOGOUT';

0 commit comments

Comments
 (0)