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 { VideoComment } from './video-comment.model'
10 import { VideoCommentService } from './video-comment.service'
13 selector: 'my-video-comments',
14 templateUrl: './video-comments.component.html',
15 styleUrls: ['./video-comments.component.scss']
17 export class VideoCommentsComponent implements OnInit {
18 @Input() video: VideoDetails
21 comments: VideoComment[] = []
22 sort: SortField = '-createdAt'
23 componentPagination: ComponentPagination = {
28 inReplyToCommentId: number
29 threadComments: { [ id: number ]: VideoCommentThreadTree } = {}
30 threadLoading: { [ id: number ]: boolean } = {}
33 private authService: AuthService,
34 private notificationsService: NotificationsService,
35 private videoCommentService: VideoCommentService
39 if (this.video.commentsEnabled === true) {
40 this.loadMoreComments()
44 viewReplies (comment: VideoComment) {
45 this.threadLoading[comment.id] = true
47 this.videoCommentService.getVideoThreadComments(this.video.id, comment.id)
50 this.threadComments[comment.id] = res
51 this.threadLoading[comment.id] = false
54 err => this.notificationsService.error('Error', err.message)
59 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
62 this.comments = this.comments.concat(res.comments)
63 this.componentPagination.totalItems = res.totalComments
66 err => this.notificationsService.error('Error', err.message)
70 onCommentThreadCreated (comment: VideoComment) {
71 this.comments.unshift(comment)
74 onWantedToReply (comment: VideoComment) {
75 this.inReplyToCommentId = comment.id
79 this.inReplyToCommentId = undefined
83 return this.authService.isLoggedIn()
87 this.componentPagination.currentPage++
89 if (this.hasMoreComments()) {
90 this.loadMoreComments()
94 protected hasMoreComments () {
96 if (this.componentPagination.totalItems === 0) return false
99 if (!this.componentPagination.totalItems) return true
101 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
102 return maxPage > this.componentPagination.currentPage