split out withCurrentUser and loadCurrentUser for now

This commit is contained in:
Sacha Greif 2016-11-30 10:50:04 +09:00
parent 25f685c06c
commit d5be97b56c
6 changed files with 65 additions and 42 deletions

View file

@ -50,7 +50,8 @@ PostsCommentsThread.propTypes = {
currentUser: React.PropTypes.object
};
const fragment = gql`
PostsCommentsThread.fragmentName = 'commentsListFragment';
PostsCommentsThread.fragment = gql`
fragment commentsListFragment on Comment {
_id
postId
@ -71,17 +72,8 @@ const fragment = gql`
const options = {
collection: Comments,
queryName: 'commentsListQuery',
fragmentName: 'commentsListFragment',
fragment: fragment,
limit: 0,
options: {
variables: {
postId: {
type: 'String',
usedForTotal: true
}
}
},
fragmentName: PostsCommentsThread.fragmentName,
fragment: PostsCommentsThread.fragment,
};
Telescope.registerComponent('PostsCommentsThread', PostsCommentsThread, withCurrentUser, withList(options));

View file

@ -1,7 +1,7 @@
import Telescope from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
import { IntlProvider, intlShape} from 'react-intl';
import { withCurrentUser } from 'meteor/nova:users';
import { loadCurrentUser } from 'meteor/nova:users';
class App extends Component {
@ -43,6 +43,6 @@ App.childContextTypes = {
intl: intlShape,
}
Telescope.registerComponent('App', App, withCurrentUser);
Telescope.registerComponent('App', App, loadCurrentUser);
export default App;

View file

@ -0,0 +1,32 @@
import Telescope from 'meteor/nova:lib';
import Users from '../collection.js';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
const loadCurrentUser = component => {
const preloadedFields = _.compact(_.map(Users.simpleSchema()._schema, (field, fieldName) => {
return field.preload ? fieldName : undefined;
}));
// console.log('preloaded fields', preloadedFields);
return graphql(
gql`query getCurrentUser {
currentUser {
${preloadedFields.join('\n')}
}
}
`, {
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
)(component);
};
export default loadCurrentUser;

View file

@ -1,32 +1,30 @@
import React, { Component, PropTypes } from 'react';
import hoistStatics from 'hoist-non-react-statics';
import { Meteor } from 'meteor/meteor';
import Telescope from 'meteor/nova:lib';
import Users from '../collection.js';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
const withCurrentUser = component => {
/**
* withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
**/
export default function withCurrentUser(WrappedComponent) {
const preloadedFields = _.compact(_.map(Users.simpleSchema()._schema, (field, fieldName) => {
return field.preload ? fieldName : undefined;
}));
// console.log('preloaded fields', preloadedFields);
return graphql(
gql`query getCurrentUser {
currentUser {
${preloadedFields.join('\n')}
}
class WithCurrentUser extends Component {
constructor(...args) {
super(...args);
}
`, {
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
)(component);
};
export default withCurrentUser;
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} />;
}
}
WithCurrentUser.contextTypes = { client: PropTypes.object.isRequired };
WithCurrentUser.displayName = `withCurrentUser(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`;
WithCurrentUser.WrappedComponent = WrappedComponent;
return hoistStatics(WithCurrentUser, WrappedComponent);
}

View file

@ -9,3 +9,4 @@ import './permissions.js';
export default Users;
export { default as withCurrentUser } from './containers/withCurrentUser.js';
export { default as loadCurrentUser } from './containers/loadCurrentUser.js';