From 7416fbf3359fc0b890cba3dae9661fe450604b21 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 29 Dec 2017 09:29:32 +0100 Subject: Add infinite scroll to comments --- .../comment/video-comments.component.html | 8 +++- .../comment/video-comments.component.ts | 43 +++++++++++++++++----- 2 files changed, 40 insertions(+), 11 deletions(-) (limited to 'client/src') diff --git a/client/src/app/videos/+video-watch/comment/video-comments.component.html b/client/src/app/videos/+video-watch/comment/video-comments.component.html index 9d7581269..5c6908150 100644 --- a/client/src/app/videos/+video-watch/comment/video-comments.component.html +++ b/client/src/app/videos/+video-watch/comment/video-comments.component.html @@ -9,7 +9,13 @@ (commentCreated)="onCommentThreadCreated($event)" > -
+
{ - 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 + } } -- cgit v1.2.3