2016-11-15 18:33:16 +01:00
|
|
|
import React from 'react';
|
|
|
|
import hoistStatics from 'hoist-non-react-statics';
|
|
|
|
import { Meteor } from 'meteor/meteor';
|
|
|
|
import Telescope from 'meteor/nova:lib';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
|
|
|
|
**/
|
|
|
|
export default function withCurrentUser(WrappedComponent) {
|
|
|
|
|
|
|
|
const WithCurrentUser = (props, context) => {
|
|
|
|
|
2016-11-16 11:02:19 +01:00
|
|
|
const {client} = context; // grab the apollo client from the context
|
2016-11-15 18:33:16 +01:00
|
|
|
|
|
|
|
const currentUser = client ? client.store.getState().apollo.data[`User${Meteor.userId()}`] : null;
|
|
|
|
|
2016-11-16 11:02:19 +01:00
|
|
|
return currentUser ? <WrappedComponent currentUser={currentUser} {...props} /> : <WrappedComponent {...props} />;
|
2016-11-15 18:33:16 +01:00
|
|
|
}
|
|
|
|
|
2016-11-16 11:02:19 +01:00
|
|
|
WithCurrentUser.contextTypes = { client: PropTypes.object.isRequired };
|
2016-11-15 18:33:16 +01:00
|
|
|
WithCurrentUser.displayName = `withCurrentUser(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`;
|
|
|
|
WithCurrentUser.WrappedComponent = WrappedComponent;
|
|
|
|
|
2016-11-16 11:02:19 +01:00
|
|
|
return hoistStatics(WithCurrentUser, WrappedComponent);
|
2016-11-15 18:33:16 +01:00
|
|
|
}
|