mirror of
https://github.com/vale981/Vulcan
synced 2025-03-06 10:01:40 -05:00
split out withCurrentUser and loadCurrentUser for now
This commit is contained in:
parent
25f685c06c
commit
d5be97b56c
6 changed files with 65 additions and 42 deletions
|
@ -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));
|
||||
|
|
|
@ -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;
|
32
packages/nova-users/lib/containers/loadCurrentUser.js
Normal file
32
packages/nova-users/lib/containers/loadCurrentUser.js
Normal 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;
|
|
@ -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;
|
||||
}));
|
||||
class WithCurrentUser extends Component {
|
||||
constructor(...args) {
|
||||
super(...args);
|
||||
}
|
||||
|
||||
// console.log('preloaded fields', preloadedFields);
|
||||
render() {
|
||||
const {client} = this.context; // grab the apollo client from the context
|
||||
|
||||
return graphql(
|
||||
gql`query getCurrentUser {
|
||||
currentUser {
|
||||
${preloadedFields.join('\n')}
|
||||
const currentUser = client ? client.store.getState().apollo.data[`User${Meteor.userId()}`] : null;
|
||||
|
||||
return currentUser ? <WrappedComponent currentUser={currentUser} {...this.props} /> : <WrappedComponent {...this.props} />;
|
||||
}
|
||||
}
|
||||
`, {
|
||||
props(props) {
|
||||
const {data: {loading, currentUser}} = props;
|
||||
return {
|
||||
loading,
|
||||
currentUser,
|
||||
};
|
||||
},
|
||||
}
|
||||
)(component);
|
||||
};
|
||||
|
||||
export default withCurrentUser;
|
||||
WithCurrentUser.contextTypes = { client: PropTypes.object.isRequired };
|
||||
WithCurrentUser.displayName = `withCurrentUser(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`;
|
||||
WithCurrentUser.WrappedComponent = WrappedComponent;
|
||||
|
||||
return hoistStatics(WithCurrentUser, WrappedComponent);
|
||||
}
|
|
@ -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';
|
||||
|
|
Loading…
Add table
Reference in a new issue