Browse Source

sort tweets

Carsten Porth 5 years ago
parent
commit
99cb6147ce
3 changed files with 356 additions and 189 deletions
  1. 327 182
      app/package-lock.json
  2. 1 1
      app/package.json
  3. 28 6
      app/src/pages/home/home.ts

File diff suppressed because it is too large
+ 327 - 182
app/package-lock.json


+ 1 - 1
app/package.json

@@ -51,7 +51,7 @@
     "zone.js": "0.8.26"
   },
   "devDependencies": {
-    "@ionic/app-scripts": "3.1.10",
+    "@ionic/app-scripts": "^3.2.0",
     "typescript": "~2.6.2"
   },
   "description": "HybridOSN is a Twitter client and a so called Dapp which allows its users to exchange data also via a P2P network using blockchain technology.",

+ 28 - 6
app/src/pages/home/home.ts

@@ -34,15 +34,22 @@ export class HomePage {
   ionViewDidEnter() {
     this.twitter
       .fetchHomeFeed()
-      .then(res => (this.data = this.data.concat(res.data)));
+      .then(
+        res =>
+          (this.data = this.data
+            .concat(res.data)
+            .sort((a, b) => this.sortByDateAsc(a, b)))
+      );
     this.gun
       .getLastTweetFromUser("username")
       .then(hash => this.ipfs.fetchTweet(hash))
       .then(tweet => this.addUserObject(tweet))
-      .then(res => {
-        this.data = this.data.concat(res);
-        console.log(this.data, res);
-      });
+      .then(
+        res =>
+          (this.data = this.data
+            .concat(res)
+            .sort((a, b) => this.sortByDateAsc(a, b)))
+      );
   }
 
   doRefresh(refresher) {
@@ -57,7 +64,9 @@ export class HomePage {
     this.twitter
       .fetchHomeFeedSince(this.data[this.data.length - 1].id)
       .then(res => {
-        this.data = this.data.concat(res.data);
+        this.data = this.data
+          .concat(res.data)
+          .sort((a, b) => this.sortByDateAsc(a, b));
         infiniteScroll.complete();
       });
   }
@@ -70,4 +79,17 @@ export class HomePage {
     tweet.user = await this.twitter.fetchUser(tweet.user_id);
     return tweet;
   }
+
+  private sortByDateAsc(a, b) {
+    const dateA = new Date(a.created_at);
+    const dateB = new Date(b.created_at);
+
+    if (dateA > dateB) {
+      return -1;
+    } else if (dateA < dateB) {
+      return 1;
+    } else {
+      return 0;
+    }
+  }
 }

Some files were not shown because too many files changed in this diff