diff --git a/ui/component/commentReactions/index.js b/ui/component/commentReactions/index.js index 995d0216f..9dc76dafe 100644 --- a/ui/component/commentReactions/index.js +++ b/ui/component/commentReactions/index.js @@ -1,16 +1,11 @@ import { connect } from 'react-redux'; import Comment from './view'; -import { - makeSelectMyReactionsForComment, - makeSelectOthersReactionsForComment, - selectPendingCommentReacts, -} from 'redux/selectors/comments'; +import { makeSelectMyReactionsForComment, makeSelectOthersReactionsForComment } from 'redux/selectors/comments'; import { doCommentReact } from 'redux/actions/comments'; const select = (state, props) => ({ myReacts: makeSelectMyReactionsForComment(props.commentId)(state), othersReacts: makeSelectOthersReactionsForComment(props.commentId)(state), - pendingCommentReacts: selectPendingCommentReacts(state), }); const perform = dispatch => ({ diff --git a/ui/component/commentReactions/view.jsx b/ui/component/commentReactions/view.jsx index 0645bbbb0..644c5524b 100644 --- a/ui/component/commentReactions/view.jsx +++ b/ui/component/commentReactions/view.jsx @@ -15,7 +15,7 @@ type Props = { }; export default function CommentReactions(props: Props) { - const { myReacts, othersReacts, commentId, react, pendingCommentReacts } = props; + const { myReacts, othersReacts, commentId, react } = props; const [activeChannel] = usePersistedState('comment-channel'); const getCountForReact = type => { @@ -38,7 +38,7 @@ export default function CommentReactions(props: Props) { className={classnames('comment__action', { 'comment__action--active': myReacts && myReacts.includes(REACTION_TYPES.LIKE), })} - disabled={!activeChannel || pendingCommentReacts.includes(commentId + REACTION_TYPES.LIKE)} + disabled={!activeChannel} onClick={() => react(commentId, REACTION_TYPES.LIKE)} label={getCountForReact(REACTION_TYPES.LIKE)} /> @@ -49,7 +49,7 @@ export default function CommentReactions(props: Props) { className={classnames('comment__action', { 'comment__action--active': myReacts && myReacts.includes(REACTION_TYPES.DISLIKE), })} - disabled={!activeChannel || pendingCommentReacts.includes(commentId + REACTION_TYPES.DISLIKE)} + disabled={!activeChannel} onClick={() => react(commentId, REACTION_TYPES.DISLIKE)} label={getCountForReact(REACTION_TYPES.DISLIKE)} /> diff --git a/ui/redux/actions/comments.js b/ui/redux/actions/comments.js index 62d344acb..ad477a8f1 100644 --- a/ui/redux/actions/comments.js +++ b/ui/redux/actions/comments.js @@ -103,7 +103,7 @@ export function doCommentReact(commentId: string, type: string) { }); return; } - if (pendingReacts.includes(commentId + exclusiveTypes[type])) { + if (pendingReacts.includes(commentId + exclusiveTypes[type]) || pendingReacts.includes(commentId + type)) { // ignore dislikes during likes, for example return; } @@ -134,31 +134,48 @@ export function doCommentReact(commentId: string, type: string) { type: ACTIONS.COMMENT_REACT_STARTED, data: commentId + type, }); + // simulate api return shape: ['like'] -> { 'like': 1 } const myReactsObj = myReacts.reduce((acc, el) => { acc[el] = 1; return acc; }, {}); + dispatch({ + type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED, + data: { + myReactions: { [commentId]: myReactsObj }, + othersReactions: { [commentId]: othersReacts }, + }, + }); + Lbry.comment_react(params) .then((result: CommentReactListResponse) => { dispatch({ type: ACTIONS.COMMENT_REACT_COMPLETED, data: commentId + type, }); - dispatch({ - type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED, - data: { - myReactions: { [commentId]: myReactsObj }, - othersReactions: { [commentId]: othersReacts }, - }, - }); }) .catch(error => { dispatch({ type: ACTIONS.COMMENT_REACT_FAILED, data: commentId + type, }); + + const myRevertedReactsObj = myReacts + .filter(el => el !== type) + .reduce((acc, el) => { + acc[el] = 1; + return acc; + }, {}); + + dispatch({ + type: ACTIONS.COMMENT_REACTION_LIST_COMPLETED, + data: { + myReactions: { [commentId]: myRevertedReactsObj }, + othersReactions: { [commentId]: othersReacts }, + }, + }); }); }; }