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-07-21 14:56:41 +00:00
|
|
|
handleCommentSubmit(msg: { val: string; formId: string }) {
|
|
|
|
this.state.commentForm.content = msg.val;
|
|
|
|
this.state.commentForm.form_id = msg.formId;
|
2020-07-17 01:12:51 +00:00
|
|
|
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) {
|
2020-07-21 14:56:41 +00:00
|
|
|
if (
|
|
|
|
res.op == UserOperation.CreateComment ||
|
|
|
|
res.op == UserOperation.EditComment
|
|
|
|
) {
|
2020-03-08 22:47:27 +00:00
|
|
|
let data = res.data as CommentResponse;
|
2020-07-21 14:56:41 +00:00
|
|
|
|
|
|
|
// This only finishes this form, if the randomly generated form_id matches the one received
|
|
|
|
if (this.state.commentForm.form_id == data.form_id) {
|
|
|
|
this.setState({ finished: true });
|
|
|
|
}
|
2020-03-08 22:47:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-08 05:19:02 +00:00
|
|
|
}
|