Skip to content

Instantly share code, notes, and snippets.

@hgenru
Created July 20, 2016 08:20
Show Gist options
  • Save hgenru/457d50f2350b309944cb3bb58952b9fb to your computer and use it in GitHub Desktop.
Save hgenru/457d50f2350b309944cb3bb58952b9fb to your computer and use it in GitHub Desktop.
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