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

132 lines
4.1 KiB
React
Raw Normal View History

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';
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', 'deleteComment'].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
editSuccessCallback() {
2016-02-18 21:39:33 +09:00
this.setState({showEdit: false});
2016-03-17 17:48:25 +09:00
}
2016-02-18 21:39:33 +09:00
deleteComment() {
2016-06-09 17:42:20 +09:00
const comment = this.props.comment;
2016-06-09 17:42:20 +09:00
const deleteConfirmMessage = this.context.intl.formatMessage({id: "comments.delete_confirm"}, {body: Telescope.utils.trimWords(comment.body, 20)});
const deleteSuccessMessage = this.context.intl.formatMessage({id: "comments.delete_success"}, {body: Telescope.utils.trimWords(comment.body, 20)});
if (window.confirm(deleteConfirmMessage)) {
this.context.actions.call('comments.deleteById', comment._id, (error, result) => {
this.context.messages.flash(deleteSuccessMessage, "success");
this.context.events.track("comment deleted", {'_id': comment._id});
});
}
2016-06-09 17:42:20 +09:00
}
2016-02-18 21:39:33 +09:00
renderComment() {
const htmlBody = {__html: this.props.comment.htmlBody};
return (
<div className="comments-item-text">
<div dangerouslySetInnerHTML={htmlBody}></div>
2016-06-09 17:42:20 +09:00
{!this.props.comment.isDeleted ? <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">
<Telescope.components.CommentsNew
postId={this.props.comment.postId}
parentComment={this.props.comment}
successCallback={this.replySuccessCallback}
cancelCallback={this.replyCancelCallback}
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 (
<Telescope.components.CommentsEdit
comment={this.props.comment}
successCallback={this.editSuccessCallback}
cancelCallback={this.editCancelCallback}
/>
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>
2016-06-09 17:42:20 +09:00
{Users.can.edit(this.props.currentUser, this.props.comment) ? <a className="comment-edit" onClick={this.showEdit}><FormattedMessage id="comments.edit"/></a> : null}
{Users.can.edit(this.props.currentUser, this.props.comment) ? <a className="comment-delete" onClick={this.deleteComment}><FormattedMessage id="comments.delete"/></a> : null}
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
currentUser: React.PropTypes.object, // the current user
}
2016-02-16 16:12:13 +09:00
2016-06-09 17:42:20 +09:00
CommentsItem.contextTypes = {
actions: React.PropTypes.object,
messages: React.PropTypes.object,
events: React.PropTypes.object,
2016-06-09 17:42:20 +09:00
intl: intlShape
}
module.exports = CommentsItem;