zcash-grant-system/frontend/client/components/Proposal/Comments/index.tsx

119 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-09-10 09:55:26 -07:00
import React from 'react';
import { connect } from 'react-redux';
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';
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';
import Placeholder from 'components/Placeholder';
import MarkdownEditor, { MARKDOWN_TYPE } from 'components/MarkdownEditor';
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;
postProposalComment: typeof postProposalComment;
2018-09-10 09:55:26 -07:00
}
type Props = DispatchProps & OwnProps & StateProps;
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() {
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) {
content = <Comments comments={comments} proposalId={proposalId} />;
} else {
2018-09-10 09:55:26 -07:00
content = (
<Placeholder
title="No comments have been made yet"
subtitle="Why not be the first?"
/>
2018-09-10 09:55:26 -07:00
);
}
}
return (
<>
<div className="ProposalComments-post">
<MarkdownEditor
onChange={this.handleCommentChange}
type={MARKDOWN_TYPE.REDUCED}
/>
<div style={{ marginTop: '0.5rem' }} />
<Button onClick={this.postComment} disabled={!comment.length}>
Submit comment
</Button>
</div>
{content}
</>
);
2018-09-10 09:55:26 -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,
postProposalComment,
2018-09-10 09:55:26 -07:00
},
)(ProposalComments);