Skip to content

Commit b076953

Browse files
committed
Update profile pages
1 parent fb2b5b4 commit b076953

File tree

16 files changed

+191
-59
lines changed

16 files changed

+191
-59
lines changed

src/@types/user.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface IUser {
1717
username: string;
1818
email?: string;
1919
password: string;
20+
accessToken?: string;
2021
}
2122

2223
interface DispatchAuth {

src/App/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ const _App = (props: Props) => {
1515
useEffect(() => {
1616
const { loadUser, logout } = props;
1717
// check for token in LS
18-
if (localStorage.token) {
18+
if (localStorage.user) {
1919
loadUser();
2020
}
2121

2222
// log user out from all tabs if they log out in one tab
2323
window.addEventListener('storage', () => {
24-
if (!localStorage.token) logout();
24+
if (!localStorage.user) logout();
2525
});
2626
}, []);
2727
return <Routes />;

src/assets/scss/_footer.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
padding: 20px 0 !important;
55
text-align: center;
66
height: $heightFooter;
7-
position: absolute;
8-
left: 0;
9-
bottom: 0;
10-
right: 0;
7+
// position: absolute;
8+
// left: 0;
9+
// bottom: 0;
10+
// right: 0;
1111
}
1212
.footer .footer-logo {
1313
font-size: 1.2rem;

src/assets/scss/_header.scss

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.ant-layout-header {
2-
position: fixed;
2+
position: relative;
33
left: 0;
44
right: 0;
55
z-index: 9;
@@ -13,22 +13,22 @@
1313
.navbar-section {
1414
border-bottom: solid 1px #e8e8e8;
1515
overflow: auto;
16-
box-shadow: 0 0 30px #f3f1f1;
16+
box-shadow: 0 0 2px #f3f1f1;
1717
}
1818
.navbar {
1919
display: flex;
2020
align-items: center;
21-
padding: 0 10px;
21+
padding: 0;
2222

2323
.ant-menu-item {
24-
padding: 0px 5px;
24+
padding: 0px 2px;
2525
}
2626
.ant-menu-submenu-title {
27-
padding: 10px 20px;
27+
padding: 4px;
2828
}
2929
.ant-menu-item a,
3030
.ant-menu-submenu-title a {
31-
padding: 10px 15px;
31+
padding: 10px 0px;
3232
}
3333
.ant-menu-horizontal {
3434
border-bottom: none;

src/assets/scss/index.scss

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ img {
4747
.ant-layout-content {
4848
background-color: #fff;
4949
min-height: calc(100vh - #{$heightFooter} - #{$heightHeader}) !important;
50-
padding-bottom: 2rem;
5150
overflow: auto;
5251
}
5352
.ant-layout.main-layout {

src/components/Auth/Auth.reducers.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ let userType: IUser = {
55
username: '',
66
email: undefined,
77
password: '',
8+
accessToken: '',
89
};
910
const initialState = {
1011
loading: true,
1112
isAuthenticated: false,
12-
token: localStorage.getItem('token'),
13+
token: null,
1314
user: userType,
1415
};
1516

@@ -22,22 +23,30 @@ export const authReducer = (state = initialState, action: ActionRedux) => {
2223
isAuthenticated: true,
2324
loading: false,
2425
token: payload.id,
25-
user: payload.user,
26+
user: payload,
2627
};
2728
case types.LOGIN_SUCCESS:
2829
case types.REGISTER_SUCCESS:
29-
localStorage.setItem('token', payload.id);
30+
localStorage.setItem('user', JSON.stringify(payload));
3031
return {
3132
...state,
3233
...payload,
3334
isAuthenticated: true,
3435
loading: false,
36+
user: payload,
3537
};
3638
case types.LOGIN_FAILED:
3739
case types.AUTH_ERROR:
38-
case types.LOGOUT:
3940
case types.REGISTER_FAILED:
40-
localStorage.removeItem('token');
41+
localStorage.removeItem('user');
42+
return {
43+
...state,
44+
token: null,
45+
isAuthenticated: false,
46+
loading: false,
47+
};
48+
case types.LOGOUT:
49+
localStorage.removeItem('user');
4150
return {
4251
...state,
4352
token: null,

src/components/Auth/Auth.thunks.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,64 @@ import * as actions from './Auth.actions';
44
import { v4 as uuid } from 'uuid';
55

66
export const loadUser = () => async dispatch => {
7-
const tokenId = localStorage.getItem('token');
7+
const userJson = localStorage.getItem('user') || '{}';
8+
const user = JSON.parse(userJson) as IUser;
9+
const id = user.id;
810
console.log('Load user running!');
9-
if (!tokenId) {
10-
return dispatch(actions.authError);
11+
console.log('User from loadUser', user);
12+
if (!id) {
13+
return dispatch(actions.authError());
1114
}
1215
try {
13-
const res = await axios.get(`${URL.baseAPIUrl}/api/users/${tokenId}`);
16+
const res = await axios.get(`${URL.baseAPIUrl}/api/users/${id}`);
1417
console.log('Response', res);
1518
console.log('Hey loadUser function triggered');
1619
if (res) {
1720
return dispatch(actions.userLoaded(res.data));
1821
}
19-
dispatch(actions.authError);
22+
return dispatch(actions.authError());
2023
} catch (error) {
2124
console.log('Some error loadUser running', error);
22-
return dispatch(actions.authError);
25+
return dispatch(actions.authError());
2326
}
2427
};
2528

2629
export const login = (payload: ReqLogin) => async dispatch => {
2730
const { username, password } = payload;
31+
console.log('Login triggered');
2832
try {
2933
const res = await axios.get(`${URL.baseAPIUrl}/api/users`);
3034
const allUsers = res.data;
3135

3236
let user = allUsers.filter(x => x.username === username)[0];
33-
console.log('user', user);
37+
console.log('user found from login', user);
3438
if (user && user.password === password) {
3539
dispatch(actions.loginSuccess(user));
3640
dispatch(loadUser());
3741
return;
38-
} else {
39-
console.log('Invalid credentials');
40-
return dispatch(actions.loginFailed);
4142
}
43+
return dispatch(actions.loginFailed());
4244
} catch (error) {
43-
return dispatch(actions.loginFailed);
45+
return dispatch(actions.loginFailed());
4446
}
4547
};
4648

4749
export const register = (payload: ReqLogin) => async dispatch => {
50+
console.log('Register triggered');
4851
try {
4952
const id = uuid();
50-
const newUser = { id, ...payload };
53+
const accessToken = id;
54+
55+
const newUser = { ...payload, id, accessToken };
5156
await axios.post(`${URL.baseAPIUrl}/api/users`, newUser);
5257
dispatch(actions.registerSuccess(newUser));
5358
console.log('Resgister success', newUser);
54-
const all = await axios.get(`${URL.baseAPIUrl}/api/users`);
55-
console.log('All users', all);
5659
dispatch(loadUser());
5760
} catch (error) {
58-
return dispatch(actions.registerFailed);
61+
return dispatch(actions.registerFailed());
5962
}
6063
};
6164
export const logout = () => async dispatch => {
62-
return dispatch(actions.logoutSuccess);
65+
console.log('Logout triggered');
66+
return dispatch(actions.logoutSuccess());
6367
};

src/components/Auth/Login.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState } from 'react';
22
import { Form, Input, Button, Checkbox, message } from 'antd';
33
import { UserOutlined, LockOutlined } from '@ant-design/icons';
44
import { connect, ConnectedProps } from 'react-redux';
5-
import { useHistory, Link } from 'react-router-dom';
5+
import { useHistory, Link, Redirect } from 'react-router-dom';
66
import { login } from './Auth.thunks';
77
import { PATH } from 'src/constants/paths';
88

@@ -18,22 +18,22 @@ interface Props extends ConnectedProps<typeof connector> {}
1818
const _Login = (props: Props) => {
1919
// eslint-disable-next-line
2020
const [error, setError] = useState('');
21-
const { login } = props;
22-
const history = useHistory();
21+
const { login, isAuthenticated } = props;
2322

2423
const onFinish = async formData => {
2524
const { username, password } = formData;
2625
const payload = { username, password };
2726
try {
2827
await login(payload);
29-
message.success('Login successfully');
30-
history.push(PATH.HOME);
3128
} catch (error) {
3229
message.error(error.message);
3330
setError(error.payload.message);
3431
}
3532
};
3633

34+
if (isAuthenticated) {
35+
return <Redirect to={PATH.HOME} />;
36+
}
3737
return (
3838
<div className="container">
3939
<div className="login-form-wrap">

src/components/Auth/Profile.tsx

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import React from 'react';
2+
import { connect, ConnectedProps } from 'react-redux';
3+
import { Card, Avatar, Row, Col } from 'antd';
4+
import {
5+
EditOutlined,
6+
EllipsisOutlined,
7+
SettingOutlined,
8+
} from '@ant-design/icons';
9+
const { Meta } = Card;
10+
11+
const mapStateToProps = (state: AppState) => ({
12+
user: state.auth.user,
13+
});
14+
15+
const mapDispatchToProps = {};
16+
17+
const connector = connect(mapStateToProps, mapDispatchToProps);
18+
interface Props extends ConnectedProps<typeof connector> {}
19+
const _Profile = (props: Props) => {
20+
const { user } = props;
21+
return (
22+
<div className="mt-1">
23+
<Row>
24+
<Col span={8} offset={10}>
25+
<Card
26+
style={{ width: 350 }}
27+
cover={
28+
<img
29+
alt="example"
30+
src="https://gw.alipayobjects.com/zos/rmsportal/JiqGstEfoWAOHiTxclqi.png"
31+
/>
32+
}
33+
actions={[
34+
<SettingOutlined key="setting" />,
35+
<EditOutlined key="edit" />,
36+
<EllipsisOutlined key="ellipsis" />,
37+
]}
38+
>
39+
<Meta
40+
avatar={
41+
<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />
42+
}
43+
title={user.username}
44+
description="User profile"
45+
></Meta>
46+
<div className="additional">
47+
<p></p>
48+
<Row>
49+
<Col offset={4}>
50+
<small>Email: </small>
51+
</Col>
52+
53+
<Col offset={4}>
54+
<strong>{user.email}</strong>
55+
</Col>
56+
</Row>
57+
<Row>
58+
<Col offset={4}>
59+
<small>Password: </small>
60+
</Col>
61+
62+
<Col offset={2}>
63+
<strong>{user.password}</strong>
64+
</Col>
65+
</Row>
66+
</div>
67+
</Card>
68+
</Col>
69+
</Row>
70+
,
71+
</div>
72+
);
73+
};
74+
75+
export const Profile = connector(_Profile);

src/components/Auth/Register.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useState } from 'react';
22
import { Form, Input, Button, message } from 'antd';
33
import { UserOutlined, LockOutlined, MailOutlined } from '@ant-design/icons';
44
import { connect, ConnectedProps } from 'react-redux';
5-
import { useHistory, Link } from 'react-router-dom';
5+
import { useHistory, Link, Redirect } from 'react-router-dom';
66
import { register } from './Auth.thunks';
77
import { PATH } from 'src/constants/paths';
88

@@ -18,21 +18,23 @@ interface Props extends ConnectedProps<typeof connector> {}
1818
const _Register = (props: Props) => {
1919
// eslint-disable-next-line
2020
const [error, setError] = useState('');
21-
const { register } = props;
21+
const { register, isAuthenticated } = props;
2222
const history = useHistory();
2323

2424
const onFinish = async formData => {
2525
try {
2626
await register(formData);
27-
message.success('Register successfully');
28-
history.push(PATH.HOME);
2927
} catch (error) {
3028
setError(error.payload.message);
3129
}
3230
};
3331
const onFinishFailed = errorInfo => {
3432
console.log('Failed:', errorInfo);
3533
};
34+
35+
if (isAuthenticated) {
36+
return <Redirect to={PATH.HOME} />;
37+
}
3638
return (
3739
<div className="container">
3840
<div className="login-form-wrap">

0 commit comments

Comments
 (0)