1 import { Observable } from 'rxjs'
2 import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { Notifier, User } from '@app/core'
5 import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
6 import { Video } from '@app/shared/shared-main'
7 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
8 import { VideoCommentCreate } from '@shared/models'
9 import { VideoComment } from './video-comment.model'
10 import { VideoCommentService } from './video-comment.service'
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
17 export class VideoCommentAddComponent extends FormReactive implements OnInit {
20 @Input() parentComment: VideoComment
21 @Input() parentComments: VideoComment[]
22 @Input() focusOnInit = false
24 @Output() commentCreated = new EventEmitter<VideoComment>()
25 @Output() cancel = new EventEmitter()
27 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
28 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
33 protected formValidatorService: FormValidatorService,
34 private videoCommentValidatorsService: VideoCommentValidatorsService,
35 private notifier: Notifier,
36 private videoCommentService: VideoCommentService,
37 private modalService: NgbModal,
38 private router: Router
45 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
49 if (this.focusOnInit === true) {
50 this.textareaElement.nativeElement.focus()
53 if (this.parentComment) {
54 const mentions = this.parentComments
55 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
58 const mentionsSet = new Set(mentions)
59 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
61 this.form.patchValue({ text: mentionsText })
68 if (!this.form.valid) return
73 openVisitorModal (event: any) {
74 if (this.user === null) { // we only open it for visitors
75 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
76 event.srcElement.blur()
77 event.preventDefault()
79 this.modalService.open(this.visitorModal)
84 this.modalService.dismissAll()
88 // If we validate very quickly the comment form, we might comment twice
89 if (this.addingComment) return
91 this.addingComment = true
93 const commentCreate: VideoCommentCreate = this.form.value
94 let obs: Observable<VideoComment>
96 if (this.parentComment) {
97 obs = this.addCommentReply(commentCreate)
99 obs = this.addCommentThread(commentCreate)
104 this.addingComment = false
105 this.commentCreated.emit(comment)
110 this.addingComment = false
112 this.notifier.error(err.text)
117 isAddButtonDisplayed () {
118 return this.form.value['text']
122 return window.location.href
126 if (this.user) return this.user.accountAvatarUrl
127 return window.location.origin + '/client/assets/images/default-avatar.png'
131 this.hideVisitorModal()
132 this.router.navigate([ '/login' ])
135 cancelCommentReply () {
136 this.cancel.emit(null)
137 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
140 private addCommentReply (commentCreate: VideoCommentCreate) {
141 return this.videoCommentService
142 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
145 private addCommentThread (commentCreate: VideoCommentCreate) {
146 return this.videoCommentService
147 .addCommentThread(this.video.id, commentCreate)