import { Link } from 'inferno-router';
import { Subscription } from 'rxjs';
import {
- CommentNode as CommentNodeI,
- CommentForm as CommentFormI,
- WebSocketJsonResponse,
+ CreateComment,
+ EditComment,
UserOperation,
CommentResponse,
} from 'lemmy-js-client';
-import { capitalizeFirstLetter, wsJsonToRes, wsSubscribe } from '../utils';
+import { CommentNode as CommentNodeI } from '../interfaces';
+import {
+ capitalizeFirstLetter,
+ wsJsonToRes,
+ wsSubscribe,
+ wsUserOp,
+} from '../utils';
import { WebSocketService, UserService } from '../services';
import { i18n } from '../i18next';
import { T } from 'inferno-i18next';
}
interface CommentFormState {
- commentForm: CommentFormI;
buttonTitle: string;
finished: boolean;
+ formId: string;
}
export class CommentForm extends Component<CommentFormProps, CommentFormState> {
private subscription: Subscription;
private emptyState: CommentFormState = {
- commentForm: {
- auth: null,
- content: null,
- post_id: this.props.node
- ? this.props.node.comment.post_id
- : this.props.postId,
- creator_id: UserService.Instance.user
- ? UserService.Instance.user.id
- : null,
- },
buttonTitle: !this.props.node
? capitalizeFirstLetter(i18n.t('post'))
: this.props.edit
? capitalizeFirstLetter(i18n.t('save'))
: capitalizeFirstLetter(i18n.t('reply')),
finished: false,
+ formId: null,
};
constructor(props: any, context: any) {
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;
- this.state.commentForm.creator_id = this.props.node.comment.creator_id;
- } else {
- // A reply gets a new parent id
- this.state.commentForm.parent_id = this.props.node.comment.id;
- }
- }
-
this.parseMessage = this.parseMessage.bind(this);
this.subscription = wsSubscribe(this.parseMessage);
}
<div class="mb-3">
{UserService.Instance.user ? (
<MarkdownTextArea
- initialContent={this.state.commentForm.content}
+ initialContent={
+ this.props.node
+ ? this.props.node.comment_view.comment.content
+ : null
+ }
buttonTitle={this.state.buttonTitle}
finished={this.state.finished}
replyType={!!this.props.node}
}
handleCommentSubmit(msg: { val: string; formId: string }) {
- this.state.commentForm.content = msg.val;
- this.state.commentForm.form_id = msg.formId;
+ let content = msg.val;
+ this.state.formId = msg.formId;
+
+ let node = this.props.node;
+
if (this.props.edit) {
- WebSocketService.Instance.editComment(this.state.commentForm);
+ let form: EditComment = {
+ content,
+ form_id: this.state.formId,
+ edit_id: node.comment_view.comment.id,
+ auth: UserService.Instance.authField(),
+ };
+ WebSocketService.Instance.client.editComment(form);
} else {
- WebSocketService.Instance.createComment(this.state.commentForm);
+ let form: CreateComment = {
+ content,
+ form_id: this.state.formId,
+ post_id: node ? node.comment_view.post.id : this.props.postId,
+ parent_id: node ? node.comment_view.comment.parent_id : null,
+ auth: UserService.Instance.authField(),
+ };
+ WebSocketService.Instance.client.createComment(form);
}
this.setState(this.state);
}
this.props.onReplyCancel();
}
- parseMessage(msg: WebSocketJsonResponse) {
- let res = wsJsonToRes(msg);
+ parseMessage(msg: any) {
+ let op = wsUserOp(msg);
// Only do the showing and hiding if logged in
if (UserService.Instance.user) {
if (
- res.op == UserOperation.CreateComment ||
- res.op == UserOperation.EditComment
+ op == UserOperation.CreateComment ||
+ op == UserOperation.EditComment
) {
- let data = res.data as CommentResponse;
+ let data = wsJsonToRes<CommentResponse>(msg).data;
// This only finishes this form, if the randomly generated form_id matches the one received
- if (this.state.commentForm.form_id == data.form_id) {
+ if (this.state.formId == data.form_id) {
this.setState({ finished: true });
- // Necessary because it broke tribute for some reaso
+ // Necessary because it broke tribute for some reason
this.setState({ finished: false });
}
}