2016-12-21 12:04:43 +01:00
|
|
|
import React, { Component } from 'react';
|
2016-11-30 16:58:28 +09:00
|
|
|
import hoistStatics from 'hoist-non-react-statics';
|
2016-12-21 12:04:43 +01:00
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2016-12-12 11:34:28 +09:00
|
|
|
import { Utils } from 'meteor/nova:lib';
|
2016-12-21 12:04:43 +01:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-11-30 16:58:28 +09:00
|
|
|
|
2017-01-12 17:30:25 +09:00
|
|
|
const withCurrentUser1 = component => {
|
|
|
|
|
|
|
|
const preloadedFields = Users.getPreloadedFields();
|
|
|
|
|
|
|
|
return graphql(gql`
|
|
|
|
query getCurrentUser {
|
|
|
|
currentUser {
|
|
|
|
${preloadedFields.join('\n')}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
options(ownProps) {
|
|
|
|
return {
|
|
|
|
alias: 'withCurrentUser',
|
|
|
|
};
|
|
|
|
},
|
|
|
|
props(props) {
|
|
|
|
const {data: {loading, currentUser}} = props;
|
|
|
|
return {
|
|
|
|
loading,
|
|
|
|
currentUser,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)(component);
|
|
|
|
}
|
|
|
|
|
2016-11-30 16:58:28 +09:00
|
|
|
/**
|
2016-12-21 12:04:43 +01:00
|
|
|
* withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
|
|
|
|
**/
|
2017-01-12 17:30:25 +09:00
|
|
|
const withCurrentUser2 = WrappedComponent => {
|
2016-12-21 12:04:43 +01:00
|
|
|
|
|
|
|
class WithCurrentUser extends Component {
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
|
|
|
}
|
|
|
|
|
2016-12-23 12:01:16 +01:00
|
|
|
// uncomment for debugging/monitoring
|
|
|
|
// componentWillUnmount() {
|
|
|
|
// console.log('unmounting', Utils.getComponentDisplayName(WrappedComponent));
|
|
|
|
// }
|
2016-12-21 12:04:43 +01:00
|
|
|
|
|
|
|
render() {
|
|
|
|
|
2016-12-23 12:01:16 +01:00
|
|
|
const preloadedFields = Users.getPreloadedFields();
|
|
|
|
|
2016-12-21 12:04:43 +01:00
|
|
|
const ComponentWithData = graphql(
|
|
|
|
gql`query getCurrentUser {
|
|
|
|
currentUser {
|
2016-12-23 12:01:16 +01:00
|
|
|
${preloadedFields.join('\n')}
|
2016-12-21 12:04:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`, {
|
|
|
|
options(ownProps) {
|
|
|
|
return {
|
|
|
|
variables: {},
|
|
|
|
// pollInterval: 20000,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
props(props) {
|
|
|
|
const {data: {loading, currentUser}} = props;
|
|
|
|
return {
|
|
|
|
loading,
|
|
|
|
currentUser,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
)(WrappedComponent);
|
|
|
|
|
|
|
|
return <ComponentWithData {...this.props} />
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WithCurrentUser.displayName = `withCurrentUser(${Utils.getComponentDisplayName(WrappedComponent)})`
|
|
|
|
WithCurrentUser.WrappedComponent = WrappedComponent
|
|
|
|
|
|
|
|
return hoistStatics(WithCurrentUser, WrappedComponent);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
************************** old pattern without executing a query **************************
|
2016-11-30 16:58:28 +09:00
|
|
|
* withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
|
|
|
|
**/
|
2016-12-21 12:04:43 +01:00
|
|
|
|
|
|
|
function withCurrentUserWithoutQuery(WrappedComponent) {
|
2016-11-30 16:58:28 +09:00
|
|
|
|
2016-12-21 12:04:43 +01:00
|
|
|
class WithCurrentUser extends React.Component {
|
2016-11-30 16:58:28 +09:00
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
2016-12-14 13:17:39 +09:00
|
|
|
this.logCurrentUser = this.logCurrentUser.bind(this);
|
2016-11-30 16:58:28 +09:00
|
|
|
}
|
2016-12-14 13:17:39 +09:00
|
|
|
|
2016-11-30 16:58:28 +09:00
|
|
|
render() {
|
|
|
|
const {client} = this.context; // grab the apollo client from the context
|
|
|
|
|
|
|
|
const currentUser = client ? client.store.getState().apollo.data[`User${Meteor.userId()}`] : null;
|
|
|
|
|
|
|
|
return currentUser ? <WrappedComponent currentUser={currentUser} {...this.props} /> : <WrappedComponent {...this.props} />;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-21 12:04:43 +01:00
|
|
|
WithCurrentUser.contextTypes = { client: React.PropTypes.object.isRequired };
|
2016-12-12 11:34:28 +09:00
|
|
|
WithCurrentUser.displayName = `withCurrentUser(${Utils.getComponentDisplayName(WrappedComponent)}`;
|
2016-11-30 16:58:28 +09:00
|
|
|
WithCurrentUser.WrappedComponent = WrappedComponent;
|
|
|
|
|
|
|
|
return hoistStatics(WithCurrentUser, WrappedComponent);
|
2016-12-14 13:17:39 +09:00
|
|
|
}
|
2017-01-12 17:30:25 +09:00
|
|
|
|
|
|
|
export default withCurrentUser1;
|