From 6652368221b993da66052ef7bfdac0e8ece9cbb6 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 18 Jul 2017 21:04:30 +0700 Subject: [PATCH] Don't export internals of embed stream in plugin-api (for now) --- .../coral-embed-stream/src/graphql/utils.js | 29 +++---------------- client/coral-framework/utils/index.js | 22 ++++++++++++++ plugin-api/beta/client/actions/stream.js | 1 - plugin-api/beta/client/utils/index.js | 1 + plugin-api/beta/client/utils/stream.js | 1 - .../client/containers/TabPane.js | 2 +- .../client/index.js | 5 ++-- 7 files changed, 31 insertions(+), 30 deletions(-) delete mode 100644 plugin-api/beta/client/actions/stream.js create mode 100644 plugin-api/beta/client/utils/index.js delete mode 100644 plugin-api/beta/client/utils/stream.js diff --git a/client/coral-embed-stream/src/graphql/utils.js b/client/coral-embed-stream/src/graphql/utils.js index 5738785fb..4f51b0ae2 100644 --- a/client/coral-embed-stream/src/graphql/utils.js +++ b/client/coral-embed-stream/src/graphql/utils.js @@ -1,4 +1,6 @@ import update from 'immutability-helper'; +import {insertCommentsSorted} from 'coral-framework/utils'; + function determineCommentDepth(comment) { let depth = 0; let cur = comment; @@ -142,27 +144,6 @@ export function findCommentInEmbedQuery(root, callbackOrId) { return findComment(root.asset.comments.nodes, callback); } -const ascending = (a, b) => { - const dateA = new Date(a.created_at); - const dateB = new Date(b.created_at); - if (dateA < dateB) { return -1; } - if (dateA > dateB) { return 1; } - return 0; -}; - -const descending = (a, b) => ascending(a, b) * -1; - -export function insertCommentSorted(nodes, comment, sortOrder = 'CHRONOLOGICAL') { - const added = nodes.concat(comment); - if (sortOrder === 'CHRONOLOGICAL') { - return added.sort(ascending); - } - if (sortOrder === 'REVERSE_CHRONOLOGICAL') { - return added.sort(descending); - } - throw new Error(`Unknown sort order ${sortOrder}`); -} - function findAndInsertFetchedComments(parent, comments, parent_id) { const isAsset = parent.__typename === 'Asset'; const connectionField = isAsset ? 'comments' : 'replies'; @@ -175,11 +156,9 @@ function findAndInsertFetchedComments(parent, comments, parent_id) { if (isAsset) { return nodes.concat(comments.nodes); } - return nodes - .concat(comments.nodes.filter( + return insertCommentsSorted(nodes, comments.nodes.filter( (comment) => !nodes.some((node) => node.id === comment.id) - )) - .sort(ascending); + )); }}, }, }); diff --git a/client/coral-framework/utils/index.js b/client/coral-framework/utils/index.js index 1866005be..bad551482 100644 --- a/client/coral-framework/utils/index.js +++ b/client/coral-framework/utils/index.js @@ -144,3 +144,25 @@ export function forEachError(error, callback) { callback({error: e, msg}); }); } + +const ascending = (a, b) => { + const dateA = new Date(a.created_at); + const dateB = new Date(b.created_at); + if (dateA < dateB) { return -1; } + if (dateA > dateB) { return 1; } + return 0; +}; + +const descending = (a, b) => ascending(a, b) * -1; + +export function insertCommentsSorted(nodes, comments, sortOrder = 'CHRONOLOGICAL') { + const added = nodes.concat(comments); + if (sortOrder === 'CHRONOLOGICAL') { + return added.sort(ascending); + } + if (sortOrder === 'REVERSE_CHRONOLOGICAL') { + return added.sort(descending); + } + throw new Error(`Unknown sort order ${sortOrder}`); +} + diff --git a/plugin-api/beta/client/actions/stream.js b/plugin-api/beta/client/actions/stream.js deleted file mode 100644 index 0ddb1ba13..000000000 --- a/plugin-api/beta/client/actions/stream.js +++ /dev/null @@ -1 +0,0 @@ -export {setActiveTab} from 'coral-embed-stream/src/actions/stream'; diff --git a/plugin-api/beta/client/utils/index.js b/plugin-api/beta/client/utils/index.js new file mode 100644 index 000000000..09a0ef6bb --- /dev/null +++ b/plugin-api/beta/client/utils/index.js @@ -0,0 +1 @@ +export {insertCommentsSorted} from 'coral-framework/utils'; diff --git a/plugin-api/beta/client/utils/stream.js b/plugin-api/beta/client/utils/stream.js deleted file mode 100644 index dbaa302a9..000000000 --- a/plugin-api/beta/client/utils/stream.js +++ /dev/null @@ -1 +0,0 @@ -export {findCommentInEmbedQuery, insertSorted} from 'coral-embed-stream/src/graphql/utils'; diff --git a/plugins/talk-plugin-featured-comments/client/containers/TabPane.js b/plugins/talk-plugin-featured-comments/client/containers/TabPane.js index 2c33fb5b9..54d23f82c 100644 --- a/plugins/talk-plugin-featured-comments/client/containers/TabPane.js +++ b/plugins/talk-plugin-featured-comments/client/containers/TabPane.js @@ -3,7 +3,7 @@ import {bindActionCreators} from 'redux'; import {compose, gql} from 'react-apollo'; import TabPane from '../components/TabPane'; import {withFragments} from 'plugin-api/beta/client/hocs'; -import {setActiveTab} from 'plugin-api/beta/client/actions/stream'; +import {setActiveTab} from 'coral-embed-stream/src/actions/stream'; const mapDispatchToProps = (dispatch) => bindActionCreators({ diff --git a/plugins/talk-plugin-featured-comments/client/index.js b/plugins/talk-plugin-featured-comments/client/index.js index 1c5d2cb82..6d3191374 100644 --- a/plugins/talk-plugin-featured-comments/client/index.js +++ b/plugins/talk-plugin-featured-comments/client/index.js @@ -5,7 +5,8 @@ import FeaturedButton from './components/FeaturedButton'; import translations from './translations.json'; import update from 'immutability-helper'; -import {findCommentInEmbedQuery, insertCommentSorted} from 'plugin-api/beta/client/utils/stream'; +import {findCommentInEmbedQuery} from 'coral-embed-stream/src/graphql/utils'; +import {insertCommentsSorted} from 'plugin-api/beta/client/utils'; export default { translations, @@ -51,7 +52,7 @@ export default { asset: { featuredComments: { nodes: { - $apply: (nodes) => insertCommentSorted(nodes, comment, 'REVERSE_CHRONOLOGICAL') + $apply: (nodes) => insertCommentsSorted(nodes, comment, 'REVERSE_CHRONOLOGICAL') } }, featuredCommentsCount: {