2018-09-10 09:55:26 -07:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2018-09-18 15:15:01 -07:00
|
|
|
import { Spin, Button } from 'antd';
|
2018-09-10 09:55:26 -07:00
|
|
|
import { AppState } from 'store/reducers';
|
2018-10-04 21:27:02 -07:00
|
|
|
import { ProposalWithCrowdFund } from 'types';
|
2018-09-18 15:15:01 -07:00
|
|
|
import { fetchProposalComments, postProposalComment } from 'modules/proposals/actions';
|
2018-09-10 09:55:26 -07:00
|
|
|
import {
|
|
|
|
getProposalComments,
|
|
|
|
getIsFetchingComments,
|
|
|
|
getCommentsError,
|
|
|
|
} from 'modules/proposals/selectors';
|
|
|
|
import Comments from 'components/Comments';
|
2018-09-18 15:15:01 -07:00
|
|
|
import Placeholder from 'components/Placeholder';
|
|
|
|
import MarkdownEditor, { MARKDOWN_TYPE } from 'components/MarkdownEditor';
|
2018-09-22 09:44:55 -07:00
|
|
|
import './style.less';
|
2018-09-10 09:55:26 -07:00
|
|
|
|
|
|
|
interface OwnProps {
|
|
|
|
proposalId: ProposalWithCrowdFund['proposalId'];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface StateProps {
|
|
|
|
comments: ReturnType<typeof getProposalComments>;
|
|
|
|
isFetchingComments: ReturnType<typeof getIsFetchingComments>;
|
|
|
|
commentsError: ReturnType<typeof getCommentsError>;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface DispatchProps {
|
|
|
|
fetchProposalComments: typeof fetchProposalComments;
|
2018-09-18 15:15:01 -07:00
|
|
|
postProposalComment: typeof postProposalComment;
|
2018-09-10 09:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Props = DispatchProps & OwnProps & StateProps;
|
|
|
|
|
2018-09-18 15:15:01 -07:00
|
|
|
interface State {
|
|
|
|
comment: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class ProposalComments extends React.Component<Props, State> {
|
|
|
|
state: State = {
|
|
|
|
comment: '',
|
|
|
|
};
|
|
|
|
|
2018-09-10 09:55:26 -07:00
|
|
|
componentDidMount() {
|
|
|
|
if (this.props.proposalId) {
|
|
|
|
this.props.fetchProposalComments(this.props.proposalId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillReceiveProps(nextProps: Props) {
|
|
|
|
if (nextProps.proposalId && nextProps.proposalId !== this.props.proposalId) {
|
|
|
|
this.props.fetchProposalComments(nextProps.proposalId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-09-18 15:15:01 -07:00
|
|
|
const { proposalId, comments, isFetchingComments, commentsError } = this.props;
|
|
|
|
const { comment } = this.state;
|
2018-09-10 09:55:26 -07:00
|
|
|
let content = null;
|
|
|
|
|
|
|
|
if (isFetchingComments) {
|
|
|
|
content = <Spin />;
|
|
|
|
} else if (commentsError) {
|
|
|
|
content = (
|
|
|
|
<>
|
|
|
|
<h2>Something went wrong</h2>
|
|
|
|
<p>{commentsError}</p>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
} else if (comments) {
|
|
|
|
if (comments.length) {
|
2018-09-18 15:15:01 -07:00
|
|
|
content = <Comments comments={comments} proposalId={proposalId} />;
|
|
|
|
} else {
|
2018-09-10 09:55:26 -07:00
|
|
|
content = (
|
2018-09-18 15:15:01 -07:00
|
|
|
<Placeholder
|
|
|
|
title="No comments have been made yet"
|
|
|
|
subtitle="Why not be the first?"
|
|
|
|
/>
|
2018-09-10 09:55:26 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 15:15:01 -07:00
|
|
|
return (
|
|
|
|
<>
|
2018-09-22 09:44:55 -07:00
|
|
|
<div className="ProposalComments-post">
|
2018-09-18 15:15:01 -07:00
|
|
|
<MarkdownEditor
|
|
|
|
onChange={this.handleCommentChange}
|
|
|
|
type={MARKDOWN_TYPE.REDUCED}
|
|
|
|
/>
|
|
|
|
<div style={{ marginTop: '0.5rem' }} />
|
|
|
|
<Button onClick={this.postComment} disabled={!comment.length}>
|
|
|
|
Submit comment
|
|
|
|
</Button>
|
2018-09-22 09:44:55 -07:00
|
|
|
</div>
|
2018-09-18 15:15:01 -07:00
|
|
|
{content}
|
|
|
|
</>
|
|
|
|
);
|
2018-09-10 09:55:26 -07:00
|
|
|
}
|
2018-09-18 15:15:01 -07:00
|
|
|
|
|
|
|
private handleCommentChange = (comment: string) => {
|
|
|
|
this.setState({ comment });
|
|
|
|
};
|
|
|
|
|
|
|
|
private postComment = () => {
|
|
|
|
this.props.postProposalComment(this.props.proposalId, this.state.comment);
|
|
|
|
};
|
2018-09-10 09:55:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(
|
|
|
|
(state: AppState, ownProps: OwnProps) => ({
|
|
|
|
comments: getProposalComments(state, ownProps.proposalId),
|
|
|
|
isFetchingComments: getIsFetchingComments(state),
|
|
|
|
commentsError: getCommentsError(state),
|
|
|
|
}),
|
|
|
|
{
|
|
|
|
fetchProposalComments,
|
2018-09-18 15:15:01 -07:00
|
|
|
postProposalComment,
|
2018-09-10 09:55:26 -07:00
|
|
|
},
|
|
|
|
)(ProposalComments);
|