1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { FormBuilder, FormGroup } from '@angular/forms'
3 import { NotificationsService } from 'angular2-notifications'
4 import { Observable } from 'rxjs'
5 import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
6 import { FormReactive } from '../../../shared'
7 import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment'
8 import { User } from '../../../shared/users'
9 import { Video } from '../../../shared/video/video.model'
10 import { VideoComment } from './video-comment.model'
11 import { VideoCommentService } from './video-comment.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
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>()
32 validationMessages = {
33 'text': VIDEO_COMMENT_TEXT.MESSAGES
36 @ViewChild('textarea') private textareaElement: ElementRef
39 private formBuilder: FormBuilder,
40 private notificationsService: NotificationsService,
41 private videoCommentService: VideoCommentService,
48 this.form = this.formBuilder.group({
49 text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ]
52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
58 if (this.focusOnInit === true) {
59 this.textareaElement.nativeElement.focus()
62 if (this.parentComment) {
63 const mentions = this.parentComments
64 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
67 const mentionsSet = new Set(mentions)
68 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
70 this.form.patchValue({ text: mentionsText })
76 if (!this.form.valid) return
82 const commentCreate: VideoCommentCreate = this.form.value
83 let obs: Observable<any>
85 if (this.parentComment) {
86 obs = this.addCommentReply(commentCreate)
88 obs = this.addCommentThread(commentCreate)
93 this.commentCreated.emit(comment)
97 err => this.notificationsService.error(this.i18n('Error'), err.text)
101 isAddButtonDisplayed () {
102 return this.form.value['text']
105 private addCommentReply (commentCreate: VideoCommentCreate) {
106 return this.videoCommentService
107 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
110 private addCommentThread (commentCreate: VideoCommentCreate) {
111 return this.videoCommentService
112 .addCommentThread(this.video.id, commentCreate)