2020-07-17 01:12:51 +00:00
|
|
|
import { Component } from 'inferno';
|
2020-07-10 00:03:47 +00:00
|
|
|
import { Link } from 'inferno-router';
|
2020-03-08 22:47:27 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
import { retryWhen, delay, take } from 'rxjs/operators';
|
2019-10-19 00:20:27 +00:00
|
|
|
import {
|
|
|
|
CommentNode as CommentNodeI,
|
|
|
|
CommentForm as CommentFormI,
|
2020-03-08 22:47:27 +00:00
|
|
|
WebSocketJsonResponse,
|
|
|
|
UserOperation,
|
|
|
|
CommentResponse,
|
2019-10-19 00:20:27 +00:00
|
|
|
} from '../interfaces';
|
2020-07-17 01:12:51 +00:00
|
|
|
import { capitalizeFirstLetter, wsJsonToRes } from '../utils';
|
2019-04-15 23:12:06 +00:00
|
|
|
import { WebSocketService, UserService } from '../services';
|
2019-08-10 00:14:43 +00:00
|
|
|
import { i18n } from '../i18next';
|
2020-07-10 00:03:47 +00:00
|
|
|
import { T } from 'inferno-i18next';
|
2020-07-17 01:12:51 +00:00
|
|
|
import { MarkdownTextArea } from './markdown-textarea';
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
interface CommentFormProps {
|
|
|
|
postId?: number;
|
|
|
|
node?: CommentNodeI;
|
|
|
|
onReplyCancel?(): any;
|
|
|
|
edit?: boolean;
|
2019-04-15 23:12:06 +00:00
|
|
|
disabled?: boolean;
|
2020-07-12 01:47:38 +00:00
|
|
|
focus?: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface CommentFormState {
|
|
|
|
commentForm: CommentFormI;
|
|
|
|
buttonTitle: string;
|
2020-07-17 01:12:51 +00:00
|
|
|
finished: boolean;
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
|
2020-03-08 22:47:27 +00:00
|
|
|
private subscription: Subscription;
|
2019-04-08 05:19:02 +00:00
|
|
|
private emptyState: CommentFormState = {
|
|
|
|
commentForm: {
|
|
|
|
auth: null,
|
|
|
|
content: null,
|
2019-10-19 00:20:27 +00:00
|
|
|
post_id: this.props.node
|
|
|
|
? this.props.node.comment.post_id
|
|
|
|
: this.props.postId,
|
|
|
|
creator_id: UserService.Instance.user
|
|
|
|
? UserService.Instance.user.id
|
|
|
|
: null,
|
2019-04-08 05:19:02 +00:00
|
|
|
},
|
2019-10-19 00:20:27 +00:00
|
|
|
buttonTitle: !this.props.node
|
|
|
|
? capitalizeFirstLetter(i18n.t('post'))
|
|
|
|
: this.props.edit
|
2020-06-22 22:33:51 +00:00
|
|
|
? capitalizeFirstLetter(i18n.t('save'))
|
2019-10-19 00:20:27 +00:00
|
|
|
: capitalizeFirstLetter(i18n.t('reply')),
|
2020-07-17 01:12:51 +00:00
|
|
|
finished: false,
|
2019-10-19 00:20:27 +00:00
|
|
|
};
|
2019-04-08 05:19:02 +00:00
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
2020-07-17 01:12:51 +00:00
|
|
|
this.handleCommentSubmit = this.handleCommentSubmit.bind(this);
|
|
|
|
this.handleReplyCancel = this.handleReplyCancel.bind(this);
|
2020-04-12 16:47:48 +00:00
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
this.state = this.emptyState;
|
|
|
|
|
|
|
|
if (this.props.node) {
|
|
|
|
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;
|
2019-04-15 23:12:06 +00:00
|
|
|
this.state.commentForm.creator_id = this.props.node.comment.creator_id;
|
2019-04-08 05:19:02 +00:00
|
|
|
} else {
|
|
|
|
// A reply gets a new parent id
|
|
|
|
this.state.commentForm.parent_id = this.props.node.comment.id;
|
|
|
|
}
|
2019-10-19 00:20:27 +00:00
|
|
|
}
|
2020-03-08 22:47:27 +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-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 22:47:27 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2019-04-08 05:19:02 +00:00
|
|
|
render() {
|
|
|
|
return (
|
2019-08-18 01:59:59 +00:00
|
|
|
<div class="mb-3">
|
2020-07-10 00:03:47 +00:00
|
|
|
{UserService.Instance.user ? (
|
2020-07-17 01:12:51 +00:00
|
|
|
<MarkdownTextArea
|
|
|
|
initialContent={this.state.commentForm.content}
|
|
|
|
buttonTitle={this.state.buttonTitle}
|
|
|
|
finished={this.state.finished}
|
|
|
|
replyType={!!this.props.node}
|
|
|
|
focus={this.props.focus}
|
|
|
|
disabled={this.props.disabled}
|
|
|
|
onSubmit={this.handleCommentSubmit}
|
|
|
|
onReplyCancel={this.handleReplyCancel}
|
|
|
|
/>
|
2020-07-10 00:03:47 +00:00
|
|
|
) : (
|
2020-07-14 17:21:31 +00:00
|
|
|
<div class="alert alert-light" role="alert">
|
2020-07-10 00:03:47 +00:00
|
|
|
<svg class="icon icon-inline mr-2">
|
|
|
|
<use xlinkHref="#icon-alert-triangle"></use>
|
|
|
|
</svg>
|
|
|
|
<T i18nKey="must_login" class="d-inline">
|
2020-07-14 17:17:51 +00:00
|
|
|
#
|
|
|
|
<Link class="alert-link" to="/login">
|
|
|
|
#
|
|
|
|
</Link>
|
2020-07-10 00:03:47 +00:00
|
|
|
</T>
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
2020-07-10 00:03:47 +00:00
|
|
|
)}
|
2019-04-08 05:19:02 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-06-27 13:03:34 +00:00
|
|
|
handleFinished(op: UserOperation, data: CommentResponse) {
|
2020-06-23 16:52:07 +00:00
|
|
|
let isReply =
|
|
|
|
this.props.node !== undefined && data.comment.parent_id !== null;
|
|
|
|
let xor =
|
|
|
|
+!(data.comment.parent_id !== null) ^ +(this.props.node !== undefined);
|
|
|
|
|
|
|
|
if (
|
|
|
|
(data.comment.creator_id == UserService.Instance.user.id &&
|
2020-06-27 13:03:34 +00:00
|
|
|
((op == UserOperation.CreateComment &&
|
|
|
|
// If its a reply, make sure parent child match
|
|
|
|
isReply &&
|
|
|
|
data.comment.parent_id == this.props.node.comment.id) ||
|
|
|
|
// Otherwise, check the XOR of the two
|
|
|
|
(!isReply && xor))) ||
|
|
|
|
// If its a comment edit, only check that its from your user, and that its a
|
|
|
|
// text edit only
|
|
|
|
|
2020-07-02 00:38:11 +00:00
|
|
|
(data.comment.creator_id == UserService.Instance.user.id &&
|
|
|
|
op == UserOperation.EditComment &&
|
|
|
|
data.comment.content)
|
2020-06-23 16:52:07 +00:00
|
|
|
) {
|
2020-07-17 01:12:51 +00:00
|
|
|
this.state.finished = true;
|
2020-06-23 16:52:07 +00:00
|
|
|
this.setState(this.state);
|
2020-03-08 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:12:51 +00:00
|
|
|
handleCommentSubmit(val: string) {
|
|
|
|
this.state.commentForm.content = val;
|
|
|
|
if (this.props.edit) {
|
|
|
|
WebSocketService.Instance.editComment(this.state.commentForm);
|
2019-04-08 05:19:02 +00:00
|
|
|
} else {
|
2020-07-17 01:12:51 +00:00
|
|
|
WebSocketService.Instance.createComment(this.state.commentForm);
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
2020-07-17 01:12:51 +00:00
|
|
|
this.setState(this.state);
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|
|
|
|
|
2020-07-17 01:12:51 +00:00
|
|
|
handleReplyCancel() {
|
|
|
|
this.props.onReplyCancel();
|
2019-09-08 03:42:01 +00:00
|
|
|
}
|
2020-03-08 22:47:27 +00:00
|
|
|
|
|
|
|
parseMessage(msg: WebSocketJsonResponse) {
|
|
|
|
let res = wsJsonToRes(msg);
|
|
|
|
|
|
|
|
// Only do the showing and hiding if logged in
|
|
|
|
if (UserService.Instance.user) {
|
|
|
|
if (res.op == UserOperation.CreateComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-06-27 13:03:34 +00:00
|
|
|
this.handleFinished(res.op, data);
|
2020-03-08 22:47:27 +00:00
|
|
|
} else if (res.op == UserOperation.EditComment) {
|
|
|
|
let data = res.data as CommentResponse;
|
2020-06-27 13:03:34 +00:00
|
|
|
this.handleFinished(res.op, data);
|
2020-03-08 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|