b8e2acd52f11316f9ca37a68e68df539fcf208ed
[oweals/peertube.git] / client / src / app / videos / +video-watch / comment / video-comment.component.ts
1 import { Component, EventEmitter, Input, Output } 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 { User } from '../../../shared/users'
6 import { Video } from '../../../shared/video/video.model'
7 import { VideoComment } from './video-comment.model'
8 import { VideoCommentService } from './video-comment.service'
9
10 @Component({
11   selector: 'my-video-comment',
12   templateUrl: './video-comment.component.html',
13   styleUrls: ['./video-comment.component.scss']
14 })
15 export class VideoCommentComponent {
16   @Input() video: Video
17   @Input() comment: VideoComment
18   @Input() commentTree: VideoCommentThreadTree
19   @Input() inReplyToCommentId: number
20
21   @Output() wantedToReply = new EventEmitter<VideoComment>()
22   @Output() resetReply = new EventEmitter()
23
24   constructor (private authService: AuthService,
25                private notificationsService: NotificationsService,
26                private videoCommentService: VideoCommentService) {
27   }
28
29   onCommentReplyCreated (comment: VideoComment) {
30     this.videoCommentService.addCommentReply(this.video.id, this.comment.id, comment)
31       .subscribe(
32         createdComment => {
33           if (!this.commentTree) {
34             this.commentTree = {
35               comment: this.comment,
36               children: []
37             }
38           }
39
40           this.commentTree.children.push({
41             comment: createdComment,
42             children: []
43           })
44           this.resetReply.emit()
45         },
46
47         err => this.notificationsService.error('Error', err.message)
48       )
49   }
50
51   onWantToReply () {
52     this.wantedToReply.emit(this.comment)
53   }
54
55   isUserLoggedIn () {
56     return this.authService.isLoggedIn()
57   }
58
59   // Event from child comment
60   onWantedToReply (comment: VideoComment) {
61     this.wantedToReply.emit(comment)
62   }
63
64   onResetReply () {
65     this.resetReply.emit()
66   }
67 }