]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/components/comment-form.tsx
First pass at v2_api
[lemmy-ui.git] / src / shared / components / comment-form.tsx
index b032a065c6dc869b312410462b88f2d3c1e266af..5a0d404a9c8d3cb61906ed596e32ddcc06311153 100644 (file)
@@ -2,13 +2,18 @@ import { Component } from 'inferno';
 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';
@@ -24,30 +29,21 @@ interface CommentFormProps {
 }
 
 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) {
@@ -58,18 +54,6 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
 
     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);
   }
@@ -83,7 +67,11 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
       <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}
@@ -110,12 +98,28 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
   }
 
   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);
   }
@@ -124,22 +128,22 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
     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 });
         }
       }