1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { NotificationsService } from 'angular2-notifications'
3 import { Observable } from 'rxjs'
4 import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
5 import { FormReactive } from '../../../shared'
6 import { User } from '../../../shared/users'
7 import { Video } from '../../../shared/video/video.model'
8 import { VideoComment } from './video-comment.model'
9 import { VideoCommentService } from './video-comment.service'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
12 import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service'
15 selector: 'my-video-comment-add',
16 templateUrl: './video-comment-add.component.html',
17 styleUrls: ['./video-comment-add.component.scss']
19 export class VideoCommentAddComponent extends FormReactive implements OnInit {
22 @Input() parentComment: VideoComment
23 @Input() parentComments: VideoComment[]
24 @Input() focusOnInit = false
26 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
28 @ViewChild('textarea') private textareaElement: ElementRef
30 private addingComment = false
33 protected formValidatorService: FormValidatorService,
34 private videoCommentValidatorsService: VideoCommentValidatorsService,
35 private notificationsService: NotificationsService,
36 private videoCommentService: VideoCommentService,
44 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
47 if (this.focusOnInit === true) {
48 this.textareaElement.nativeElement.focus()
51 if (this.parentComment) {
52 const mentions = this.parentComments
53 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
56 const mentionsSet = new Set(mentions)
57 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
59 this.form.patchValue({ text: mentionsText })
65 if (!this.form.valid) return
71 // If we validate very quickly the comment form, we might comment twice
72 if (this.addingComment) return
74 this.addingComment = true
76 const commentCreate: VideoCommentCreate = this.form.value
77 let obs: Observable<any>
79 if (this.parentComment) {
80 obs = this.addCommentReply(commentCreate)
82 obs = this.addCommentThread(commentCreate)
87 this.addingComment = false
88 this.commentCreated.emit(comment)
93 this.addingComment = false
95 this.notificationsService.error(this.i18n('Error'), err.text)
100 isAddButtonDisplayed () {
101 return this.form.value['text']
104 private addCommentReply (commentCreate: VideoCommentCreate) {
105 return this.videoCommentService
106 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
109 private addCommentThread (commentCreate: VideoCommentCreate) {
110 return this.videoCommentService
111 .addCommentThread(this.video.id, commentCreate)