Add infinite scroll to comments
authorChocobozzz <me@florianbigard.com>
Fri, 29 Dec 2017 08:29:32 +0000 (09:29 +0100)
committerChocobozzz <me@florianbigard.com>
Fri, 29 Dec 2017 08:29:32 +0000 (09:29 +0100)
client/src/app/videos/+video-watch/comment/video-comments.component.html
client/src/app/videos/+video-watch/comment/video-comments.component.ts

index 9d7581269d185316119100d057b8380b37f9df29..5c6908150777069ae7c31f88e7fc354b0b23362f 100644 (file)
@@ -9,7 +9,13 @@
     (commentCreated)="onCommentThreadCreated($event)"
   ></my-video-comment-add>
 
-  <div class="comment-threads">
+  <div
+    class="comment-threads"
+    infiniteScroll
+    [infiniteScrollUpDistance]="1.5"
+    [infiniteScrollDistance]="0.5"
+    (scrolled)="onNearOfBottom()"
+  >
     <div *ngFor="let comment of comments">
       <my-video-comment
         [comment]="comment"
index 32e0f2fbd80cbdda28656983b16b23e799eb9fc0..f4dda90895e062f2a540cbe80e2e15a280d520cb 100644 (file)
@@ -22,7 +22,7 @@ export class VideoCommentsComponent implements OnInit {
   sort: SortField = '-createdAt'
   componentPagination: ComponentPagination = {
     currentPage: 1,
-    itemsPerPage: 25,
+    itemsPerPage: 10,
     totalItems: null
   }
   inReplyToCommentId: number
@@ -36,15 +36,7 @@ export class VideoCommentsComponent implements OnInit {
   ) {}
 
   ngOnInit () {
-    this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
-      .subscribe(
-        res => {
-          this.comments = res.comments
-          this.componentPagination.totalItems = res.totalComments
-        },
-
-        err => this.notificationsService.error('Error', err.message)
-      )
+    this.loadMoreComments()
   }
 
   viewReplies (comment: VideoComment) {
@@ -61,6 +53,18 @@ export class VideoCommentsComponent implements OnInit {
       )
   }
 
+  loadMoreComments () {
+    this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
+      .subscribe(
+        res => {
+          this.comments = this.comments.concat(res.comments)
+          this.componentPagination.totalItems = res.totalComments
+        },
+
+        err => this.notificationsService.error('Error', err.message)
+      )
+  }
+
   onCommentThreadCreated (comment: VideoComment) {
     this.comments.unshift(comment)
   }
@@ -76,4 +80,23 @@ export class VideoCommentsComponent implements OnInit {
   isUserLoggedIn () {
     return this.authService.isLoggedIn()
   }
+
+  onNearOfBottom () {
+    this.componentPagination.currentPage++
+
+    if (this.hasMoreComments()) {
+      this.loadMoreComments()
+    }
+  }
+
+  protected hasMoreComments () {
+    // No results
+    if (this.componentPagination.totalItems === 0) return false
+
+    // Not loaded yet
+    if (!this.componentPagination.totalItems) return true
+
+    const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
+    return maxPage > this.componentPagination.currentPage
+  }
 }