mirror of
https://github.com/vale981/Vulcan
synced 2025-03-10 04:26:41 -04:00
131 lines
3.8 KiB
JavaScript
131 lines
3.8 KiB
JavaScript
import { Components, registerComponent } from 'meteor/nova:lib';
|
|
import React, { PropTypes, Component } from 'react';
|
|
import { intlShape, FormattedMessage, FormattedRelative } from 'react-intl';
|
|
import { ShowIf, withCurrentUser, withMessages } from 'meteor/nova:core';
|
|
import Comments from 'meteor/nova:comments';
|
|
|
|
class CommentsItem extends Component{
|
|
|
|
constructor() {
|
|
super();
|
|
['showReply', 'replyCancelCallback', 'replySuccessCallback', 'showEdit', 'editCancelCallback', 'editSuccessCallback', 'removeSuccessCallback'].forEach(methodName => {this[methodName] = this[methodName].bind(this)});
|
|
this.state = {
|
|
showReply: false,
|
|
showEdit: false
|
|
};
|
|
}
|
|
|
|
showReply(event) {
|
|
event.preventDefault();
|
|
this.setState({showReply: true});
|
|
}
|
|
|
|
replyCancelCallback(event) {
|
|
event.preventDefault();
|
|
this.setState({showReply: false});
|
|
}
|
|
|
|
replySuccessCallback() {
|
|
this.setState({showReply: false});
|
|
}
|
|
|
|
showEdit(event) {
|
|
event.preventDefault();
|
|
this.setState({showEdit: true});
|
|
}
|
|
|
|
editCancelCallback(event) {
|
|
event.preventDefault();
|
|
this.setState({showEdit: false});
|
|
}
|
|
|
|
editSuccessCallback() {
|
|
this.setState({showEdit: false});
|
|
}
|
|
|
|
removeSuccessCallback({documentId}) {
|
|
const deleteDocumentSuccess = this.context.intl.formatMessage({id: 'comments.delete_success'});
|
|
this.props.flash(deleteDocumentSuccess, "success");
|
|
// todo: handle events in async callback
|
|
// this.context.events.track("comment deleted", {_id: documentId});
|
|
}
|
|
|
|
renderComment() {
|
|
const htmlBody = {__html: this.props.comment.htmlBody};
|
|
|
|
const showReplyButton = !this.props.comment.isDeleted && !!this.props.currentUser;
|
|
|
|
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>
|
|
)
|
|
}
|
|
|
|
renderReply() {
|
|
|
|
return (
|
|
<div className="comments-item-reply">
|
|
<Components.CommentsNewForm
|
|
postId={this.props.comment.postId}
|
|
parentComment={this.props.comment}
|
|
successCallback={this.replySuccessCallback}
|
|
cancelCallback={this.replyCancelCallback}
|
|
type="reply"
|
|
/>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
renderEdit() {
|
|
|
|
return (
|
|
<Components.CommentsEditForm
|
|
comment={this.props.comment}
|
|
successCallback={this.editSuccessCallback}
|
|
cancelCallback={this.editCancelCallback}
|
|
removeSuccessCallback={this.removeSuccessCallback}
|
|
/>
|
|
)
|
|
}
|
|
|
|
render() {
|
|
const comment = this.props.comment;
|
|
|
|
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}/>
|
|
<div className="comments-item-date"><FormattedRelative value={comment.postedAt}/></div>
|
|
<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>
|
|
</ShowIf>
|
|
</div>
|
|
{this.state.showEdit ? this.renderEdit() : this.renderComment()}
|
|
</div>
|
|
{this.state.showReply ? this.renderReply() : null}
|
|
</div>
|
|
)
|
|
}
|
|
|
|
}
|
|
|
|
CommentsItem.propTypes = {
|
|
comment: React.PropTypes.object.isRequired, // the current comment
|
|
currentUser: React.PropTypes.object,
|
|
};
|
|
|
|
CommentsItem.contextTypes = {
|
|
events: React.PropTypes.object,
|
|
intl: intlShape
|
|
};
|
|
|
|
registerComponent('CommentsItem', CommentsItem, withCurrentUser, withMessages);
|