import React from 'react'; import { connect } from 'react-redux'; import moment from 'moment'; import { Button, message } from 'antd'; import { Link } from 'react-router-dom'; import Markdown from 'components/Markdown'; import UserAvatar from 'components/UserAvatar'; import MarkdownEditor, { MARKDOWN_TYPE } from 'components/MarkdownEditor'; import { postProposalComment, reportProposalComment } from 'modules/proposals/actions'; import { getIsSignedIn } from 'modules/auth/selectors'; import { Comment as IComment } from 'types'; import { AppState } from 'store/reducers'; import Like from 'components/Like'; import './style.less'; interface OwnProps { comment: IComment; } interface StateProps { isPostCommentPending: AppState['proposal']['isPostCommentPending']; postCommentError: AppState['proposal']['postCommentError']; isSignedIn: ReturnType; detail: AppState['proposal']['detail']; } interface DispatchProps { postProposalComment: typeof postProposalComment; reportProposalComment: typeof reportProposalComment; } type Props = OwnProps & StateProps & DispatchProps; interface State { reply: string; isReplying: boolean; } class Comment extends React.Component { state: State = { reply: '', isReplying: false, }; componentDidUpdate(prevProps: Props) { const { isPostCommentPending, postCommentError } = this.props; if (!isPostCommentPending && !postCommentError && prevProps.isPostCommentPending) { this.setState({ reply: '', isReplying: false }); } } public render(): React.ReactNode { const { comment, isSignedIn, isPostCommentPending } = this.props; const { isReplying, reply } = this.state; const authorPath = `/profile/${comment.author.userid}`; return (
{comment.author.displayName}
{moment.unix(comment.dateCreated).fromNow()}
{isSignedIn && ( {isReplying ? 'Cancel' : 'Reply'} )} {isSignedIn && !comment.hidden && !comment.reported && ( Report )}
{(comment.replies.length || isReplying) && (
{isReplying && (
)} {comment.replies.map(subComment => ( ))}
)}
); } private toggleReply = () => { this.setState({ isReplying: !this.state.isReplying }); }; private handleChangeReply = (reply: string) => { this.setState({ reply }); }; private reply = () => { const { comment } = this.props; const { reply } = this.state; this.props.postProposalComment(comment.proposalId, reply, comment.id); }; private report = async () => { const { proposalId, id } = this.props.comment; const res = await this.props.reportProposalComment(proposalId, id); if ((res as any).error) { message.error('Problem reporting comment: ' + (res as any).payload); } else { message.success('Comment reported'); } }; } const ConnectedComment = connect( (state: AppState) => ({ isPostCommentPending: state.proposal.isPostCommentPending, postCommentError: state.proposal.postCommentError, isSignedIn: getIsSignedIn(state), detail: state.proposal.detail, }), { postProposalComment, reportProposalComment, }, )(Comment); export default ConnectedComment;