-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3147 from PiyushChandra17/piyush/refactor-createR…
…edirectWithUsername refactor createRedirectWithUserName cleanup version
- Loading branch information
Showing
2 changed files
with
20 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters