2019-03-26 18:00:18 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-03-28 19:32:08 +00:00
|
|
|
import { UserOperation, Community, Post as PostI, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentLikeForm, CreateCommentLikeResponse } from '../interfaces';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
|
|
|
import { msgOp } from '../utils';
|
2019-03-27 19:54:55 +00:00
|
|
|
import { MomentTime } from './moment-time';
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
interface CommentNodeI {
|
|
|
|
comment: Comment;
|
|
|
|
children?: Array<CommentNodeI>;
|
|
|
|
};
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
interface State {
|
|
|
|
post: PostI;
|
|
|
|
comments: Array<Comment>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class Post extends Component<any, State> {
|
|
|
|
|
|
|
|
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;
|
2019-03-27 16:59:21 +00:00
|
|
|
|
|
|
|
this.state.post.id = Number(this.props.match.params.id);
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
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')
|
|
|
|
);
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
WebSocketService.Instance.getPost(this.state.post.id);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
|
|
|
<div class="row">
|
2019-03-27 16:59:21 +00:00
|
|
|
<div class="col-12 col-sm-8 col-lg-7 mb-3">
|
|
|
|
{this.postHeader()}
|
|
|
|
<CommentForm postId={this.state.post.id} />
|
|
|
|
{this.commentsTree()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-sm-4 col-lg-3 mb-3">
|
|
|
|
{this.newComments()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-sm-12 col-lg-2">
|
|
|
|
{this.sidebar()}
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
postHeader() {
|
|
|
|
let title = this.state.post.url
|
2019-03-27 19:54:55 +00:00
|
|
|
? <h5>
|
|
|
|
<a href={this.state.post.url}>{this.state.post.name}</a>
|
|
|
|
<small><a className="ml-2 text-muted font-italic" href={this.state.post.url}>{(new URL(this.state.post.url)).hostname}</a></small>
|
2019-03-28 19:32:08 +00:00
|
|
|
</h5>
|
2019-03-27 16:59:21 +00:00
|
|
|
: <h5>{this.state.post.name}</h5>;
|
2019-03-26 18:00:18 +00:00
|
|
|
return (
|
|
|
|
<div>
|
2019-03-27 19:54:55 +00:00
|
|
|
<div>{title}</div>
|
|
|
|
<div>via {this.state.post.attributed_to} <MomentTime data={this.state.post} /></div>
|
|
|
|
<div>{this.state.post.body}</div>
|
|
|
|
</div>
|
2019-03-27 16:59:21 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
newComments() {
|
|
|
|
return (
|
|
|
|
<div class="sticky-top">
|
|
|
|
<h4>New Comments</h4>
|
2019-03-26 18:00:18 +00:00
|
|
|
{this.state.comments.map(comment =>
|
2019-03-27 19:54:55 +00:00
|
|
|
<CommentNodes nodes={[{comment: comment}]} noIndent />
|
2019-03-26 18:00:18 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
|
|
|
|
sidebar() {
|
|
|
|
return (
|
|
|
|
<div class="sticky-top">
|
|
|
|
<h4>Sidebar</h4>
|
|
|
|
<p>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.</p>
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
|
|
|
|
// buildCommentsTree(): Array<CommentNodeI> {
|
|
|
|
buildCommentsTree(): any {
|
|
|
|
let tree: Array<CommentNodeI> = this.createCommentsTree(this.state.comments);
|
|
|
|
console.log(tree); // TODO this is redoing every time and it shouldn't
|
|
|
|
return tree;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
private createCommentsTree(comments: Array<Comment>): Array<CommentNodeI> {
|
|
|
|
let hashTable = {};
|
|
|
|
for (let comment of comments) {
|
|
|
|
let node: CommentNodeI = {
|
|
|
|
comment: comment
|
|
|
|
};
|
|
|
|
hashTable[comment.id] = { ...node, children : [] };
|
|
|
|
}
|
|
|
|
let tree: Array<CommentNodeI> = [];
|
|
|
|
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 (
|
2019-03-27 18:37:16 +00:00
|
|
|
<div className="">
|
2019-03-27 16:59:21 +00:00
|
|
|
<CommentNodes nodes={nodes} />
|
|
|
|
</div>
|
|
|
|
);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2019-03-27 18:37:16 +00:00
|
|
|
this.state.comments = res.comments;
|
2019-03-27 16:59:21 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreateComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
this.state.comments.unshift(res.comment);
|
2019-03-26 18:00:18 +00:00
|
|
|
this.setState(this.state);
|
2019-03-28 19:32:08 +00:00
|
|
|
} else if (op == UserOperation.CreateCommentLike) {
|
|
|
|
let res: CreateCommentLikeResponse = msg;
|
|
|
|
let found: Comment = this.state.comments.find(c => c.id === res.comment.id);
|
|
|
|
found.score = res.comment.score;
|
|
|
|
found.upvotes = res.comment.upvotes;
|
|
|
|
found.downvotes = res.comment.downvotes;
|
|
|
|
if (res.comment.my_vote !== null)
|
|
|
|
found.my_vote = res.comment.my_vote;
|
|
|
|
console.log(res.comment.my_vote);
|
|
|
|
this.setState(this.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentNodesState {
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentNodesProps {
|
|
|
|
nodes: Array<CommentNodeI>;
|
2019-03-27 19:54:55 +00:00
|
|
|
noIndent?: boolean;
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentNodes extends Component<CommentNodesProps, CommentNodesState> {
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div className="comments">
|
|
|
|
{this.props.nodes.map(node =>
|
2019-03-28 19:32:08 +00:00
|
|
|
<CommentNode node={node} noIndent={this.props.noIndent} />
|
2019-03-27 16:59:21 +00:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
interface CommentNodeState {
|
|
|
|
showReply: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentNodeProps {
|
|
|
|
node: CommentNodeI;
|
|
|
|
noIndent?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
|
|
|
|
|
|
|
|
private emptyState: CommentNodeState = {
|
|
|
|
showReply: false
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
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 (
|
|
|
|
<div className={`comment ${node.comment.parent_id && !this.props.noIndent ? 'ml-4' : ''}`}>
|
|
|
|
<div className="float-left small text-center">
|
|
|
|
<div className={`pointer upvote ${node.comment.my_vote == 1 ? 'text-info' : 'text-muted'}`} onClick={linkEvent(node, this.handleCommentLike)}>▲</div>
|
|
|
|
<div>{node.comment.score}</div>
|
|
|
|
<div className={`pointer downvote ${node.comment.my_vote == -1 && 'text-danger'}`} onClick={linkEvent(node, this.handleCommentDisLike)}>▼</div>
|
|
|
|
</div>
|
|
|
|
<div className="details ml-4">
|
|
|
|
<ul class="list-inline mb-0 text-muted small">
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<a href={node.comment.attributed_to}>{node.comment.attributed_to}</a>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span>(
|
|
|
|
<span className="text-info">+{node.comment.upvotes}</span>
|
|
|
|
<span> | </span>
|
|
|
|
<span className="text-danger">-{node.comment.downvotes}</span>
|
|
|
|
<span>) </span>
|
|
|
|
</span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span><MomentTime data={node.comment} /></span>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
<p className="mb-0">{node.comment.content}</p>
|
|
|
|
<ul class="list-inline mb-1 text-muted small font-weight-bold">
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleReplyClick)}>reply</span>
|
|
|
|
</li>
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<a className="text-muted" href="test">link</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
{this.state.showReply && <CommentForm node={node} onReplyCancel={this.handleReplyCancel} />}
|
|
|
|
{this.props.node.children && <CommentNodes nodes={this.props.node.children}/>}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private getScore(): number {
|
|
|
|
return (this.props.node.comment.upvotes - this.props.node.comment.downvotes) || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyClick(i: CommentNode, event) {
|
|
|
|
i.state.showReply = true;
|
|
|
|
i.setState(i.state);
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
handleReplyCancel(): any {
|
|
|
|
this.state.showReply = false;
|
2019-03-27 16:59:21 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
|
|
|
|
handleCommentLike(i: CommentNodeI, event) {
|
|
|
|
|
|
|
|
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, event) {
|
|
|
|
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);
|
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentFormProps {
|
|
|
|
postId?: number;
|
|
|
|
node?: CommentNodeI;
|
2019-03-28 19:32:08 +00:00
|
|
|
onReplyCancel?();
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentFormState {
|
|
|
|
commentForm: CommentFormI;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
|
|
|
|
|
|
|
private emptyState: CommentFormState = {
|
|
|
|
commentForm: {
|
|
|
|
auth: null,
|
|
|
|
content: null,
|
|
|
|
post_id: null,
|
|
|
|
parent_id: null
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
if (this.props.node) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<form onSubmit={linkEvent(this, this.handleCreateCommentSubmit)}>
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-sm-12">
|
|
|
|
<textarea class="form-control" value={this.state.commentForm.content} onInput={linkEvent(this, this.handleCommentContentChange)} placeholder="Comment here" required />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-sm-12">
|
|
|
|
<button type="submit" class="btn btn-secondary mr-2">Post</button>
|
2019-03-28 19:32:08 +00:00
|
|
|
{this.props.node && <button type="button" class="btn btn-secondary" onClick={linkEvent(this, this.handleReplyCancel)}>Cancel</button>}
|
2019-03-27 16:59:21 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCreateCommentSubmit(i: CommentForm, event) {
|
|
|
|
WebSocketService.Instance.createComment(i.state.commentForm);
|
|
|
|
i.state.commentForm.content = undefined;
|
|
|
|
i.setState(i.state);
|
|
|
|
event.target.reset();
|
2019-03-28 19:32:08 +00:00
|
|
|
if (i.props.node) {
|
|
|
|
i.props.onReplyCancel();
|
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCommentContentChange(i: CommentForm, event) {
|
|
|
|
// TODO don't use setState, it triggers a re-render
|
|
|
|
i.state.commentForm.content = event.target.value;
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
handleReplyCancel(i: CommentForm, event) {
|
|
|
|
i.props.onReplyCancel();
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|