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 { Video } from '../../../shared/video/video.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 {
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 this.loadMoreComments()
42 viewReplies (comment: VideoComment) {
43 this.threadLoading[comment.id] = true
45 this.videoCommentService.getVideoThreadComments(this.video.id, comment.id)
48 this.threadComments[comment.id] = res
49 this.threadLoading[comment.id] = false
52 err => this.notificationsService.error('Error', err.message)
57 this.videoCommentService.getVideoCommentThreads(this.video.id, this.componentPagination, this.sort)
60 this.comments = this.comments.concat(res.comments)
61 this.componentPagination.totalItems = res.totalComments
64 err => this.notificationsService.error('Error', err.message)
68 onCommentThreadCreated (comment: VideoComment) {
69 this.comments.unshift(comment)
72 onWantedToReply (comment: VideoComment) {
73 this.inReplyToCommentId = comment.id
77 this.inReplyToCommentId = undefined
81 return this.authService.isLoggedIn()
85 this.componentPagination.currentPage++
87 if (this.hasMoreComments()) {
88 this.loadMoreComments()
92 protected hasMoreComments () {
94 if (this.componentPagination.totalItems === 0) return false
97 if (!this.componentPagination.totalItems) return true
99 const maxPage = this.componentPagination.totalItems / this.componentPagination.itemsPerPage
100 return maxPage > this.componentPagination.currentPage