2019-03-26 14:00:18 -04:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-04-15 19:12:06 -04:00
|
|
|
import { UserOperation, Community, Post as PostI, GetPostResponse, PostResponse, Comment, CommentResponse, CommentSortType, CreatePostLikeResponse, CommunityUser, CommunityResponse, CommentNode as CommentNodeI, BanFromCommunityResponse, AddModToCommunityResponse } from '../interfaces';
|
2019-04-08 01:19:02 -04:00
|
|
|
import { WebSocketService } from '../services';
|
|
|
|
import { msgOp, hotRank } from '../utils';
|
2019-04-03 02:49:32 -04:00
|
|
|
import { PostListing } from './post-listing';
|
2019-04-03 19:01:20 -04:00
|
|
|
import { Sidebar } from './sidebar';
|
2019-04-08 01:19:02 -04:00
|
|
|
import { CommentForm } from './comment-form';
|
|
|
|
import { CommentNodes } from './comment-nodes';
|
2019-03-30 20:38:54 -04:00
|
|
|
import * as autosize from 'autosize';
|
2019-03-26 14:00:18 -04:00
|
|
|
|
2019-03-27 12:59:21 -04:00
|
|
|
|
2019-04-03 19:01:20 -04:00
|
|
|
interface PostState {
|
2019-03-26 14:00:18 -04:00
|
|
|
post: PostI;
|
|
|
|
comments: Array<Comment>;
|
2019-03-30 02:08:02 -04:00
|
|
|
commentSort: CommentSortType;
|
2019-04-03 19:01:20 -04:00
|
|
|
community: Community;
|
2019-04-04 16:53:32 -04:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-04-08 01:19:02 -04:00
|
|
|
scrolled?: boolean;
|
|
|
|
scrolled_comment_id?: number;
|
2019-04-08 17:46:09 -04:00
|
|
|
loading: boolean;
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-04-03 19:01:20 -04:00
|
|
|
export class Post extends Component<any, PostState> {
|
2019-03-26 14:00:18 -04:00
|
|
|
|
|
|
|
private subscription: Subscription;
|
2019-04-03 19:01:20 -04:00
|
|
|
private emptyState: PostState = {
|
2019-04-03 02:49:32 -04:00
|
|
|
post: null,
|
2019-03-30 02:08:02 -04:00
|
|
|
comments: [],
|
2019-04-03 19:01:20 -04:00
|
|
|
commentSort: CommentSortType.Hot,
|
|
|
|
community: null,
|
2019-04-05 16:22:59 -04:00
|
|
|
moderators: [],
|
2019-04-08 17:46:09 -04:00
|
|
|
scrolled: false,
|
|
|
|
loading: true
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 01:19:02 -04:00
|
|
|
constructor(props: any, context: any) {
|
2019-03-26 14:00:18 -04:00
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-03-27 12:59:21 -04:00
|
|
|
|
2019-04-03 02:49:32 -04:00
|
|
|
let postId = Number(this.props.match.params.id);
|
2019-04-08 01:19:02 -04:00
|
|
|
if (this.props.match.params.comment_id) {
|
|
|
|
this.state.scrolled_comment_id = this.props.match.params.comment_id;
|
|
|
|
}
|
2019-03-26 14:00:18 -04: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-04-03 02:49:32 -04:00
|
|
|
WebSocketService.Instance.getPost(postId);
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-03-30 20:38:54 -04:00
|
|
|
componentDidMount() {
|
|
|
|
autosize(document.querySelectorAll('textarea'));
|
|
|
|
}
|
|
|
|
|
2019-04-08 01:19:02 -04:00
|
|
|
componentDidUpdate(_lastProps: any, lastState: PostState, _snapshot: any) {
|
|
|
|
if (this.state.scrolled_comment_id && !this.state.scrolled && lastState.comments.length > 0) {
|
|
|
|
var elmnt = document.getElementById(`comment-${this.state.scrolled_comment_id}`);
|
2019-04-05 16:22:59 -04:00
|
|
|
elmnt.scrollIntoView();
|
2019-04-08 01:19:02 -04:00
|
|
|
elmnt.classList.add("mark");
|
2019-04-05 16:22:59 -04:00
|
|
|
this.state.scrolled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 14:00:18 -04:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-04-08 17:46:09 -04:00
|
|
|
{this.state.loading ?
|
|
|
|
<h4><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h4> :
|
|
|
|
<div class="row">
|
2019-04-16 19:04:23 -04:00
|
|
|
<div class="col-12 col-md-8 col-lg-7 mb-3">
|
2019-04-03 16:59:37 -04:00
|
|
|
<PostListing post={this.state.post} showBody showCommunity editable />
|
2019-04-03 02:49:32 -04:00
|
|
|
<div className="mb-2" />
|
2019-04-15 19:12:06 -04:00
|
|
|
<CommentForm postId={this.state.post.id} disabled={this.state.post.locked} />
|
2019-04-03 02:49:32 -04:00
|
|
|
{this.sortRadios()}
|
|
|
|
{this.commentsTree()}
|
|
|
|
</div>
|
2019-04-16 19:04:23 -04:00
|
|
|
<div class="col-12 col-md-4 col-lg-3 mb-3 d-none d-md-block">
|
2019-04-03 02:49:32 -04:00
|
|
|
{this.state.comments.length > 0 && this.newComments()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-sm-12 col-lg-2">
|
|
|
|
{this.sidebar()}
|
|
|
|
</div>
|
2019-03-27 12:59:21 -04:00
|
|
|
</div>
|
2019-04-03 02:49:32 -04:00
|
|
|
}
|
2019-03-27 15:54:55 -04:00
|
|
|
</div>
|
2019-03-27 12:59:21 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-30 02:08:02 -04:00
|
|
|
sortRadios() {
|
|
|
|
return (
|
|
|
|
<div class="btn-group btn-group-toggle mb-3">
|
2019-04-09 17:21:19 -04:00
|
|
|
<label className={`btn btn-sm btn-secondary pointer ${this.state.commentSort === CommentSortType.Hot && 'active'}`}>Hot
|
2019-03-30 02:08:02 -04:00
|
|
|
<input type="radio" value={CommentSortType.Hot}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Hot}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)} />
|
|
|
|
</label>
|
2019-04-09 17:21:19 -04:00
|
|
|
<label className={`btn btn-sm btn-secondary pointer ${this.state.commentSort === CommentSortType.Top && 'active'}`}>Top
|
2019-03-30 02:08:02 -04:00
|
|
|
<input type="radio" value={CommentSortType.Top}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Top}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)} />
|
|
|
|
</label>
|
2019-04-09 17:21:19 -04:00
|
|
|
<label className={`btn btn-sm btn-secondary pointer ${this.state.commentSort === CommentSortType.New && 'active'}`}>New
|
2019-03-30 02:08:02 -04:00
|
|
|
<input type="radio" value={CommentSortType.New}
|
|
|
|
checked={this.state.commentSort === CommentSortType.New}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)} />
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-27 12:59:21 -04:00
|
|
|
newComments() {
|
|
|
|
return (
|
|
|
|
<div class="sticky-top">
|
2019-04-04 20:01:10 -04:00
|
|
|
<h4>New Comments</h4>
|
2019-03-26 14:00:18 -04:00
|
|
|
{this.state.comments.map(comment =>
|
2019-04-15 19:12:06 -04:00
|
|
|
<CommentNodes nodes={[{comment: comment}]} noIndent locked={this.state.post.locked} moderators={this.state.moderators} />
|
2019-03-26 14:00:18 -04:00
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2019-03-27 12:59:21 -04:00
|
|
|
|
|
|
|
sidebar() {
|
|
|
|
return (
|
|
|
|
<div class="sticky-top">
|
2019-04-04 16:53:32 -04:00
|
|
|
<Sidebar community={this.state.community} moderators={this.state.moderators} />
|
2019-03-26 14:00:18 -04:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-03-30 02:08:02 -04:00
|
|
|
|
2019-04-08 01:19:02 -04:00
|
|
|
handleCommentSortChange(i: Post, event: any) {
|
2019-03-30 02:08:02 -04:00
|
|
|
i.state.commentSort = Number(event.target.value);
|
|
|
|
i.setState(i.state);
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
2019-03-30 02:08:02 -04:00
|
|
|
private buildCommentsTree(): Array<CommentNodeI> {
|
|
|
|
let map = new Map<number, CommentNodeI>();
|
|
|
|
for (let comment of this.state.comments) {
|
2019-03-27 12:59:21 -04:00
|
|
|
let node: CommentNodeI = {
|
2019-03-30 02:08:02 -04:00
|
|
|
comment: comment,
|
|
|
|
children: []
|
2019-03-27 12:59:21 -04:00
|
|
|
};
|
2019-03-30 02:08:02 -04:00
|
|
|
map.set(comment.id, { ...node });
|
2019-03-27 12:59:21 -04:00
|
|
|
}
|
|
|
|
let tree: Array<CommentNodeI> = [];
|
2019-03-30 02:08:02 -04:00
|
|
|
for (let comment of this.state.comments) {
|
|
|
|
if( comment.parent_id ) {
|
|
|
|
map.get(comment.parent_id).children.push(map.get(comment.id));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tree.push(map.get(comment.id));
|
|
|
|
}
|
2019-03-27 12:59:21 -04:00
|
|
|
}
|
2019-03-30 02:08:02 -04:00
|
|
|
|
|
|
|
this.sortTree(tree);
|
|
|
|
|
2019-03-27 12:59:21 -04:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2019-03-30 02:08:02 -04:00
|
|
|
sortTree(tree: Array<CommentNodeI>) {
|
|
|
|
|
|
|
|
if (this.state.commentSort == CommentSortType.Top) {
|
|
|
|
tree.sort((a, b) => b.comment.score - a.comment.score);
|
|
|
|
} else if (this.state.commentSort == CommentSortType.New) {
|
|
|
|
tree.sort((a, b) => b.comment.published.localeCompare(a.comment.published));
|
|
|
|
} else if (this.state.commentSort == CommentSortType.Hot) {
|
|
|
|
tree.sort((a, b) => hotRank(b.comment) - hotRank(a.comment));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let node of tree) {
|
|
|
|
this.sortTree(node.children);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-27 12:59:21 -04:00
|
|
|
commentsTree() {
|
|
|
|
let nodes = this.buildCommentsTree();
|
|
|
|
return (
|
2019-03-27 14:37:16 -04:00
|
|
|
<div className="">
|
2019-04-15 19:12:06 -04:00
|
|
|
<CommentNodes nodes={nodes} locked={this.state.post.locked} moderators={this.state.moderators} />
|
2019-03-27 12:59:21 -04:00
|
|
|
</div>
|
|
|
|
);
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
parseMessage(msg: any) {
|
|
|
|
console.log(msg);
|
|
|
|
let op: UserOperation = msgOp(msg);
|
|
|
|
if (msg.error) {
|
|
|
|
alert(msg.error);
|
|
|
|
return;
|
|
|
|
} else if (op == UserOperation.GetPost) {
|
2019-04-03 16:59:37 -04:00
|
|
|
let res: GetPostResponse = msg;
|
2019-03-26 14:00:18 -04:00
|
|
|
this.state.post = res.post;
|
2019-03-27 14:37:16 -04:00
|
|
|
this.state.comments = res.comments;
|
2019-04-03 19:01:20 -04:00
|
|
|
this.state.community = res.community;
|
2019-04-04 16:53:32 -04:00
|
|
|
this.state.moderators = res.moderators;
|
2019-04-08 17:46:09 -04:00
|
|
|
this.state.loading = false;
|
2019-03-27 12:59:21 -04:00
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.CreateComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
this.state.comments.unshift(res.comment);
|
2019-03-26 14:00:18 -04:00
|
|
|
this.setState(this.state);
|
2019-03-29 00:56:23 -04:00
|
|
|
} else if (op == UserOperation.EditComment) {
|
|
|
|
let res: CommentResponse = msg;
|
|
|
|
let found = this.state.comments.find(c => c.id == res.comment.id);
|
|
|
|
found.content = res.comment.content;
|
|
|
|
found.updated = res.comment.updated;
|
2019-04-15 19:12:06 -04:00
|
|
|
found.removed = res.comment.removed;
|
|
|
|
found.upvotes = res.comment.upvotes;
|
|
|
|
found.downvotes = res.comment.downvotes;
|
|
|
|
found.score = res.comment.score;
|
|
|
|
|
2019-03-29 00:56:23 -04:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
else if (op == UserOperation.CreateCommentLike) {
|
2019-04-03 19:01:20 -04:00
|
|
|
let res: CommentResponse = msg;
|
2019-03-28 15:32:08 -04:00
|
|
|
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;
|
|
|
|
this.setState(this.state);
|
2019-04-03 02:49:32 -04:00
|
|
|
} else if (op == UserOperation.CreatePostLike) {
|
|
|
|
let res: CreatePostLikeResponse = msg;
|
|
|
|
this.state.post.my_vote = res.post.my_vote;
|
|
|
|
this.state.post.score = res.post.score;
|
|
|
|
this.state.post.upvotes = res.post.upvotes;
|
|
|
|
this.state.post.downvotes = res.post.downvotes;
|
|
|
|
this.setState(this.state);
|
2019-04-03 16:59:37 -04:00
|
|
|
} else if (op == UserOperation.EditPost) {
|
|
|
|
let res: PostResponse = msg;
|
|
|
|
this.state.post = res.post;
|
|
|
|
this.setState(this.state);
|
2019-04-04 18:29:14 -04:00
|
|
|
} else if (op == UserOperation.EditCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community = res.community;
|
|
|
|
this.state.post.community_id = res.community.id;
|
|
|
|
this.state.post.community_name = res.community.name;
|
|
|
|
this.setState(this.state);
|
2019-04-05 02:26:38 -04:00
|
|
|
} else if (op == UserOperation.FollowCommunity) {
|
|
|
|
let res: CommunityResponse = msg;
|
|
|
|
this.state.community.subscribed = res.community.subscribed;
|
|
|
|
this.state.community.number_of_subscribers = res.community.number_of_subscribers;
|
|
|
|
this.setState(this.state);
|
2019-04-15 19:12:06 -04:00
|
|
|
} else if (op == UserOperation.BanFromCommunity) {
|
|
|
|
let res: BanFromCommunityResponse = msg;
|
|
|
|
this.state.comments.filter(c => c.creator_id == res.user.id)
|
|
|
|
.forEach(c => c.banned = res.banned);
|
|
|
|
this.setState(this.state);
|
|
|
|
} else if (op == UserOperation.AddModToCommunity) {
|
|
|
|
let res: AddModToCommunityResponse = msg;
|
|
|
|
this.state.moderators = res.moderators;
|
|
|
|
this.setState(this.state);
|
2019-03-26 14:00:18 -04:00
|
|
|
}
|
2019-03-27 12:59:21 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 00:56:23 -04:00
|
|
|
|
2019-03-27 12:59:21 -04:00
|
|
|
|