From 5cb9fdccf95e353a8329feb9cea1e85439bde028 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 6 Apr 2017 19:26:29 +0700 Subject: [PATCH] Refactor plugin --- .../coral-plugin-respect/client/actions.js | 6 - .../client/components/RespectButton.js | 155 +----------------- plugins/coral-plugin-respect/client/index.js | 7 +- .../coral-plugin-respect/client/reducer.js | 15 -- 4 files changed, 3 insertions(+), 180 deletions(-) delete mode 100644 plugins/coral-plugin-respect/client/actions.js delete mode 100644 plugins/coral-plugin-respect/client/reducer.js diff --git a/plugins/coral-plugin-respect/client/actions.js b/plugins/coral-plugin-respect/client/actions.js deleted file mode 100644 index 9af883182..000000000 --- a/plugins/coral-plugin-respect/client/actions.js +++ /dev/null @@ -1,6 +0,0 @@ -const buttonClicked = () => ({type: 'BUTTON_CLICKED'}); - -export const clickButton = () => dispatch => { - dispatch(buttonClicked()); -}; - diff --git a/plugins/coral-plugin-respect/client/components/RespectButton.js b/plugins/coral-plugin-respect/client/components/RespectButton.js index 10a13ae93..91aa19686 100644 --- a/plugins/coral-plugin-respect/client/components/RespectButton.js +++ b/plugins/coral-plugin-respect/client/components/RespectButton.js @@ -2,19 +2,13 @@ import React from 'react'; import styles from './style.css'; import Icon from './Icon'; -import {compose, gql, graphql} from 'react-apollo'; -import {connect} from 'react-redux'; -import {bindActionCreators} from 'redux'; import {I18n} from 'coral-framework'; -import get from 'lodash/get'; import cn from 'classnames'; import translations from '../translations.json'; const lang = new I18n(translations); -import {showSignInDialog} from 'coral-framework/actions/auth'; - -class RespectButton extends React.Component { +export default class RespectButton extends React.Component { static slot = 'Comment.Detail'; @@ -67,150 +61,3 @@ class RespectButton extends React.Component { } } -const COMMENT_QUERY = gql` - query CommentQuery($commentId: ID!) { - comment(id: $commentId) { - id - action_summaries { - ... on RespectActionSummary { - count - current_user { - id - } - } - } - } - me { - status - } - } -`; - -const withQuery = graphql(COMMENT_QUERY); - -const withDeleteAction = graphql(gql` - mutation deleteAction($id: ID!) { - deleteAction(id:$id) { - errors { - translation_key - } - } - } -`, { - options: (props) => ({ - refetchQueries: [ - { - query: COMMENT_QUERY, - variables: { - commentId: props.commentId, - }, - }, - ], - }), - props: ({mutate}) => ({ - deleteAction: (id) => { - return mutate({ - variables: {id}, - optimisticResponse: { - deleteAction: { - __typename: 'DeleteActionResponse', - errors: null, - } - }, - updateQueries: { - CommentQuery: (prev) => { - if (get(prev, 'comment.action_summaries.0.current_user.id') !== id) { - return prev; - } - const next = { - ...prev, - comment: { - ...prev.comment, - action_summaries: [{ - __typename: 'RespectActionSummary', - count: prev.comment.action_summaries[0].count - 1, - current_user: null, - }], - } - }; - return next; - }, - }, - }, - ); - }}), -}); - -const withPostRespect = graphql(gql` - mutation createRespect($respect: CreateRespectInput!) { - createRespect(respect: $respect) { - respect { - id - } - errors { - translation_key - } - } - } -`, { - options: (props) => ({ - refetchQueries: [ - { - query: COMMENT_QUERY, - variables: { - commentId: props.commentId, - }, - }, - ], - }), - props: ({mutate}) => ({ - postRespect: (respect) => { - return mutate({ - variables: {respect}, - optimisticResponse: { - createRespect: { - __typename: 'CreateRespectResponse', - errors: null, - respect: { - __typename: 'RespectAction', - id: 'pending', - }, - } - }, - updateQueries: { - CommentQuery: (prev, {mutationResult, queryVariables}) => { - if (queryVariables.commentId !== respect.item_id) { - return prev; - } - const respectAction = mutationResult.data.createRespect.respect; - const count = prev.action_summaries ? prev.action_summaries.count : 0; - const next = { - ...prev, - comment: { - ...prev.comment, - action_summaries: [{ - __typename: 'RespectActionSummary', - count: count + 1, - current_user: respectAction, - }], - } - }; - return next; - }, - }, - }); - }}) -}); - -const mapDispatchToProps = dispatch => - bindActionCreators({showSignInDialog}, dispatch); - -const enhance = compose( - connect(null, mapDispatchToProps), - withDeleteAction, - withPostRespect, - withQuery, -); - -export default enhance(RespectButton); - diff --git a/plugins/coral-plugin-respect/client/index.js b/plugins/coral-plugin-respect/client/index.js index 7a8d54535..f3d710513 100644 --- a/plugins/coral-plugin-respect/client/index.js +++ b/plugins/coral-plugin-respect/client/index.js @@ -1,7 +1,4 @@ -import RespectButton from './components/RespectButton'; -import * as reducer from './reducer'; - +import RespectButton from './containers/RespectButton'; export default { - components: [RespectButton], - reducer, + components: [RespectButton] }; diff --git a/plugins/coral-plugin-respect/client/reducer.js b/plugins/coral-plugin-respect/client/reducer.js deleted file mode 100644 index aa7bbda5e..000000000 --- a/plugins/coral-plugin-respect/client/reducer.js +++ /dev/null @@ -1,15 +0,0 @@ -import {Map} from 'immutable'; - -const initialState = Map({ - clicked: false -}); - -export function respect (state = initialState, action) { - switch (action.type) { - case 'BUTTON_CLICKED' : - return state - .set('clicked', !state.get('clicked')); - default: - return state; - } -}