From baf5a3400199cc409bf1bf681a067d36bb0bc5f0 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Thu, 6 Apr 2017 00:36:58 +0700 Subject: [PATCH] Fully working redux+apollo powered plugin --- .../client/components/RespectButton.js | 155 +++++++++++++----- .../client/mutations/index.js | 13 -- .../client/mutations/postRespect.graphql | 10 -- 3 files changed, 114 insertions(+), 64 deletions(-) delete mode 100644 plugins/coral-plugin-respect/client/mutations/index.js delete mode 100644 plugins/coral-plugin-respect/client/mutations/postRespect.graphql diff --git a/plugins/coral-plugin-respect/client/components/RespectButton.js b/plugins/coral-plugin-respect/client/components/RespectButton.js index a6c61de7c..ea8302efe 100644 --- a/plugins/coral-plugin-respect/client/components/RespectButton.js +++ b/plugins/coral-plugin-respect/client/components/RespectButton.js @@ -6,85 +6,53 @@ 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'; -import {withPostRespect} from '../mutations'; const lang = new I18n(translations); -import {getActionSummary} from 'coral-framework/utils'; import {showSignInDialog} from 'coral-framework/actions/auth'; -import {deleteAction} from 'coral-framework/graphql/mutations'; class RespectButton extends React.Component { static slot = 'Comment.Detail'; - constructor(props) { - super(props); - - this.state = { - localPost: null, // Set to the ID of an action if one is posted - localDelete: false // Set to true is the user deletes an action, unless localPost is already set. - }; - } - handleClick = () => { const {postRespect, showSignInDialog, deleteAction, commentId} = this.props; const {me, comment} = this.props.data; - const {localPost, localDelete} = this.state; - const respect = getActionSummary('RespectActionSummary', comment); - const respected = (respect && respect.current_user && !localDelete) || localPost; - - /** - * If the current user does not exist, trigger signIn Box - */ + const respect = comment.action_summaries[0]; + const respected = (respect && respect.current_user); + // If the current user does not exist, trigger sign in dialog. if (!me) { const offset = document.getElementById(`c_${commentId}`).getBoundingClientRect().top - 75; showSignInDialog(offset); return; } - /** - * If the current user is banned, do nothing - */ - + // If the current user is banned, do nothing. if (me.status === 'BANNED') { return; } if (!respected) { - this.setState({ - localPost: 'temp' - }); - postRespect({ item_id: commentId, item_type: 'COMMENTS' - }).then(({data}) => { - this.setState({ - localPost: data.createRespect.respect.id - }); }); - } else { - this.setState((prev) => prev.localPost ? {...prev, localPost: null} : {...prev, localDelete: true}); - deleteAction(localPost || respect.current_user.id); + deleteAction(respect.current_user.id); } } render() { const {comment} = this.props.data; - const {localPost, localDelete} = this.state; - const respect = comment && getActionSummary('RespectActionSummary', comment); - const respected = (respect && respect.current_user && !localDelete) || localPost; + const respect = comment && comment.action_summaries && comment.action_summaries[0]; + const respected = respect && respect.current_user; let count = respect ? respect.count : 0; - if (localPost) {count += 1;} - if (localDelete) {count -= 1;} - return (