2018-09-10 09:55:26 -07:00
|
|
|
import React from 'react';
|
2018-09-18 15:15:01 -07:00
|
|
|
import { connect } from 'react-redux';
|
2018-09-10 09:55:26 -07:00
|
|
|
import moment from 'moment';
|
2019-02-18 14:35:21 -08:00
|
|
|
import { Button, message } from 'antd';
|
2018-11-08 10:52:36 -08:00
|
|
|
import { Link } from 'react-router-dom';
|
2018-09-13 14:05:36 -07:00
|
|
|
import Markdown from 'components/Markdown';
|
2018-11-08 10:52:36 -08:00
|
|
|
import UserAvatar from 'components/UserAvatar';
|
2018-09-18 15:15:01 -07:00
|
|
|
import MarkdownEditor, { MARKDOWN_TYPE } from 'components/MarkdownEditor';
|
2019-02-18 14:35:21 -08:00
|
|
|
import { postProposalComment, reportProposalComment } from 'modules/proposals/actions';
|
2018-11-08 11:38:00 -08:00
|
|
|
import { getIsSignedIn } from 'modules/auth/selectors';
|
2018-11-08 11:16:42 -08:00
|
|
|
import { Comment as IComment } from 'types';
|
2018-09-18 15:15:01 -07:00
|
|
|
import { AppState } from 'store/reducers';
|
2018-09-22 09:53:22 -07:00
|
|
|
import './style.less';
|
2018-09-10 09:55:26 -07:00
|
|
|
|
2018-09-18 15:15:01 -07:00
|
|
|
interface OwnProps {
|
2018-09-10 09:55:26 -07:00
|
|
|
comment: IComment;
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:15:01 -07:00
|
|
|
interface StateProps {
|
|
|
|
isPostCommentPending: AppState['proposal']['isPostCommentPending'];
|
|
|
|
postCommentError: AppState['proposal']['postCommentError'];
|
2018-11-08 11:38:00 -08:00
|
|
|
isSignedIn: ReturnType<typeof getIsSignedIn>;
|
2018-09-18 15:15:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interface DispatchProps {
|
|
|
|
postProposalComment: typeof postProposalComment;
|
2019-02-18 14:35:21 -08:00
|
|
|
reportProposalComment: typeof reportProposalComment;
|
2018-09-18 15:15:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Props = OwnProps & StateProps & DispatchProps;
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
reply: string;
|
|
|
|
isReplying: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Comment extends React.Component<Props> {
|
|
|
|
state: State = {
|
|
|
|
reply: '',
|
|
|
|
isReplying: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps: Props) {
|
|
|
|
// TODO: Come up with better check on if our comment post was a success
|
|
|
|
const { isPostCommentPending, postCommentError } = this.props;
|
|
|
|
if (!isPostCommentPending && !postCommentError && prevProps.isPostCommentPending) {
|
|
|
|
this.setState({ reply: '', isReplying: false });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-10 09:55:26 -07:00
|
|
|
public render(): React.ReactNode {
|
2018-11-20 08:38:01 -08:00
|
|
|
const { comment, isSignedIn, isPostCommentPending } = this.props;
|
2018-09-18 15:15:01 -07:00
|
|
|
const { isReplying, reply } = this.state;
|
2018-12-14 11:36:22 -08:00
|
|
|
const authorPath = `/profile/${comment.author.userid}`;
|
2018-09-10 09:55:26 -07:00
|
|
|
return (
|
2018-09-22 09:53:22 -07:00
|
|
|
<div className="Comment">
|
|
|
|
<div className="Comment-info">
|
2018-11-08 10:52:36 -08:00
|
|
|
<Link to={authorPath}>
|
|
|
|
<div className="Comment-info-thumb">
|
|
|
|
<UserAvatar user={comment.author} />
|
|
|
|
</div>
|
|
|
|
</Link>
|
|
|
|
<Link to={authorPath}>
|
|
|
|
<div className="Comment-info-name">{comment.author.displayName}</div>
|
|
|
|
</Link>
|
2018-11-08 10:29:29 -08:00
|
|
|
<div className="Comment-info-time">
|
|
|
|
{moment.unix(comment.dateCreated).fromNow()}
|
|
|
|
</div>
|
2018-09-22 09:53:22 -07:00
|
|
|
</div>
|
2018-09-10 09:55:26 -07:00
|
|
|
|
2018-09-22 09:53:22 -07:00
|
|
|
<div className="Comment-body">
|
2018-11-14 09:59:48 -08:00
|
|
|
<Markdown source={comment.content} type={MARKDOWN_TYPE.REDUCED} />
|
2018-09-22 09:53:22 -07:00
|
|
|
</div>
|
2018-09-10 09:55:26 -07:00
|
|
|
|
2018-11-08 11:38:00 -08:00
|
|
|
{isSignedIn && (
|
|
|
|
<div className="Comment-controls">
|
|
|
|
<a className="Comment-controls-button" onClick={this.toggleReply}>
|
|
|
|
{isReplying ? 'Cancel' : 'Reply'}
|
|
|
|
</a>
|
2019-02-18 14:35:21 -08:00
|
|
|
{!comment.hidden &&
|
|
|
|
!comment.reported && (
|
|
|
|
<a className="Comment-controls-button" onClick={this.report}>
|
|
|
|
Report
|
|
|
|
</a>
|
|
|
|
)}
|
2018-11-08 11:38:00 -08:00
|
|
|
</div>
|
|
|
|
)}
|
2018-09-10 09:55:26 -07:00
|
|
|
|
2018-09-18 15:15:01 -07:00
|
|
|
{(comment.replies.length || isReplying) && (
|
2018-09-22 09:53:22 -07:00
|
|
|
<div className="Comment-replies">
|
2018-09-18 15:15:01 -07:00
|
|
|
{isReplying && (
|
2018-09-22 09:53:22 -07:00
|
|
|
<div className="Comment-replies-form">
|
2018-09-18 15:15:01 -07:00
|
|
|
<MarkdownEditor
|
|
|
|
onChange={this.handleChangeReply}
|
|
|
|
type={MARKDOWN_TYPE.REDUCED}
|
|
|
|
/>
|
|
|
|
<div style={{ marginTop: '0.5rem' }} />
|
2018-11-20 08:38:01 -08:00
|
|
|
<Button
|
|
|
|
onClick={this.reply}
|
|
|
|
disabled={!reply.length}
|
|
|
|
loading={isPostCommentPending}
|
|
|
|
>
|
2018-09-18 15:15:01 -07:00
|
|
|
Submit reply
|
|
|
|
</Button>
|
2018-09-22 09:53:22 -07:00
|
|
|
</div>
|
2018-09-18 15:15:01 -07:00
|
|
|
)}
|
|
|
|
{comment.replies.map(subComment => (
|
2018-11-08 11:14:52 -08:00
|
|
|
<ConnectedComment key={subComment.id} comment={subComment} />
|
2018-09-10 09:55:26 -07:00
|
|
|
))}
|
2018-09-22 09:53:22 -07:00
|
|
|
</div>
|
2018-09-10 09:55:26 -07:00
|
|
|
)}
|
2018-09-22 09:53:22 -07:00
|
|
|
</div>
|
2018-09-10 09:55:26 -07:00
|
|
|
);
|
|
|
|
}
|
2018-09-18 15:15:01 -07:00
|
|
|
|
|
|
|
private toggleReply = () => {
|
|
|
|
this.setState({ isReplying: !this.state.isReplying });
|
|
|
|
};
|
|
|
|
|
|
|
|
private handleChangeReply = (reply: string) => {
|
|
|
|
this.setState({ reply });
|
|
|
|
};
|
|
|
|
|
|
|
|
private reply = () => {
|
2018-11-08 11:14:52 -08:00
|
|
|
const { comment } = this.props;
|
2018-09-18 15:15:01 -07:00
|
|
|
const { reply } = this.state;
|
2018-11-08 11:14:52 -08:00
|
|
|
this.props.postProposalComment(comment.proposalId, reply, comment.id);
|
2018-09-18 15:15:01 -07:00
|
|
|
};
|
2019-02-18 14:35:21 -08:00
|
|
|
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
};
|
2018-09-10 09:55:26 -07:00
|
|
|
}
|
2018-09-18 15:15:01 -07:00
|
|
|
|
|
|
|
const ConnectedComment = connect<StateProps, DispatchProps, OwnProps, AppState>(
|
|
|
|
(state: AppState) => ({
|
|
|
|
isPostCommentPending: state.proposal.isPostCommentPending,
|
|
|
|
postCommentError: state.proposal.postCommentError,
|
2018-11-08 11:38:00 -08:00
|
|
|
isSignedIn: getIsSignedIn(state),
|
2018-09-18 15:15:01 -07:00
|
|
|
}),
|
|
|
|
{
|
|
|
|
postProposalComment,
|
2019-02-18 14:35:21 -08:00
|
|
|
reportProposalComment,
|
2018-09-18 15:15:01 -07:00
|
|
|
},
|
|
|
|
)(Comment);
|
|
|
|
|
|
|
|
export default ConnectedComment;
|