From 54ea71bedf2bb80a4e7919a1c6e6ee375b2ee18a Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 25 Apr 2017 18:24:25 +0700 Subject: [PATCH] Support fragments in plugins --- .../src/components/Comment.js | 21 ++++++++++-- .../src/components/Embed.js | 4 +-- .../src/components/Stream.js | 6 +++- .../src/containers/Comment.js | 15 ++++++++- .../src/containers/Embed.js | 23 ++++++++----- .../src/containers/Stream.js | 6 ++-- client/coral-framework/helpers/plugins.js | 33 +++++++++++++++++++ .../client/containers/RespectButton.js | 22 ++++++++++++- 8 files changed, 111 insertions(+), 19 deletions(-) diff --git a/client/coral-embed-stream/src/components/Comment.js b/client/coral-embed-stream/src/components/Comment.js index c44e06282..b2289716a 100644 --- a/client/coral-embed-stream/src/components/Comment.js +++ b/client/coral-embed-stream/src/components/Comment.js @@ -174,8 +174,14 @@ class Comment extends React.Component { ? : null } - - + { (currentUser && (comment.user.id !== currentUser.id)) ? - +
@@ -257,6 +270,8 @@ class Comment extends React.Component { return commentIsIgnored(reply) ? : ; @@ -60,7 +60,7 @@ export default class Embed extends React.Component { } { loggedIn ? userBox : null } - + diff --git a/client/coral-embed-stream/src/components/Stream.js b/client/coral-embed-stream/src/components/Stream.js index d03dda571..cedd1cc1c 100644 --- a/client/coral-embed-stream/src/components/Stream.js +++ b/client/coral-embed-stream/src/components/Stream.js @@ -24,7 +24,7 @@ class Stream extends React.Component { render () { const { - data: {asset, asset: {comments}, comment, myIgnoredUsers}, + root: {asset, asset: {comments}, comment, myIgnoredUsers}, postItem, addNotification, postFlag, @@ -106,6 +106,8 @@ class Stream extends React.Component { { highlightedComment ? : pym.scrollParentToChildEl('coralStream'), 0); @@ -53,7 +53,7 @@ class EmbedContainer extends React.Component { } render() { - if (!this.props.data.asset) { + if (!this.props.root.asset) { return ; } return ; @@ -83,9 +83,14 @@ export const withQuery = graphql(EMBED_QUERY, { excludeIgnored: Boolean(auth && auth.user && auth.user.id), }, }), - props: ({data}) => ({ - data, - }) + props: ({data: { + fetchMore, loading, networkStatus, refetch, startPolling, + stopPolling, subscribeToMore, updateQuery, variables, + ...root}}) => ({ + data: {fetchMore, loading, networkStatus, refetch, startPolling, + stopPolling, subscribeToMore, updateQuery, variables}, + root, + }), }); const mapStateToProps = state => ({ diff --git a/client/coral-embed-stream/src/containers/Stream.js b/client/coral-embed-stream/src/containers/Stream.js index 838df1084..4f5018c2a 100644 --- a/client/coral-embed-stream/src/containers/Stream.js +++ b/client/coral-embed-stream/src/containers/Stream.js @@ -119,7 +119,7 @@ class StreamContainer extends React.Component { componentDidMount() { this.props.data.refetch(); this.countPoll = setInterval(() => { - const {asset} = this.props.data; + const {asset} = this.props.root; this.getCounts({ asset_id: asset.id, limit: asset.comments.length, @@ -219,7 +219,9 @@ const fragments = { me { status } + ...Comment_root } + ${Comment.fragments.root} ${Comment.fragments.comment} `, }; @@ -245,6 +247,7 @@ const mapDispatchToProps = dispatch => }, dispatch); export default compose( + withFragments(fragments), connect(mapStateToProps, mapDispatchToProps), postComment, postFlag, @@ -254,6 +257,5 @@ export default compose( removeCommentTag, ignoreUser, deleteAction, - withFragments(fragments), )(StreamContainer); diff --git a/client/coral-framework/helpers/plugins.js b/client/coral-framework/helpers/plugins.js index e0d345db4..02f97b101 100644 --- a/client/coral-framework/helpers/plugins.js +++ b/client/coral-framework/helpers/plugins.js @@ -1,7 +1,10 @@ import React from 'react'; import merge from 'lodash/merge'; import flatten from 'lodash/flatten'; +import flattenDeep from 'lodash/flattenDeep'; +import uniq from 'lodash/uniq'; import plugins from 'pluginsConfig'; +import {gql} from 'react-apollo'; export const pluginReducers = merge( ...plugins @@ -19,3 +22,33 @@ export function getSlotElements(slot, props = {}) { return components .map((component, i) => React.createElement(component, {...props, key: i})); } + +function getComponentFragments(components) { + return components + .map(c => c.fragments) + .filter(fragments => fragments) + .reduce((res, fragments) => { + Object.keys(fragments).forEach(key => { + if (!(key in res)) { + res[key] = {names: '', definitions: ''}; + } + res[key].names += `...${fragments[key].definitions[0].name.value}\n`; + res[key].definitions = gql`${res[key].definitions}${fragments[key]}`; + }); + return res; + }, {}); +} + +export function getSlotsFragments(slots) { + if (!Array.isArray(slots)) { + slots = [slots]; + } + const components = uniq(flattenDeep(slots.map(slot => { + return plugins + .filter(o => o.module.slots[slot]) + .map(o => o.module.slots[slot]); + }))); + + return getComponentFragments(components); +} + diff --git a/plugins/coral-plugin-respect/client/containers/RespectButton.js b/plugins/coral-plugin-respect/client/containers/RespectButton.js index bcf0e342d..a69c62032 100644 --- a/plugins/coral-plugin-respect/client/containers/RespectButton.js +++ b/plugins/coral-plugin-respect/client/containers/RespectButton.js @@ -2,7 +2,7 @@ import {compose, gql, graphql} from 'react-apollo'; import {connect} from 'react-redux'; import {bindActionCreators} from 'redux'; import get from 'lodash/get'; - +import withFragments from 'coral-framework/hocs/withFragments'; import {showSignInDialog} from 'coral-framework/actions/auth'; import RespectButton from '../components/RespectButton'; @@ -155,6 +155,26 @@ const mapDispatchToProps = dispatch => bindActionCreators({showSignInDialog}, dispatch); const enhance = compose( + withFragments({ + root: gql` + fragment RespectButton_root on RootQuery { + me { + status + } + } + `, + comment: gql` + fragment RespectButton_comment on Comment { + action_summaries { + ... on RespectActionSummary { + count + current_user { + id + } + } + } + }`, + }), connect(null, mapDispatchToProps), withDeleteAction, withPostRespect,