From 404dd6214c80bf8097a15590890ea9f37f4d14b2 Mon Sep 17 00:00:00 2001 From: Sacha Greif Date: Tue, 29 Nov 2016 18:54:37 +0900 Subject: [PATCH] work on withCurrentUser --- .../framework-demo/lib/components/App.jsx | 81 ------------------- packages/nova-core/lib/components/App.jsx | 36 +-------- .../lib/containers/withCurrentUser-old.js | 30 +++++++ .../lib/containers/withCurrentUser.js | 52 ++++++------ packages/nova-core/package.js | 3 +- packages/nova-email/package.js | 2 +- packages/nova-users/lib/client.js | 4 +- .../lib/containers/withCurrentUser.js | 38 +++++++++ packages/nova-users/lib/modules.js | 3 +- packages/nova-users/lib/server.js | 4 +- packages/nova-users/package.js | 2 +- 11 files changed, 107 insertions(+), 148 deletions(-) delete mode 100644 packages/framework-demo/lib/components/App.jsx create mode 100644 packages/nova-core/lib/containers/withCurrentUser-old.js create mode 100644 packages/nova-users/lib/containers/withCurrentUser.js diff --git a/packages/framework-demo/lib/components/App.jsx b/packages/framework-demo/lib/components/App.jsx deleted file mode 100644 index 27c34336f..000000000 --- a/packages/framework-demo/lib/components/App.jsx +++ /dev/null @@ -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 ( - - { - this.props.loading ? - : -
{this.props.children}
- } -
- ) - } - -} - -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); \ No newline at end of file diff --git a/packages/nova-core/lib/components/App.jsx b/packages/nova-core/lib/components/App.jsx index 140b4beb4..29d36383b 100644 --- a/packages/nova-core/lib/components/App.jsx +++ b/packages/nova-core/lib/components/App.jsx @@ -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; \ No newline at end of file diff --git a/packages/nova-core/lib/containers/withCurrentUser-old.js b/packages/nova-core/lib/containers/withCurrentUser-old.js new file mode 100644 index 000000000..e0bb6d450 --- /dev/null +++ b/packages/nova-core/lib/containers/withCurrentUser-old.js @@ -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 ? : ; +// } +// } + +// WithCurrentUser.contextTypes = { client: PropTypes.object.isRequired }; +// WithCurrentUser.displayName = `withCurrentUser(${Telescope.utils.getComponentDisplayName(WrappedComponent)}`; +// WithCurrentUser.WrappedComponent = WrappedComponent; + +// return hoistStatics(WithCurrentUser, WrappedComponent); +// } \ No newline at end of file diff --git a/packages/nova-core/lib/containers/withCurrentUser.js b/packages/nova-core/lib/containers/withCurrentUser.js index 7d50ecf14..c43ccc76d 100644 --- a/packages/nova-core/lib/containers/withCurrentUser.js +++ b/packages/nova-core/lib/containers/withCurrentUser.js @@ -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 ? : ; + `, { + 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); -} \ No newline at end of file +export default withCurrentUser; \ No newline at end of file diff --git a/packages/nova-core/package.js b/packages/nova-core/package.js index 5be906bff..7cbb2de41 100644 --- a/packages/nova-core/package.js +++ b/packages/nova-core/package.js @@ -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([ diff --git a/packages/nova-email/package.js b/packages/nova-email/package.js index cb192939f..902ce50e4 100644 --- a/packages/nova-email/package.js +++ b/packages/nova-email/package.js @@ -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"); diff --git a/packages/nova-users/lib/client.js b/packages/nova-users/lib/client.js index c5c5a1452..32600f821 100644 --- a/packages/nova-users/lib/client.js +++ b/packages/nova-users/lib/client.js @@ -1,3 +1 @@ -import Users from './modules.js'; - -export default Users; \ No newline at end of file +export * from './modules.js'; \ No newline at end of file diff --git a/packages/nova-users/lib/containers/withCurrentUser.js b/packages/nova-users/lib/containers/withCurrentUser.js new file mode 100644 index 000000000..268785583 --- /dev/null +++ b/packages/nova-users/lib/containers/withCurrentUser.js @@ -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; \ No newline at end of file diff --git a/packages/nova-users/lib/modules.js b/packages/nova-users/lib/modules.js index d76b942ce..03acae0ca 100644 --- a/packages/nova-users/lib/modules.js +++ b/packages/nova-users/lib/modules.js @@ -7,4 +7,5 @@ import './emails.js'; import './avatar.js'; import './permissions.js'; -export default Users; \ No newline at end of file +export default Users; +// export { default as withCurrentUser } from './containers/withCurrentUser.js'; diff --git a/packages/nova-users/lib/server.js b/packages/nova-users/lib/server.js index e8eee2aa0..41b544f9d 100644 --- a/packages/nova-users/lib/server.js +++ b/packages/nova-users/lib/server.js @@ -1,6 +1,4 @@ -import Users from './modules.js'; - import './server/create_user.js'; import './server/urls.js'; -export default Users; \ No newline at end of file +export * from './modules.js'; \ No newline at end of file diff --git a/packages/nova-users/package.js b/packages/nova-users/package.js index ea9a70a8f..298789ec6 100644 --- a/packages/nova-users/package.js +++ b/packages/nova-users/package.js @@ -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' ]);