Skip to content

Commit 99b9463

Browse files
authored
Merge pull request #3147 from PiyushChandra17/piyush/refactor-createRedirectWithUsername
refactor createRedirectWithUserName cleanup version
2 parents 934fb07 + 6af4649 commit 99b9463

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
import React from 'react';
2-
import { connect } from 'react-redux';
3-
import browserHistory from '../browserHistory';
1+
import { useEffect } from 'react';
2+
import { useSelector } from 'react-redux';
3+
import { useHistory } from 'react-router-dom';
4+
import PropTypes from 'prop-types';
45

5-
const RedirectToUser = ({ username, url = '/:username/sketches' }) => {
6-
React.useEffect(() => {
7-
if (username == null) {
8-
return;
6+
const RedirectToUser = ({ url = '/:username/sketches' }) => {
7+
const history = useHistory();
8+
const username = useSelector((state) =>
9+
state.user ? state.user.username : null
10+
);
11+
useEffect(() => {
12+
if (username) {
13+
history.replace(url.replace(':username', username));
914
}
10-
11-
browserHistory.replace(url.replace(':username', username));
12-
}, [username]);
15+
}, [history, url, username]);
1316

1417
return null;
1518
};
1619

17-
function mapStateToProps(state) {
18-
return {
19-
username: state.user ? state.user.username : null
20-
};
21-
}
22-
23-
const ConnectedRedirectToUser = connect(mapStateToProps)(RedirectToUser);
24-
25-
const createRedirectWithUsername = (url) => (props) => (
26-
<ConnectedRedirectToUser {...props} url={url} />
27-
);
20+
RedirectToUser.propTypes = {
21+
url: PropTypes.string.isRequired
22+
};
2823

29-
export default createRedirectWithUsername;
24+
export default RedirectToUser;

client/routes.jsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import NewPasswordView from './modules/User/pages/NewPasswordView';
1717
import AccountView from './modules/User/pages/AccountView';
1818
import CollectionView from './modules/User/pages/CollectionView';
1919
import DashboardView from './modules/User/pages/DashboardView';
20-
import createRedirectWithUsername from './components/createRedirectWithUsername';
20+
import RedirectToUser from './components/createRedirectWithUsername';
2121
import { getUser } from './modules/User/actions';
2222
import {
2323
userIsAuthenticated,
@@ -84,11 +84,11 @@ const routes = (
8484

8585
<Route
8686
path="/sketches"
87-
component={createRedirectWithUsername('/:username/sketches')}
87+
component={() => <RedirectToUser url="/:username/sketches" />}
8888
/>
8989
<Route
9090
path="/assets"
91-
component={createRedirectWithUsername('/:username/assets')}
91+
component={() => <RedirectToUser url="/:username/assets" />}
9292
/>
9393
<Route path="/account" component={userIsAuthenticated(AccountView)} />
9494
<Route path="/about" component={IDEView} />

0 commit comments

Comments
 (0)