Created
July 20, 2016 08:20
-
-
Save hgenru/457d50f2350b309944cb3bb58952b9fb to your computer and use it in GitHub Desktop.
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
import React from 'react'; | |
import {connect} from 'react-redux'; | |
import {Link} from 'react-router'; | |
import {fetchCurrentUser} from '../../common/ducks/index'; | |
function stateToProps(state) { | |
return { | |
user: state.$user | |
}; | |
} | |
function actionsToProps(dispatch) { | |
return { | |
fetchCurrentUser: () => dispatch(fetchCurrentUser()) | |
}; | |
} | |
export default (Component) => { | |
class $$User extends React.Component { | |
componentWillMount() { | |
this.requestUser(); | |
} | |
requestUser() { | |
if (!this.props.user) { | |
this.props.fetchCurrentUser(); | |
} | |
} | |
render() { | |
let loginLink = ( | |
<div>You should <Link to="/login">loggin in</Link></div>); | |
return ( | |
this.props.user ? | |
<Component {...this.props}/> : loginLink | |
); | |
} | |
} | |
return connect(stateToProps, actionsToProps)($$User); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment