From 8a619a219f9ad8b8db6407ac52238f1374f3b16a Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 18 Jul 2017 21:10:59 +0700 Subject: [PATCH] Refactor isFeatured / isTagged --- client/coral-framework/utils/index.js | 1 + plugin-api/beta/client/hocs/withTags.js | 7 ++----- plugin-api/beta/client/utils/index.js | 2 +- .../coral-plugin-offtopic/client/components/OffTopicTag.js | 5 ++--- .../client/components/FeaturedTag.js | 5 ++--- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/client/coral-framework/utils/index.js b/client/coral-framework/utils/index.js index bad551482..0917b73ba 100644 --- a/client/coral-framework/utils/index.js +++ b/client/coral-framework/utils/index.js @@ -166,3 +166,4 @@ export function insertCommentsSorted(nodes, comments, sortOrder = 'CHRONOLOGICAL throw new Error(`Unknown sort order ${sortOrder}`); } +export const isTagged = (tags, which) => tags.some((t) => t.tag.name === which); diff --git a/plugin-api/beta/client/hocs/withTags.js b/plugin-api/beta/client/hocs/withTags.js index 3a50dc87e..bf559024a 100644 --- a/plugin-api/beta/client/hocs/withTags.js +++ b/plugin-api/beta/client/hocs/withTags.js @@ -7,7 +7,7 @@ import {capitalize} from 'coral-framework/helpers/strings'; import withMutation from 'coral-framework/hocs/withMutation'; import withFragments from 'coral-framework/hocs/withFragments'; import {addNotification} from 'coral-framework/actions/notification'; -import {forEachError} from 'coral-framework/utils'; +import {forEachError, isTagged} from 'coral-framework/utils'; export default (tag) => (WrappedComponent) => { if (typeof tag !== 'string') { @@ -28,9 +28,6 @@ export default (tag) => (WrappedComponent) => { } `; - const isTagged = (tags) => - !!tags.filter((t) => t.tag.name === Tag.toUpperCase()).length; - const withAddTag = withMutation( gql` mutation AddTag($id: ID!, $asset_id: ID!, $name: String!) { @@ -163,7 +160,7 @@ export default (tag) => (WrappedComponent) => { render() { const {comment} = this.props; - const alreadyTagged = isTagged(comment.tags); + const alreadyTagged = isTagged(comment.tags, Tag.toUpperCase()); return !!tags.filter((t) => t.tag.name === 'OFF_TOPIC').length; +import {isTagged} from 'plugin-api/beta/client/utils'; export default (props) => ( { - isOffTopic(props.comment.tags) && props.depth === 0 ? ( + isTagged(props.comment.tags, 'OFF_TOPIC') && props.depth === 0 ? ( {t('off_topic')} diff --git a/plugins/talk-plugin-featured-comments/client/components/FeaturedTag.js b/plugins/talk-plugin-featured-comments/client/components/FeaturedTag.js index ef1758c32..6cd236370 100644 --- a/plugins/talk-plugin-featured-comments/client/components/FeaturedTag.js +++ b/plugins/talk-plugin-featured-comments/client/components/FeaturedTag.js @@ -1,13 +1,12 @@ import React from 'react'; import styles from './FeaturedTag.css'; import {t} from 'plugin-api/beta/client/services'; - -const isFeatured = (tags) => !!tags.filter((t) => t.tag.name === 'FEATURED').length; +import {isTagged} from 'plugin-api/beta/client/utils'; export default (props) => ( { - isFeatured(props.comment.tags) && props.depth === 0 ? ( + isTagged(props.comment.tags, 'FEATURED') && props.depth === 0 ? ( {t('featured')}