work on withCurrentUser

This commit is contained in:
Sacha Greif 2016-11-29 18:54:37 +09:00 committed by xavcz
parent 7b4e64e97f
commit 404dd6214c
11 changed files with 107 additions and 148 deletions

View file

@ -1,81 +0,0 @@
import Telescope from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
import { IntlProvider, intlShape} from 'react-intl';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import Layout from './Layout.jsx';
class App extends Component {
getLocale() {
return Telescope.settings.get("locale", "en");
}
getChildContext() {
const messages = Telescope.strings[this.getLocale()] || {};
const intlProvider = new IntlProvider({locale: this.getLocale()}, messages);
const { intl } = intlProvider.getChildContext();
return { intl };
}
render() {
return (
<IntlProvider locale={this.getLocale()} messages={Telescope.strings[this.getLocale()]}>
{
this.props.loading ?
<Telescope.components.Loading /> :
<div>{this.props.children}</div>
}
</IntlProvider>
)
}
}
App.propTypes = {
loading: React.PropTypes.bool,
currentUser: React.PropTypes.object,
actions: React.PropTypes.object,
}
App.childContextTypes = {
currentUser: React.PropTypes.object,
actions: React.PropTypes.object,
intl: intlShape,
}
// we are not "forced" to use the containers helpers to run specific queries like `getCurrentUser` which doesn't take any argument
const currentUserContainer = graphql(
gql`query getCurrentUser {
currentUser {
_id
username
createdAt
isAdmin
__bio
__displayName
__email
__emailHash
__groups
__htmlBio
__karma
__slug
__twitterUsername
__website
}
}
`, {
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
);
Telescope.registerComponent('App', App, currentUserContainer);

View file

@ -1,8 +1,7 @@
import Telescope from 'meteor/nova:lib';
import React, { PropTypes, Component } from 'react';
import { IntlProvider, intlShape} from 'react-intl';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
import withCurrentUser from '../containers/withCurrentUser.js';
class App extends Component {
@ -44,37 +43,6 @@ App.childContextTypes = {
intl: intlShape,
}
// we are not "forced" to use the containers helpers to run specific queries like `getCurrentUser` which doesn't take any argument
const currentUserContainer = graphql(
gql`query getCurrentUser {
currentUser {
_id
username
createdAt
isAdmin
__bio
__displayName
__email
__emailHash
__groups
__htmlBio
__karma
__slug
__twitterUsername
__website
}
}
`, {
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
);
Telescope.registerComponent('App', App, currentUserContainer);
Telescope.registerComponent('App', App, withCurrentUser);
export default App;

View file

@ -0,0 +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';
// /**
// * withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
// **/
// export default function withCurrentUser(WrappedComponent) {
// class WithCurrentUser extends Component {
// constructor(...args) {
// super(...args);
// }
// 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

@ -1,30 +1,36 @@
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 'meteor/nova:users';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
/**
* withCurrentUser - HOC to give access to the currentUser as a prop of a WrappedComponent
**/
export default function withCurrentUser(WrappedComponent) {
// we are not "forced" to use the containers helpers to run specific queries like `getCurrentUser` which doesn't take any argument
const withCurrentUser = options => {
class WithCurrentUser extends Component {
constructor(...args) {
super(...args);
const preloadedFields = _.compact(_.map(Users.simpleSchema()._schema, (field, fieldName) => {
return field.preload ? fieldName : undefined;
}));
return graphql(
gql`query getCurrentUser {
currentUser {
_id
username
createdAt
${preloadedFields.join('\n')}
}
}
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} />;
`, {
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
}
)
};
WithCurrentUser.contextTypes = { client: PropTypes.object.isRequired };
WithCurrentUser.displayName = `withCurrentUser(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`;
WithCurrentUser.WrappedComponent = WrappedComponent;
// Telescope.replaceComponent('App', Telescope.components.App, withCurrentUser);
return hoistStatics(WithCurrentUser, WrappedComponent);
}
export default withCurrentUser;

View file

@ -10,7 +10,8 @@ Package.onUse(function(api) {
api.versionsFrom("METEOR@1.0");
api.use([
'nova:lib@0.27.4-nova'
'nova:lib@0.27.4-nova',
'nova:users@0.27.4-nova',
]);
api.imply([

View file

@ -10,7 +10,7 @@ Package.onUse(function (api) {
api.versionsFrom(['METEOR@1.0']);
api.use([
'nova:core@0.27.4-nova'
'nova:lib@0.27.4-nova'
]);
api.mainModule("lib/server.js", "server");

View file

@ -1,3 +1 @@
import Users from './modules.js';
export default Users;
export * from './modules.js';

View file

@ -0,0 +1,38 @@
import Telescope from 'meteor/nova:lib';
import Users from './collection.js';
import { graphql } from 'react-apollo';
import gql from 'graphql-tag';
// we are not "forced" to use the containers helpers to run specific queries like `getCurrentUser` which doesn't take any argument
const withCurrentUser = options => {
const preloadedFields = _.compact(_.map(Users.simpleSchema()._schema, (field, fieldName) => {
return field.preload ? fieldName : undefined;
}));
console.log(preloadedFields)
return graphql(
gql`query getCurrentUser {
currentUser {
_id
username
createdAt
isAdmin
${preloadedFields.join('\n')}
}
}
`, {
props(props) {
const {data: {loading, currentUser}} = props;
return {
loading,
currentUser,
};
},
}
)
};
Telescope.replaceComponent('App', Telescope.components.App, withCurrentUser);
export default withCurrentUser;

View file

@ -7,4 +7,5 @@ import './emails.js';
import './avatar.js';
import './permissions.js';
export default Users;
export default Users;
// export { default as withCurrentUser } from './containers/withCurrentUser.js';

View file

@ -1,6 +1,4 @@
import Users from './modules.js';
import './server/create_user.js';
import './server/urls.js';
export default Users;
export * from './modules.js';

View file

@ -10,7 +10,7 @@ Package.onUse(function (api) {
api.versionsFrom(['METEOR@1.0']);
api.use([
'nova:core@0.27.4-nova',
'nova:lib@0.27.4-nova',
'nova:email@0.27.4-nova'
]);