2020-09-11 19:51:31 +02:00
|
|
|
// @flow
|
2020-10-01 20:43:44 +02:00
|
|
|
import { ENABLE_COMMENT_REACTIONS } from 'config';
|
|
|
|
import * as ICONS from 'constants/icons';
|
|
|
|
import * as REACTION_TYPES from 'constants/reactions';
|
|
|
|
import React from 'react';
|
|
|
|
import classnames from 'classnames';
|
|
|
|
import Button from 'component/button';
|
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-09-11 19:51:31 +02:00
|
|
|
|
|
|
|
type Props = {
|
2020-09-29 16:10:23 +02:00
|
|
|
myReacts: Array<string>,
|
|
|
|
othersReacts: any,
|
|
|
|
react: (string, string) => void,
|
|
|
|
commentId: string,
|
2020-09-11 19:51:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function CommentReactions(props: Props) {
|
2020-10-01 20:43:44 +02:00
|
|
|
const { myReacts, othersReacts, commentId, react } = props;
|
|
|
|
const [activeChannel] = usePersistedState('comment-channel');
|
2020-09-29 16:10:23 +02:00
|
|
|
|
2020-10-01 20:43:44 +02:00
|
|
|
const getCountForReact = type => {
|
|
|
|
let count = 0;
|
|
|
|
if (othersReacts && othersReacts[type]) {
|
|
|
|
count += othersReacts[type];
|
|
|
|
}
|
|
|
|
if (myReacts && myReacts.includes(type)) {
|
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
};
|
2020-09-11 19:51:31 +02:00
|
|
|
|
2020-10-01 20:43:44 +02:00
|
|
|
if (!ENABLE_COMMENT_REACTIONS) {
|
|
|
|
return null;
|
|
|
|
}
|
2020-09-29 22:15:35 +02:00
|
|
|
|
2020-10-01 20:43:44 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
title={__('Upvote')}
|
|
|
|
icon={ICONS.UPVOTE}
|
|
|
|
className={classnames('comment__action', {
|
|
|
|
'comment__action--active': myReacts && myReacts.includes(REACTION_TYPES.LIKE),
|
|
|
|
})}
|
|
|
|
disabled={!activeChannel}
|
|
|
|
onClick={() => react(commentId, REACTION_TYPES.LIKE)}
|
|
|
|
label={getCountForReact(REACTION_TYPES.LIKE)}
|
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
title={__('Downvote')}
|
|
|
|
icon={ICONS.DOWNVOTE}
|
|
|
|
className={classnames('comment__action', {
|
|
|
|
'comment__action--active': myReacts && myReacts.includes(REACTION_TYPES.DISLIKE),
|
|
|
|
})}
|
|
|
|
disabled={!activeChannel}
|
|
|
|
onClick={() => react(commentId, REACTION_TYPES.DISLIKE)}
|
|
|
|
label={getCountForReact(REACTION_TYPES.DISLIKE)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
2020-09-11 19:51:31 +02:00
|
|
|
}
|