]> Untitled Git - lemmy-ui.git/blobdiff - src/shared/components/post/post.tsx
Changing all bigints to numbers
[lemmy-ui.git] / src / shared / components / post / post.tsx
index 637cc927de6bcffa3de451ab28334693b2d21edb..96d7911ec792307ffd2f9ef58ac60ddca4062d32 100644 (file)
@@ -6,7 +6,6 @@ import {
   BanFromCommunityResponse,
   BanPersonResponse,
   BlockPersonResponse,
-  CommentNode as CommentNodeI,
   CommentReportResponse,
   CommentResponse,
   CommentSortType,
@@ -17,22 +16,23 @@ import {
   GetPost,
   GetPostResponse,
   GetSiteResponse,
-  ListingType,
   PostReportResponse,
   PostResponse,
   PostView,
   PurgeItemResponse,
   Search,
   SearchResponse,
-  SearchType,
-  SortType,
   UserOperation,
   wsJsonToRes,
   wsUserOp,
 } from "lemmy-js-client";
 import { Subscription } from "rxjs";
 import { i18n } from "../../i18next";
-import { CommentViewType, InitialFetchRequest } from "../../interfaces";
+import {
+  CommentNodeI,
+  CommentViewType,
+  InitialFetchRequest,
+} from "../../interfaces";
 import { UserService, WebSocketService } from "../../services";
 import {
   buildCommentsTree,
@@ -97,7 +97,7 @@ export class Post extends Component<any, PostState> {
     postId: getIdFromProps(this.props),
     commentId: getCommentIdFromProps(this.props),
     commentTree: [],
-    commentSort: CommentSortType[CommentSortType.Hot],
+    commentSort: "Hot",
     commentViewType: CommentViewType.Tree,
     scrolled: false,
     loading: true,
@@ -161,7 +161,6 @@ export class Post extends Component<any, PostState> {
   }
 
   fetchPost() {
-    this.setState({ commentsRes: undefined, postRes: undefined });
     let auth = myAuth(false);
     let postForm: GetPost = {
       id: this.state.postId,
@@ -175,7 +174,7 @@ export class Post extends Component<any, PostState> {
       parent_id: this.state.commentId,
       max_depth: commentTreeMaxDepth,
       sort: this.state.commentSort,
-      type_: ListingType.All,
+      type_: "All",
       saved_only: false,
       auth,
     };
@@ -187,9 +186,9 @@ export class Post extends Component<any, PostState> {
     if (q) {
       let form: Search = {
         q,
-        type_: SearchType.Url,
-        sort: SortType.TopAll,
-        listing_type: ListingType.All,
+        type_: "Url",
+        sort: "TopAll",
+        listing_type: "All",
         page: 1,
         limit: trendingFetchLimit,
         auth: myAuth(false),
@@ -212,8 +211,8 @@ export class Post extends Component<any, PostState> {
 
     let commentsForm: GetComments = {
       max_depth: commentTreeMaxDepth,
-      sort: CommentSortType.Hot,
-      type_: ListingType.All,
+      sort: "Hot",
+      type_: "All",
       saved_only: false,
       auth,
     };
@@ -374,57 +373,53 @@ export class Post extends Component<any, PostState> {
         <div className="btn-group btn-group-toggle flex-wrap mr-3 mb-2">
           <label
             className={`btn btn-outline-secondary pointer ${
-              CommentSortType[this.state.commentSort] === CommentSortType.Hot &&
-              "active"
+              this.state.commentSort === "Hot" && "active"
             }`}
           >
             {i18n.t("hot")}
             <input
               type="radio"
-              value={CommentSortType.Hot}
-              checked={this.state.commentSort === CommentSortType.Hot}
+              value={"Hot"}
+              checked={this.state.commentSort === "Hot"}
               onChange={linkEvent(this, this.handleCommentSortChange)}
             />
           </label>
           <label
             className={`btn btn-outline-secondary pointer ${
-              CommentSortType[this.state.commentSort] === CommentSortType.Top &&
-              "active"
+              this.state.commentSort === "Top" && "active"
             }`}
           >
             {i18n.t("top")}
             <input
               type="radio"
-              value={CommentSortType.Top}
-              checked={this.state.commentSort === CommentSortType.Top}
+              value={"Top"}
+              checked={this.state.commentSort === "Top"}
               onChange={linkEvent(this, this.handleCommentSortChange)}
             />
           </label>
           <label
             className={`btn btn-outline-secondary pointer ${
-              CommentSortType[this.state.commentSort] === CommentSortType.New &&
-              "active"
+              this.state.commentSort === "New" && "active"
             }`}
           >
             {i18n.t("new")}
             <input
               type="radio"
-              value={CommentSortType.New}
-              checked={this.state.commentSort === CommentSortType.New}
+              value={"New"}
+              checked={this.state.commentSort === "New"}
               onChange={linkEvent(this, this.handleCommentSortChange)}
             />
           </label>
           <label
             className={`btn btn-outline-secondary pointer ${
-              CommentSortType[this.state.commentSort] === CommentSortType.Old &&
-              "active"
+              this.state.commentSort === "Old" && "active"
             }`}
           >
             {i18n.t("old")}
             <input
               type="radio"
-              value={CommentSortType.Old}
-              checked={this.state.commentSort === CommentSortType.Old}
+              value={"Old"}
+              checked={this.state.commentSort === "Old"}
               onChange={linkEvent(this, this.handleCommentSortChange)}
             />
           </label>
@@ -496,8 +491,10 @@ export class Post extends Component<any, PostState> {
 
   handleCommentSortChange(i: Post, event: any) {
     i.setState({
-      commentSort: CommentSortType[event.target.value],
+      commentSort: event.target.value as CommentSortType,
       commentViewType: CommentViewType.Tree,
+      commentsRes: undefined,
+      postRes: undefined,
     });
     i.fetchPost();
   }
@@ -507,7 +504,7 @@ export class Post extends Component<any, PostState> {
     if (comments) {
       i.setState({
         commentViewType: Number(event.target.value),
-        commentSort: CommentSortType.New,
+        commentSort: "New",
         commentTree: buildCommentsTree(comments, !!i.state.commentId),
       });
     }
@@ -650,6 +647,7 @@ export class Post extends Component<any, PostState> {
         data.recipient_ids.length == 0 &&
         !creatorBlocked &&
         postRes &&
+        data.comment_view.post.id == postRes.post_view.post.id &&
         commentsRes
       ) {
         commentsRes.comments.unshift(data.comment_view);
@@ -694,9 +692,9 @@ export class Post extends Component<any, PostState> {
       op == UserOperation.SavePost
     ) {
       let data = wsJsonToRes<PostResponse>(msg);
-      let pv = this.state.postRes?.post_view;
-      if (pv) {
-        pv = data.post_view;
+      let res = this.state.postRes;
+      if (res) {
+        res.post_view = data.post_view;
         this.setState(this.state);
         setupTippy();
       }
@@ -716,10 +714,10 @@ export class Post extends Component<any, PostState> {
     } else if (op == UserOperation.BanFromCommunity) {
       let data = wsJsonToRes<BanFromCommunityResponse>(msg);
 
-      let postRes = this.state.postRes;
-      if (postRes) {
-        if (postRes.post_view.creator.id == data.person_view.person.id) {
-          postRes.post_view.creator_banned_from_community = data.banned;
+      let res = this.state.postRes;
+      if (res) {
+        if (res.post_view.creator.id == data.person_view.person.id) {
+          res.post_view.creator_banned_from_community = data.banned;
         }
       }
 
@@ -729,9 +727,9 @@ export class Post extends Component<any, PostState> {
       this.setState(this.state);
     } else if (op == UserOperation.AddModToCommunity) {
       let data = wsJsonToRes<AddModToCommunityResponse>(msg);
-      let postRes = this.state.postRes;
-      if (postRes) {
-        postRes.moderators = data.moderators;
+      let res = this.state.postRes;
+      if (res) {
+        res.moderators = data.moderators;
         this.setState(this.state);
       }
     } else if (op == UserOperation.BanPerson) {
@@ -740,10 +738,10 @@ export class Post extends Component<any, PostState> {
         .filter(c => c.creator.id == data.person_view.person.id)
         .forEach(c => (c.creator.banned = data.banned));
 
-      let postRes = this.state.postRes;
-      if (postRes) {
-        if (postRes.post_view.creator.id == data.person_view.person.id) {
-          postRes.post_view.creator.banned = data.banned;
+      let res = this.state.postRes;
+      if (res) {
+        if (res.post_view.creator.id == data.person_view.person.id) {
+          res.post_view.creator.banned = data.banned;
         }
       }
       this.setState(this.state);
@@ -753,7 +751,7 @@ export class Post extends Component<any, PostState> {
     } else if (op == UserOperation.Search) {
       let data = wsJsonToRes<SearchResponse>(msg);
       let xPosts = data.posts.filter(
-        p => p.post.id != Number(this.props.match.params.id)
+        p => p.post.ap_id != this.state.postRes?.post_view.post.ap_id
       );
       this.setState({ crossPosts: xPosts.length > 0 ? xPosts : undefined });
     } else if (op == UserOperation.LeaveAdmin) {
@@ -761,11 +759,11 @@ export class Post extends Component<any, PostState> {
       this.setState({ siteRes: data });
     } else if (op == UserOperation.TransferCommunity) {
       let data = wsJsonToRes<GetCommunityResponse>(msg);
-      let postRes = this.state.postRes;
-      if (postRes) {
-        postRes.community_view = data.community_view;
-        postRes.post_view.community = data.community_view.community;
-        postRes.moderators = data.moderators;
+      let res = this.state.postRes;
+      if (res) {
+        res.community_view = data.community_view;
+        res.post_view.community = data.community_view.community;
+        res.moderators = data.moderators;
         this.setState(this.state);
       }
     } else if (op == UserOperation.BlockPerson) {