]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
Send account activitypub update events
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment-add.component.ts
CommitLineData
eacf925e 1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
4635f59d
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { NotificationsService } from 'angular2-notifications'
4import { Observable } from 'rxjs/Observable'
5import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
6import { FormReactive } from '../../../shared'
7import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment'
8import { Video } from '../../../shared/video/video.model'
9import { VideoComment } from './video-comment.model'
10import { VideoCommentService } from './video-comment.service'
11
12@Component({
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
16})
17export class VideoCommentAddComponent extends FormReactive implements OnInit {
18 @Input() video: Video
19 @Input() parentComment: VideoComment
eacf925e 20 @Input() focusOnInit = false
4635f59d
C
21
22 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
23
24 form: FormGroup
25 formErrors = {
26 'text': ''
27 }
28 validationMessages = {
29 'text': VIDEO_COMMENT_TEXT.MESSAGES
30 }
31
eacf925e
C
32 @ViewChild('textarea') private textareaElement: ElementRef
33
4635f59d
C
34 constructor (
35 private formBuilder: FormBuilder,
36 private notificationsService: NotificationsService,
37 private videoCommentService: VideoCommentService
38 ) {
39 super()
40 }
41
42 buildForm () {
43 this.form = this.formBuilder.group({
44 text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ]
45 })
46
47 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
48 }
49
50 ngOnInit () {
51 this.buildForm()
eacf925e
C
52
53 if (this.focusOnInit === true) {
54 this.textareaElement.nativeElement.focus()
55 }
4635f59d
C
56 }
57
58 formValidated () {
59 const commentCreate: VideoCommentCreate = this.form.value
60 let obs: Observable<any>
61
62 if (this.parentComment) {
63 obs = this.addCommentReply(commentCreate)
64 } else {
65 obs = this.addCommentThread(commentCreate)
66 }
67
68 obs.subscribe(
69 comment => {
70 this.commentCreated.emit(comment)
71 this.form.reset()
72 },
73
74 err => this.notificationsService.error('Error', err.text)
75 )
d50acfab 76 }
4635f59d
C
77
78 isAddButtonDisplayed () {
79 return this.form.value['text']
80 }
81
82 private addCommentReply (commentCreate: VideoCommentCreate) {
83 return this.videoCommentService
84 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
85 }
86
87 private addCommentThread (commentCreate: VideoCommentCreate) {
88 return this.videoCommentService
89 .addCommentThread(this.video.id, commentCreate)
90 }
91}