Skip to content

Commit

Permalink
Merge pull request #3147 from PiyushChandra17/piyush/refactor-createR…
Browse files Browse the repository at this point in the history
…edirectWithUsername

refactor createRedirectWithUserName cleanup version
  • Loading branch information
raclim authored Jun 13, 2024
2 parents 934fb07 + 6af4649 commit 99b9463
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
39 changes: 17 additions & 22 deletions client/components/createRedirectWithUsername.jsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
import React from 'react';
import { connect } from 'react-redux';
import browserHistory from '../browserHistory';
import { useEffect } from 'react';
import { useSelector } from 'react-redux';
import { useHistory } from 'react-router-dom';
import PropTypes from 'prop-types';

const RedirectToUser = ({ username, url = '/:username/sketches' }) => {
React.useEffect(() => {
if (username == null) {
return;
const RedirectToUser = ({ url = '/:username/sketches' }) => {
const history = useHistory();
const username = useSelector((state) =>
state.user ? state.user.username : null
);
useEffect(() => {
if (username) {
history.replace(url.replace(':username', username));
}

browserHistory.replace(url.replace(':username', username));
}, [username]);
}, [history, url, username]);

return null;
};

function mapStateToProps(state) {
return {
username: state.user ? state.user.username : null
};
}

const ConnectedRedirectToUser = connect(mapStateToProps)(RedirectToUser);

const createRedirectWithUsername = (url) => (props) => (
<ConnectedRedirectToUser {...props} url={url} />
);
RedirectToUser.propTypes = {
url: PropTypes.string.isRequired
};

export default createRedirectWithUsername;
export default RedirectToUser;
6 changes: 3 additions & 3 deletions client/routes.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import NewPasswordView from './modules/User/pages/NewPasswordView';
import AccountView from './modules/User/pages/AccountView';
import CollectionView from './modules/User/pages/CollectionView';
import DashboardView from './modules/User/pages/DashboardView';
import createRedirectWithUsername from './components/createRedirectWithUsername';
import RedirectToUser from './components/createRedirectWithUsername';
import { getUser } from './modules/User/actions';
import {
userIsAuthenticated,
Expand Down Expand Up @@ -84,11 +84,11 @@ const routes = (

<Route
path="/sketches"
component={createRedirectWithUsername('/:username/sketches')}
component={() => <RedirectToUser url="/:username/sketches" />}
/>
<Route
path="/assets"
component={createRedirectWithUsername('/:username/assets')}
component={() => <RedirectToUser url="/:username/assets" />}
/>
<Route path="/account" component={userIsAuthenticated(AccountView)} />
<Route path="/about" component={IDEView} />
Expand Down

0 comments on commit 99b9463

Please sign in to comment.