فهرست منبع

like public tweets

Carsten Porth 6 سال پیش
والد
کامیت
ba64095193

+ 3 - 2
app/src/components/tweet-actions/tweet-actions.html

@@ -7,8 +7,9 @@
     <ion-icon name="ios-git-compare-outline"></ion-icon>
     <span>{{ data.retweet_count | friendlyNumber }}</span>
   </div>
-  <div class="likes">
-    <ion-icon name="ios-heart-outline"></ion-icon>
+  <div class="likes" *ngIf="!data.private_tweet">
+    <ion-icon *ngIf="data.favorited" name="ios-heart" (click)="removeLike(data.id_str)" color="danger"></ion-icon>
+    <ion-icon *ngIf="!data.favorited" name="ios-heart-outline" (click)="like(data.id_str)"></ion-icon>
     <span>{{ data.favorite_count | friendlyNumber }}</span>
   </div>
 </div>

+ 18 - 1
app/src/components/tweet-actions/tweet-actions.ts

@@ -1,4 +1,5 @@
 import { Component, Input } from "@angular/core";
+import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
 
 /**
  * Generated class for the TweetActionsComponent component.
@@ -14,5 +15,21 @@ export class TweetActionsComponent {
   @Input()
   data: any[];
 
-  constructor() {}
+  constructor(private twitter: TwitterApiProvider) {}
+
+  like(id) {
+    if (!this.data["private_tweet"]) {
+      this.twitter.likeTweet(id);
+      this.data["favorited"] = true;
+      this.data["favorite_count"]++;
+    }
+  }
+
+  removeLike(id) {
+    if (!this.data["private_tweet"]) {
+      this.twitter.unlikeTweet(id);
+      this.data["favorited"] = false;
+      this.data["favorite_count"]--;
+    }
+  }
 }

+ 8 - 0
app/src/providers/twitter-api/twitter-api.ts

@@ -112,4 +112,12 @@ export class TwitterApiProvider {
 
     return friends;
   }
+
+  public async likeTweet(tweetId) {
+    return await this.client.post("favorites/create", { id: tweetId });
+  }
+
+  public async unlikeTweet(tweetId) {
+    return await this.client.post("favorites/destroy", { id: tweetId });
+  }
 }