import { Component, linkEvent } from 'inferno'; import { Link } from 'inferno-router'; import { CommentNode as CommentNodeI, CommentLikeForm, CommentForm as CommentFormI } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { mdToHtml } from '../utils'; import { MomentTime } from './moment-time'; import { CommentForm } from './comment-form'; import { CommentNodes } from './comment-nodes'; interface CommentNodeState { showReply: boolean; showEdit: boolean; } interface CommentNodeProps { node: CommentNodeI; noIndent?: boolean; viewOnly?: boolean; } export class CommentNode extends Component { private emptyState: CommentNodeState = { showReply: false, showEdit: false } constructor(props: any, context: any) { super(props, context); this.state = this.emptyState; this.handleReplyCancel = this.handleReplyCancel.bind(this); this.handleCommentLike = this.handleCommentLike.bind(this); this.handleCommentDisLike = this.handleCommentDisLike.bind(this); } render() { let node = this.props.node; return (
{node.comment.score}
  • {node.comment.creator_name}
  • ( +{node.comment.upvotes} | -{node.comment.downvotes} )
{this.state.showEdit && } {!this.state.showEdit &&
    {!this.props.viewOnly &&
  • reply
  • {this.myComment &&
  • edit
  • } {this.myComment &&
  • delete
  • }
    }
  • link
}
{this.state.showReply && } {this.props.node.children && }
) } private get myComment(): boolean { return UserService.Instance.loggedIn && this.props.node.comment.creator_id == UserService.Instance.user.id; } handleReplyClick(i: CommentNode) { i.state.showReply = true; i.setState(i.state); } handleEditClick(i: CommentNode) { i.state.showEdit = true; i.setState(i.state); } handleDeleteClick(i: CommentNode) { let deleteForm: CommentFormI = { content: "*deleted*", edit_id: i.props.node.comment.id, post_id: i.props.node.comment.post_id, parent_id: i.props.node.comment.parent_id, auth: null }; WebSocketService.Instance.editComment(deleteForm); } handleReplyCancel() { this.state.showReply = false; this.state.showEdit = false; this.setState(this.state); } handleCommentLike(i: CommentNodeI) { let form: CommentLikeForm = { comment_id: i.comment.id, post_id: i.comment.post_id, score: (i.comment.my_vote == 1) ? 0 : 1 }; WebSocketService.Instance.likeComment(form); } handleCommentDisLike(i: CommentNodeI) { let form: CommentLikeForm = { comment_id: i.comment.id, post_id: i.comment.post_id, score: (i.comment.my_vote == -1) ? 0 : -1 }; WebSocketService.Instance.likeComment(form); } }