Skip to content

refactor createRedirectWithUserName cleanup version #3147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading