1 import { Component, Input, OnInit } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model'
4 import { AuthService } from '../../../core/auth'
5 import { ComponentPagination } from '../../../shared/rest/component-pagination.model'
6 import { User } from '../../../shared/users'
7 import { SortField } from '../../../shared/video/sort-field.type'
8 import { VideoDetails } from '../../../shared/video/video-details.model'
9 import { Video } from '../../../shared/video/video.model'
10 import { VideoComment } from './video-comment.model'
11 import { VideoCommentService } from './video-comment.service'
14 selector: 'my-video-comments',
15 templateUrl: './video-comments.component.html',
16 styleUrls: ['./video-comments.component.scss']
18 export class VideoCommentsComponent implements OnInit {
19 @Input() video: VideoDetails
22 comments: VideoComment[] = []
23 sort: SortField = '-createdAt'
24 componentPagination: ComponentPagination = {
29 inReplyToCommentId: number
30 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
31 threadLoading: { [ id: number ]: boolean } = {}
34 private authService: AuthService,
35 private notificationsService: NotificationsService,
36 private videoCommentService: VideoCommentService
40 if (this.video.commentsEnabled === true) {
41 this.loadMoreComments()
45 viewReplies (comment: VideoComment) {
46 this.threadLoading[comment.id] = true
48 this.videoCommentService.getVideoThreadComments(this.video.id, comment.id)
51 this.threadComments[comment.id] = res
52 this.threadLoading[comment.id] = false
55 err => this.notificationsService.error('Error', err.message)
60 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
63 this.comments = this.comments.concat(res.comments)
64 this.componentPagination.totalItems = res.totalComments
67 err => this.notificationsService.error('Error', err.message)
71 onCommentThreadCreated (comment: VideoComment) {
72 this.comments.unshift(comment)
75 onWantedToReply (comment: VideoComment) {
76 this.inReplyToCommentId = comment.id
80 this.inReplyToCommentId = undefined
84 return this.authService.isLoggedIn()
88 this.componentPagination.currentPage++
90 if (this.hasMoreComments()) {
91 this.loadMoreComments()
95 protected hasMoreComments () {
97 if (this.componentPagination.totalItems === 0) return false
100 if (!this.componentPagination.totalItems) return true
102 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
103 return maxPage > this.componentPagination.currentPage