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'
14 import { AuthService } from '@app/core/auth'
17 selector: 'my-video-comment-add',
18 templateUrl: './video-comment-add.component.html',
19 styleUrls: ['./video-comment-add.component.scss']
21 export class VideoCommentAddComponent extends FormReactive implements OnInit {
24 @Input() parentComment: VideoComment
25 @Input() parentComments: VideoComment[]
26 @Input() focusOnInit = false
28 @Output() commentCreated = new EventEmitter<VideoComment>()
29 @Output() cancel = new EventEmitter()
31 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
32 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
37 protected formValidatorService: FormValidatorService,
38 private videoCommentValidatorsService: VideoCommentValidatorsService,
39 private notifier: Notifier,
40 private videoCommentService: VideoCommentService,
41 private authService: AuthService,
42 private modalService: NgbModal,
43 private router: Router
50 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
54 if (this.focusOnInit === true) {
55 this.textareaElement.nativeElement.focus()
58 if (this.parentComment) {
59 const mentions = this.parentComments
60 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
63 const mentionsSet = new Set(mentions)
64 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
66 this.form.patchValue({ text: mentionsText })
73 if (!this.form.valid) return
78 openVisitorModal (event: any) {
79 if (this.user === null) { // we only open it for visitors
80 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
81 event.srcElement.blur()
82 event.preventDefault()
84 this.modalService.open(this.visitorModal)
89 this.modalService.dismissAll()
93 // If we validate very quickly the comment form, we might comment twice
94 if (this.addingComment) return
96 this.addingComment = true
98 const commentCreate: VideoCommentCreate = this.form.value
99 let obs: Observable<VideoComment>
101 if (this.parentComment) {
102 obs = this.addCommentReply(commentCreate)
104 obs = this.addCommentThread(commentCreate)
109 this.addingComment = false
110 this.commentCreated.emit(comment)
115 this.addingComment = false
117 this.notifier.error(err.text)
122 isAddButtonDisplayed () {
123 return this.form.value['text']
127 return window.location.href
131 if (this.user) return this.user.accountAvatarUrl
132 return window.location.origin + '/client/assets/images/default-avatar.png'
136 this.hideVisitorModal()
137 this.router.navigate([ '/login' ])
140 cancelCommentReply () {
141 this.cancel.emit(null)
144 private addCommentReply (commentCreate: VideoCommentCreate) {
145 return this.videoCommentService
146 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
149 private addCommentThread (commentCreate: VideoCommentCreate) {
150 return this.videoCommentService
151 .addCommentThread(this.video.id, commentCreate)