import { Component, linkEvent } from 'inferno'; import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { UserOperation, Community, Post as PostI, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse } from '../interfaces'; import { WebSocketService, UserService } from '../services'; import { msgOp } from '../utils'; import { MomentTime } from './moment-time'; interface CommentNodeI { comment: Comment; children?: Array; showReply?: boolean; }; interface State { post: PostI; comments: Array; } export class Post extends Component { private subscription: Subscription; private emptyState: State = { post: { name: null, attributed_to: null, community_id: null, id: null, published: null, }, comments: [] } constructor(props, context) { super(props, context); this.state = this.emptyState; this.state.post.id = Number(this.props.match.params.id); this.subscription = WebSocketService.Instance.subject .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) .subscribe( (msg) => this.parseMessage(msg), (err) => console.error(err), () => console.log('complete') ); WebSocketService.Instance.getPost(this.state.post.id); } componentWillUnmount() { this.subscription.unsubscribe(); } render() { return (
{this.postHeader()} {this.commentsTree()}
{this.newComments()}
{this.sidebar()}
) } postHeader() { let title = this.state.post.url ?
{this.state.post.name} {(new URL(this.state.post.url)).hostname}
:
{this.state.post.name}
; return (
{title}
via {this.state.post.attributed_to}
{this.state.post.body}
) } newComments() { return (

New Comments

{this.state.comments.map(comment => )}
) } sidebar() { return (

Sidebar

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

); } // buildCommentsTree(): Array { buildCommentsTree(): any { let tree: Array = this.createCommentsTree(this.state.comments); console.log(tree); // TODO this is redoing every time and it shouldn't return tree; } private createCommentsTree(comments: Array): Array { let hashTable = {}; for (let comment of comments) { let node: CommentNodeI = { comment: comment }; hashTable[comment.id] = { ...node, children : [] }; } let tree: Array = []; for (let comment of comments) { if( comment.parent_id ) hashTable[comment.parent_id].children.push(hashTable[comment.id]); else tree.push(hashTable[comment.id]); } return tree; } commentsTree() { let nodes = this.buildCommentsTree(); return (
); } parseMessage(msg: any) { console.log(msg); let op: UserOperation = msgOp(msg); if (msg.error) { alert(msg.error); return; } else if (op == UserOperation.GetPost) { let res: PostResponse = msg; this.state.post = res.post; this.state.comments = res.comments; this.setState(this.state); } else if (op == UserOperation.CreateComment) { let res: CommentResponse = msg; this.state.comments.unshift(res.comment); this.setState(this.state); } } } interface CommentNodesState { } interface CommentNodesProps { nodes: Array; noIndent?: boolean; } export class CommentNodes extends Component { constructor(props, context) { super(props, context); this.handleReplyClick = this.handleReplyClick.bind(this); this.handleReplyCancel = this.handleReplyCancel.bind(this); } render() { return (
{this.props.nodes.map(node =>
20

{node.comment.content}

{node.showReply && } {node.children && }
)}
) } handleReplyClick(i: CommentNodeI, event) { i.showReply = true; this.setState(this.state); } handleReplyCancel(i: CommentNodeI): any { i.showReply = false; this.setState(this.state); } } interface CommentFormProps { postId?: number; node?: CommentNodeI; onReplyCancel?(node: CommentNodeI); } interface CommentFormState { commentForm: CommentFormI; topReply: boolean; } export class CommentForm extends Component { private emptyState: CommentFormState = { commentForm: { auth: null, content: null, post_id: null, parent_id: null }, topReply: true } constructor(props, context) { super(props, context); this.state = this.emptyState; if (this.props.node) { this.state.topReply = false; this.state.commentForm.post_id = this.props.node.comment.post_id; this.state.commentForm.parent_id = this.props.node.comment.id; } else { this.state.commentForm.post_id = this.props.postId; } console.log(this.state); this.handleReplyCancel = this.handleReplyCancel.bind(this); } render() { return (