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

131 lines
3.8 KiB
React
Raw Normal View History

import { Components, registerComponent } from 'meteor/nova:lib';
2016-03-17 17:48:25 +09:00
import React, { PropTypes, Component } from 'react';
2016-06-10 10:25:38 +09:00
import { intlShape, FormattedMessage, FormattedRelative } from 'react-intl';
import { ShowIf, withCurrentUser, withMessages } from 'meteor/nova:core';
2016-12-01 16:09:54 +09:00
import Comments from 'meteor/nova:comments';
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");
2016-12-02 13:43:26 +01:00
// todo: handle events in async callback
// this.context.events.track("comment deleted", {_id: documentId});
}
2016-02-18 21:39:33 +09:00
renderComment() {
const htmlBody = {__html: this.props.comment.htmlBody};
2016-11-15 18:33:16 +01:00
const showReplyButton = !this.props.comment.isDeleted && !!this.props.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}>
<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">
<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 (
<Components.CommentsEditForm
2016-11-07 23:46:12 +09:00
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">
<Components.UsersAvatar size="small" user={comment.user}/>
<Components.UsersName user={comment.user}/>
2016-06-10 10:25:38 +09:00
<div className="comments-item-date"><FormattedRelative value={comment.postedAt}/></div>
2016-12-01 16:09:54 +09:00
<ShowIf check={Comments.options.mutations.edit.check} document={this.props.comment}>
<div>
<a className="comment-edit" onClick={this.showEdit}><FormattedMessage id="comments.edit"/></a>
</div>
2016-12-01 16:09:54 +09:00
</ShowIf>
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-11-15 18:33:16 +01:00
currentUser: React.PropTypes.object,
};
2016-02-16 16:12:13 +09:00
2016-06-09 17:42:20 +09:00
CommentsItem.contextTypes = {
events: React.PropTypes.object,
2016-06-09 17:42:20 +09:00
intl: intlShape
};
2016-06-09 17:42:20 +09:00
registerComponent('CommentsItem', CommentsItem, withCurrentUser, withMessages);