import Telescope from 'meteor/nova:lib'; import React, { PropTypes, Component } from 'react'; import moment from 'moment'; import { intlShape, FormattedMessage, FormattedRelative } from 'react-intl'; import Users from 'meteor/nova:users'; class CommentsItem extends Component{ constructor() { super(); ['showReply', 'replyCancelCallback', 'replySuccessCallback', 'showEdit', 'editCancelCallback', 'editSuccessCallback', 'deleteComment'].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}); } deleteComment() { const comment = this.props.comment; 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}); }); } } renderComment() { const htmlBody = {__html: this.props.comment.htmlBody}; const showReplyButton = !this.props.comment.isDeleted && !!this.context.currentUser; return (
{ showReplyButton ? : null}
) } renderReply() { return (
) } renderEdit() { return ( ) } render() { const comment = this.props.comment; return (
{this.state.showEdit ? this.renderEdit() : this.renderComment()}
{this.state.showReply ? this.renderReply() : null}
) } } CommentsItem.propTypes = { comment: React.PropTypes.object.isRequired, // the current comment }; CommentsItem.contextTypes = { currentUser: React.PropTypes.object, actions: React.PropTypes.object, messages: React.PropTypes.object, events: React.PropTypes.object, intl: intlShape }; module.exports = CommentsItem;