about summary refs log tree commit diff
path: root/src/state/models/post-thread-view.ts
diff options
context:
space:
mode:
authorPaul Frazee <pfrazee@gmail.com>2022-11-08 12:14:51 -0600
committerPaul Frazee <pfrazee@gmail.com>2022-11-08 12:14:51 -0600
commit1fbc4cf1f2279cf4cf6804ff02616515e82d2a38 (patch)
tree0796e390ba5a04e5c46ea9a560809803252fda78 /src/state/models/post-thread-view.ts
parente650d98924051abfee40ff956f7348e2e47e7cd7 (diff)
downloadvoidsky-1fbc4cf1f2279cf4cf6804ff02616515e82d2a38.tar.zst
Finish the upvote/downvote implementation
Diffstat (limited to 'src/state/models/post-thread-view.ts')
-rw-r--r--src/state/models/post-thread-view.ts49
1 files changed, 38 insertions, 11 deletions
diff --git a/src/state/models/post-thread-view.ts b/src/state/models/post-thread-view.ts
index 0fc0dadbb..7a735de03 100644
--- a/src/state/models/post-thread-view.ts
+++ b/src/state/models/post-thread-view.ts
@@ -13,8 +13,9 @@ function* reactKeyGenerator(): Generator<string> {
 }
 
 export class PostThreadViewPostMyStateModel {
-  like?: string
   repost?: string
+  upvote?: string
+  downvote?: string
 
   constructor() {
     makeAutoObservable(this)
@@ -40,7 +41,8 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
   replyCount: number = 0
   replies?: PostThreadViewPostModel[]
   repostCount: number = 0
-  likeCount: number = 0
+  upvoteCount: number = 0
+  downvoteCount: number = 0
   indexedAt: string = ''
   myState = new PostThreadViewPostMyStateModel()
 
@@ -105,18 +107,43 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
     }
   }
 
-  async toggleLike() {
-    if (this.myState.like) {
-      await apilib.unlike(this.rootStore, this.myState.like)
+  async _clearVotes() {
+    if (this.myState.upvote) {
+      await apilib.unupvote(this.rootStore, this.myState.upvote)
       runInAction(() => {
-        this.likeCount--
-        this.myState.like = undefined
+        this.upvoteCount--
+        this.myState.upvote = undefined
       })
-    } else {
-      const res = await apilib.like(this.rootStore, this.uri, this.cid)
+    }
+    if (this.myState.downvote) {
+      await apilib.undownvote(this.rootStore, this.myState.downvote)
+      runInAction(() => {
+        this.downvoteCount--
+        this.myState.downvote = undefined
+      })
+    }
+  }
+
+  async toggleUpvote() {
+    const wasntUpvoted = !this.myState.upvote
+    await this._clearVotes()
+    if (wasntUpvoted) {
+      const res = await apilib.upvote(this.rootStore, this.uri, this.cid)
+      runInAction(() => {
+        this.upvoteCount++
+        this.myState.upvote = res.uri
+      })
+    }
+  }
+
+  async toggleDownvote() {
+    const wasntDownvoted = !this.myState.downvote
+    await this._clearVotes()
+    if (wasntDownvoted) {
+      const res = await apilib.downvote(this.rootStore, this.uri, this.cid)
       runInAction(() => {
-        this.likeCount++
-        this.myState.like = res.uri
+        this.downvoteCount++
+        this.myState.downvote = res.uri
       })
     }
   }