Vulcan/packages/nova-base-components/lib/comments/CommentsItem.jsx

136 lines
4 KiB
React
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-03-17 17:48:25 +09:00
import React, { PropTypes, Component } from 'react';
import moment from 'moment';
2016-06-10 10:25:38 +09:00
import { intlShape, FormattedMessage, FormattedRelative } from 'react-intl';
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users';
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
2016-11-07 23:46:12 +09:00
import NovaForm from "meteor/nova:forms";
2016-02-18 16:26:52 +09:00
2016-04-14 10:12:35 +09:00
class CommentsItem extends Component{
2016-02-18 21:39:33 +09:00
2016-03-17 17:48:25 +09:00
constructor() {
super();
['showReply', 'replyCancelCallback', 'replySuccessCallback', 'showEdit', 'editCancelCallback', 'editSuccessCallback', 'removeSuccessCallback'].forEach(methodName => {this[methodName] = this[methodName].bind(this)});
2016-03-17 17:48:25 +09:00
this.state = {
2016-02-18 21:39:33 +09:00
showReply: false,
showEdit: false
};
2016-03-17 17:48:25 +09:00
}
2016-02-18 16:26:52 +09:00
showReply(event) {
event.preventDefault();
this.setState({showReply: true});
2016-03-17 17:48:25 +09:00
}
2016-02-18 16:26:52 +09:00
replyCancelCallback(event) {
2016-02-18 16:26:52 +09:00
event.preventDefault();
this.setState({showReply: false});
2016-03-17 17:48:25 +09:00
}
2016-02-18 16:26:52 +09:00
replySuccessCallback() {
2016-02-18 21:39:33 +09:00
this.setState({showReply: false});
2016-03-17 17:48:25 +09:00
}
2016-02-18 21:39:33 +09:00
showEdit(event) {
event.preventDefault();
this.setState({showEdit: true});
2016-03-17 17:48:25 +09:00
}
2016-02-18 21:39:33 +09:00
editCancelCallback(event) {
2016-02-18 21:39:33 +09:00
event.preventDefault();
this.setState({showEdit: false});
2016-03-17 17:48:25 +09:00
}
2016-02-18 21:39:33 +09:00
2016-11-08 14:56:48 +09:00
editSuccessCallback() {
this.setState({showEdit: false});
2016-03-17 17:48:25 +09:00
}
2016-02-18 21:39:33 +09:00
removeSuccessCallback({documentId}) {
const deleteDocumentSuccess = this.context.intl.formatMessage({id: 'comments.delete_success'});
this.props.flash(deleteDocumentSuccess, "success");
this.context.events.track("comment deleted", {_id: documentId});
}
2016-02-18 21:39:33 +09:00
renderComment() {
const htmlBody = {__html: this.props.comment.htmlBody};
const showReplyButton = !this.props.comment.isDeleted && !!this.context.currentUser;
2016-02-18 21:39:33 +09:00
return (
<div className="comments-item-text">
<div dangerouslySetInnerHTML={htmlBody}></div>
{ showReplyButton ?
<a className="comments-item-reply-link" onClick={this.showReply}>
<Telescope.components.Icon name="reply"/> <FormattedMessage id="comments.reply"/>
</a> : null}
</div>
2016-02-18 21:39:33 +09:00
)
2016-03-17 17:48:25 +09:00
}
2016-02-18 21:39:33 +09:00
2016-02-18 17:53:04 +09:00
renderReply() {
2016-02-18 16:26:52 +09:00
return (
<div className="comments-item-reply">
2016-11-07 23:46:12 +09:00
<Telescope.components.CommentsNewForm
postId={this.props.comment.postId}
parentComment={this.props.comment}
successCallback={this.replySuccessCallback}
cancelCallback={this.replyCancelCallback}
2016-11-07 23:46:12 +09:00
type="reply"
/>
2016-02-18 16:26:52 +09:00
</div>
)
2016-03-17 17:48:25 +09:00
}
2016-02-18 16:26:52 +09:00
2016-02-18 21:39:33 +09:00
renderEdit() {
return (
2016-11-07 23:46:12 +09:00
<Telescope.components.CommentsEditForm
comment={this.props.comment}
successCallback={this.editSuccessCallback}
cancelCallback={this.editCancelCallback}
removeSuccessCallback={this.removeSuccessCallback}
/>
2016-02-18 21:39:33 +09:00
)
2016-03-17 17:48:25 +09:00
}
2016-02-18 17:53:04 +09:00
2016-02-18 21:39:33 +09:00
render() {
2016-03-19 18:19:28 +09:00
const comment = this.props.comment;
2016-02-18 21:39:33 +09:00
return (
<div className="comments-item" id={comment._id}>
<div className="comments-item-body">
<div className="comments-item-meta">
<Telescope.components.UsersAvatar size="small" user={comment.user}/>
<Telescope.components.UsersName user={comment.user}/>
2016-06-10 10:25:38 +09:00
<div className="comments-item-date"><FormattedRelative value={comment.postedAt}/></div>
<Telescope.components.CanDo action="comments.edit" document={this.props.comment}>
<div>
<a className="comment-edit" onClick={this.showEdit}><FormattedMessage id="comments.edit"/></a>
</div>
</Telescope.components.CanDo>
2016-03-19 18:19:28 +09:00
</div>
2016-02-19 10:12:08 +09:00
{this.state.showEdit ? this.renderEdit() : this.renderComment()}
</div>
{this.state.showReply ? this.renderReply() : null}
2016-02-18 21:39:33 +09:00
</div>
2016-02-18 16:26:52 +09:00
)
}
2016-03-17 17:48:25 +09:00
}
2016-04-14 10:12:35 +09:00
CommentsItem.propTypes = {
2016-03-17 17:48:25 +09:00
comment: React.PropTypes.object.isRequired, // the current comment
};
2016-02-16 16:12:13 +09:00
2016-06-09 17:42:20 +09:00
CommentsItem.contextTypes = {
currentUser: React.PropTypes.object,
events: React.PropTypes.object,
2016-06-09 17:42:20 +09:00
intl: intlShape
};
2016-06-09 17:42:20 +09:00
const mapStateToProps = state => ({ messages: state.messages, });
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
module.exports = connect(mapStateToProps, mapDispatchToProps)(CommentsItem);