1 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2 import { Router } from '@angular/router'
3 import { Notifier } from '@app/core'
4 import { Observable } from 'rxjs'
5 import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
6 import { FormReactive } from '../../../shared'
7 import { User } from '../../../shared/users'
8 import { Video } from '../../../shared/video/video.model'
9 import { VideoComment } from './video-comment.model'
10 import { VideoCommentService } from './video-comment.service'
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'
13 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
16 selector: 'my-video-comment-add',
17 templateUrl: './video-comment-add.component.html',
18 styleUrls: ['./video-comment-add.component.scss']
20 export class VideoCommentAddComponent extends FormReactive implements OnInit {
23 @Input() parentComment: VideoComment
24 @Input() parentComments: VideoComment[]
25 @Input() focusOnInit = false
27 @Output() commentCreated = new EventEmitter<VideoComment>()
28 @Output() cancel = new EventEmitter()
30 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
31 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
36 protected formValidatorService: FormValidatorService,
37 private videoCommentValidatorsService: VideoCommentValidatorsService,
38 private notifier: Notifier,
39 private videoCommentService: VideoCommentService,
40 private modalService: NgbModal,
41 private router: Router
48 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
52 if (this.focusOnInit === true) {
53 this.textareaElement.nativeElement.focus()
56 if (this.parentComment) {
57 const mentions = this.parentComments
58 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
61 const mentionsSet = new Set(mentions)
62 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
64 this.form.patchValue({ text: mentionsText })
71 if (!this.form.valid) return
76 openVisitorModal (event: any) {
77 if (this.user === null) { // we only open it for visitors
78 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
79 event.srcElement.blur()
80 event.preventDefault()
82 this.modalService.open(this.visitorModal)
87 this.modalService.dismissAll()
91 // If we validate very quickly the comment form, we might comment twice
92 if (this.addingComment) return
94 this.addingComment = true
96 const commentCreate: VideoCommentCreate = this.form.value
97 let obs: Observable<VideoComment>
99 if (this.parentComment) {
100 obs = this.addCommentReply(commentCreate)
102 obs = this.addCommentThread(commentCreate)
107 this.addingComment = false
108 this.commentCreated.emit(comment)
113 this.addingComment = false
115 this.notifier.error(err.text)
120 isAddButtonDisplayed () {
121 return this.form.value['text']
125 return window.location.href
129 if (this.user) return this.user.accountAvatarUrl
130 return window.location.origin + '/client/assets/images/default-avatar.png'
134 this.hideVisitorModal()
135 this.router.navigate([ '/login' ])
138 cancelCommentReply () {
139 this.cancel.emit(null)
140 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
143 private addCommentReply (commentCreate: VideoCommentCreate) {
144 return this.videoCommentService
145 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
148 private addCommentThread (commentCreate: VideoCommentCreate) {
149 return this.videoCommentService
150 .addCommentThread(this.video.id, commentCreate)