2019-03-26 18:00:18 +00:00
|
|
|
import { Component, linkEvent } from 'inferno';
|
2019-04-03 20:59:37 +00:00
|
|
|
import { Link } from 'inferno-router';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-04-04 22:29:14 +00:00
|
|
|
import { UserOperation, Community, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentLikeForm, CommentSortType, CreatePostLikeResponse, CommunityUser, CommunityResponse } from '../interfaces';
|
2019-03-26 18:00:18 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-03-30 07:11:44 +00:00
|
|
|
import { msgOp, hotRank,mdToHtml } from '../utils';
|
2019-03-27 19:54:55 +00:00
|
|
|
import { MomentTime } from './moment-time';
|
2019-04-03 06:49:32 +00:00
|
|
|
import { PostListing } from './post-listing';
|
2019-04-03 23:01:20 +00:00
|
|
|
import { Sidebar } from './sidebar';
|
2019-03-31 00:38:54 +00:00
|
|
|
import * as autosize from 'autosize';
|
2019-03-26 18:00:18 +00:00
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
interface CommentNodeI {
|
|
|
|
comment: Comment;
|
|
|
|
children?: Array<CommentNodeI>;
|
|
|
|
};
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
interface PostState {
|
2019-03-26 18:00:18 +00:00
|
|
|
post: PostI;
|
|
|
|
comments: Array<Comment>;
|
2019-03-30 06:08:02 +00:00
|
|
|
commentSort: CommentSortType;
|
2019-04-03 23:01:20 +00:00
|
|
|
community: Community;
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: Array<CommunityUser>;
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-03 23:01:20 +00:00
|
|
|
export class Post extends Component<any, PostState> {
|
2019-03-26 18:00:18 +00:00
|
|
|
|
|
|
|
private subscription: Subscription;
|
2019-04-03 23:01:20 +00:00
|
|
|
private emptyState: PostState = {
|
2019-04-03 06:49:32 +00:00
|
|
|
post: null,
|
2019-03-30 06:08:02 +00:00
|
|
|
comments: [],
|
2019-04-03 23:01:20 +00:00
|
|
|
commentSort: CommentSortType.Hot,
|
|
|
|
community: null,
|
2019-04-04 20:53:32 +00:00
|
|
|
moderators: []
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-03-27 16:59:21 +00:00
|
|
|
|
2019-04-03 06:49:32 +00:00
|
|
|
let postId = 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-04-03 06:49:32 +00:00
|
|
|
WebSocketService.Instance.getPost(postId);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-03-31 00:38:54 +00:00
|
|
|
componentDidMount() {
|
|
|
|
autosize(document.querySelectorAll('textarea'));
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:00:18 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div class="container">
|
2019-04-03 06:49:32 +00:00
|
|
|
{this.state.post &&
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-12 col-sm-8 col-lg-7 mb-3">
|
2019-04-03 20:59:37 +00:00
|
|
|
<PostListing post={this.state.post} showBody showCommunity editable />
|
2019-04-03 06:49:32 +00:00
|
|
|
<div className="mb-2" />
|
|
|
|
<CommentForm postId={this.state.post.id} />
|
|
|
|
{this.sortRadios()}
|
|
|
|
{this.commentsTree()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-sm-4 col-lg-3 mb-3">
|
|
|
|
{this.state.comments.length > 0 && this.newComments()}
|
|
|
|
</div>
|
|
|
|
<div class="col-12 col-sm-12 col-lg-2">
|
|
|
|
{this.sidebar()}
|
|
|
|
</div>
|
2019-03-27 16:59:21 +00:00
|
|
|
</div>
|
2019-04-03 06:49:32 +00:00
|
|
|
}
|
2019-03-27 19:54:55 +00:00
|
|
|
</div>
|
2019-03-27 16:59:21 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:08:02 +00:00
|
|
|
sortRadios() {
|
|
|
|
return (
|
|
|
|
<div class="btn-group btn-group-toggle mb-3">
|
|
|
|
<label className={`btn btn-sm btn-secondary ${this.state.commentSort === CommentSortType.Hot && 'active'}`}>Hot
|
|
|
|
<input type="radio" value={CommentSortType.Hot}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Hot}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)} />
|
|
|
|
</label>
|
|
|
|
<label className={`btn btn-sm btn-secondary ${this.state.commentSort === CommentSortType.Top && 'active'}`}>Top
|
|
|
|
<input type="radio" value={CommentSortType.Top}
|
|
|
|
checked={this.state.commentSort === CommentSortType.Top}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)} />
|
|
|
|
</label>
|
|
|
|
<label className={`btn btn-sm btn-secondary ${this.state.commentSort === CommentSortType.New && 'active'}`}>New
|
|
|
|
<input type="radio" value={CommentSortType.New}
|
|
|
|
checked={this.state.commentSort === CommentSortType.New}
|
|
|
|
onChange={linkEvent(this, this.handleCommentSortChange)} />
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
newComments() {
|
|
|
|
return (
|
|
|
|
<div class="sticky-top">
|
2019-03-29 04:56:23 +00:00
|
|
|
<h5>New Comments</h5>
|
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">
|
2019-04-04 20:53:32 +00:00
|
|
|
<Sidebar community={this.state.community} moderators={this.state.moderators} />
|
2019-03-26 18:00:18 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2019-03-30 06:08:02 +00:00
|
|
|
|
|
|
|
handleCommentSortChange(i: Post, event) {
|
|
|
|
i.state.commentSort = Number(event.target.value);
|
|
|
|
i.setState(i.state);
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
|
2019-03-30 06:08:02 +00:00
|
|
|
private buildCommentsTree(): Array<CommentNodeI> {
|
|
|
|
let map = new Map<number, CommentNodeI>();
|
|
|
|
for (let comment of this.state.comments) {
|
2019-03-27 16:59:21 +00:00
|
|
|
let node: CommentNodeI = {
|
2019-03-30 06:08:02 +00:00
|
|
|
comment: comment,
|
|
|
|
children: []
|
2019-03-27 16:59:21 +00:00
|
|
|
};
|
2019-03-30 06:08:02 +00:00
|
|
|
map.set(comment.id, { ...node });
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
let tree: Array<CommentNodeI> = [];
|
2019-03-30 06:08:02 +00: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 16:59:21 +00:00
|
|
|
}
|
2019-03-30 06:08:02 +00:00
|
|
|
|
|
|
|
this.sortTree(tree);
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2019-03-30 06:08:02 +00: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 16:59:21 +00:00
|
|
|
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) {
|
2019-04-03 20:59:37 +00:00
|
|
|
let res: GetPostResponse = msg;
|
2019-03-26 18:00:18 +00:00
|
|
|
this.state.post = res.post;
|
2019-03-27 18:37:16 +00:00
|
|
|
this.state.comments = res.comments;
|
2019-04-03 23:01:20 +00:00
|
|
|
this.state.community = res.community;
|
2019-04-04 20:53:32 +00:00
|
|
|
this.state.moderators = res.moderators;
|
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-29 04:56:23 +00: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;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
else if (op == UserOperation.CreateCommentLike) {
|
2019-04-03 23:01:20 +00:00
|
|
|
let res: CommentResponse = msg;
|
2019-03-28 19:32:08 +00: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 06:49:32 +00: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 20:59:37 +00:00
|
|
|
} else if (op == UserOperation.EditPost) {
|
|
|
|
let res: PostResponse = msg;
|
|
|
|
this.state.post = res.post;
|
|
|
|
this.setState(this.state);
|
2019-04-04 22:29:14 +00: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-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;
|
2019-03-29 04:56:23 +00:00
|
|
|
showEdit: boolean;
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentNodeProps {
|
|
|
|
node: CommentNodeI;
|
|
|
|
noIndent?: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentNode extends Component<CommentNodeProps, CommentNodeState> {
|
|
|
|
|
|
|
|
private emptyState: CommentNodeState = {
|
2019-03-29 04:56:23 +00:00
|
|
|
showReply: false,
|
|
|
|
showEdit: false
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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">
|
2019-04-03 20:59:37 +00:00
|
|
|
<Link to={`/user/${node.comment.creator_id}`}>{node.comment.creator_name}</Link>
|
2019-03-28 19:32:08 +00:00
|
|
|
</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>
|
2019-03-29 04:56:23 +00:00
|
|
|
{this.state.showEdit && <CommentForm node={node} edit onReplyCancel={this.handleReplyCancel} />}
|
|
|
|
{!this.state.showEdit &&
|
|
|
|
<div>
|
2019-03-30 07:11:44 +00:00
|
|
|
<div className="md-div" dangerouslySetInnerHTML={mdToHtml(node.comment.content)} />
|
2019-03-29 04:56:23 +00:00
|
|
|
<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>
|
|
|
|
{this.myComment &&
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
|
|
|
|
</li>
|
|
|
|
}
|
2019-03-29 06:03:17 +00:00
|
|
|
{this.myComment &&
|
|
|
|
<li className="list-inline-item">
|
|
|
|
<span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span>
|
|
|
|
</li>
|
|
|
|
}
|
2019-03-29 04:56:23 +00:00
|
|
|
<li className="list-inline-item">
|
|
|
|
<a className="text-muted" href="test">link</a>
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
</div>
|
|
|
|
{this.state.showReply && <CommentForm node={node} onReplyCancel={this.handleReplyCancel} />}
|
2019-03-30 06:08:02 +00:00
|
|
|
{this.props.node.children && <CommentNodes nodes={this.props.node.children} />}
|
2019-03-28 19:32:08 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
private get myComment(): boolean {
|
2019-04-03 20:59:37 +00:00
|
|
|
return UserService.Instance.loggedIn && this.props.node.comment.creator_id == UserService.Instance.user.id;
|
2019-03-28 19:32:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleReplyClick(i: CommentNode, event) {
|
|
|
|
i.state.showReply = true;
|
|
|
|
i.setState(i.state);
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
handleEditClick(i: CommentNode, event) {
|
|
|
|
i.state.showEdit = true;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
2019-03-29 06:03:17 +00:00
|
|
|
handleDeleteClick(i: CommentNode, event) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
handleReplyCancel(): any {
|
|
|
|
this.state.showReply = false;
|
2019-03-29 04:56:23 +00:00
|
|
|
this.state.showEdit = false;
|
2019-03-27 16:59:21 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
2019-03-28 19:32:08 +00:00
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
|
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-29 04:56:23 +00:00
|
|
|
edit?: boolean;
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentFormState {
|
|
|
|
commentForm: CommentFormI;
|
2019-03-29 04:56:23 +00:00
|
|
|
buttonTitle: string;
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
|
|
|
|
|
|
|
private emptyState: CommentFormState = {
|
|
|
|
commentForm: {
|
|
|
|
auth: null,
|
|
|
|
content: null,
|
2019-03-29 04:56:23 +00:00
|
|
|
post_id: this.props.node ? this.props.node.comment.post_id : this.props.postId
|
|
|
|
},
|
|
|
|
buttonTitle: !this.props.node ? "Post" : this.props.edit ? "Edit" : "Reply"
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
2019-03-29 04:56:23 +00:00
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
if (this.props.node) {
|
2019-03-29 04:56:23 +00:00
|
|
|
if (this.props.edit) {
|
|
|
|
this.state.commentForm.edit_id = this.props.node.comment.id;
|
|
|
|
this.state.commentForm.parent_id = this.props.node.comment.parent_id;
|
|
|
|
this.state.commentForm.content = this.props.node.comment.content;
|
|
|
|
} else {
|
|
|
|
// A reply gets a new parent id
|
|
|
|
this.state.commentForm.parent_id = this.props.node.comment.id;
|
|
|
|
}
|
|
|
|
}
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-31 00:38:54 +00:00
|
|
|
componentDidMount() {
|
|
|
|
autosize(document.querySelectorAll('textarea'));
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<div>
|
2019-03-29 04:56:23 +00:00
|
|
|
<form onSubmit={linkEvent(this, this.handleCommentSubmit)}>
|
2019-03-27 16:59:21 +00:00
|
|
|
<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">
|
2019-03-30 06:08:02 +00:00
|
|
|
<button type="submit" class="btn btn-sm btn-secondary mr-2">{this.state.buttonTitle}</button>
|
|
|
|
{this.props.node && <button type="button" class="btn btn-sm btn-secondary" onClick={linkEvent(this, this.handleReplyCancel)}>Cancel</button>}
|
2019-03-27 16:59:21 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-29 04:56:23 +00:00
|
|
|
handleCommentSubmit(i: CommentForm, event) {
|
|
|
|
if (i.props.edit) {
|
|
|
|
WebSocketService.Instance.editComment(i.state.commentForm);
|
|
|
|
} else {
|
|
|
|
WebSocketService.Instance.createComment(i.state.commentForm);
|
|
|
|
}
|
|
|
|
|
2019-03-27 16:59:21 +00:00
|
|
|
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) {
|
|
|
|
i.state.commentForm.content = event.target.value;
|
2019-03-29 04:56:23 +00:00
|
|
|
i.setState(i.state);
|
2019-03-27 16:59:21 +00:00
|
|
|
}
|
|
|
|
|
2019-03-28 19:32:08 +00:00
|
|
|
handleReplyCancel(i: CommentForm, event) {
|
|
|
|
i.props.onReplyCancel();
|
2019-03-26 18:00:18 +00:00
|
|
|
}
|
|
|
|
}
|