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 { I18n } from '@ngx-translate/i18n-polyfill'
12 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service'
14 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
15 import { AuthService } from '@app/core/auth'
18 selector: 'my-video-comment-add',
19 templateUrl: './video-comment-add.component.html',
20 styleUrls: ['./video-comment-add.component.scss']
22 export class VideoCommentAddComponent extends FormReactive implements OnInit {
25 @Input() parentComment: VideoComment
26 @Input() parentComments: VideoComment[]
27 @Input() focusOnInit = false
29 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
31 @ViewChild('visitorModal') visitorModal: NgbModal
32 @ViewChild('textarea') 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,
51 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
55 if (this.focusOnInit === true) {
56 this.textareaElement.nativeElement.focus()
59 if (this.parentComment) {
60 const mentions = this.parentComments
61 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
64 const mentionsSet = new Set(mentions)
65 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
67 this.form.patchValue({ text: mentionsText })
74 if (!this.form.valid) return
79 openVisitorModal (event: any) {
80 if (this.user === null) { // we only open it for visitors
81 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
82 event.srcElement.blur()
83 event.preventDefault()
85 this.modalService.open(this.visitorModal)
90 this.modalService.dismissAll()
94 // If we validate very quickly the comment form, we might comment twice
95 if (this.addingComment) return
97 this.addingComment = true
99 const commentCreate: VideoCommentCreate = this.form.value
100 let obs: Observable<any>
102 if (this.parentComment) {
103 obs = this.addCommentReply(commentCreate)
105 obs = this.addCommentThread(commentCreate)
110 this.addingComment = false
111 this.commentCreated.emit(comment)
116 this.addingComment = false
118 this.notifier.error(err.text)
123 isAddButtonDisplayed () {
124 return this.form.value['text']
128 return window.location.href
132 if (this.user) return this.user.accountAvatarUrl
133 return window.location.origin + '/client/assets/images/default-avatar.png'
137 this.hideVisitorModal()
138 this.router.navigate([ '/login' ])
141 private addCommentReply (commentCreate: VideoCommentCreate) {
142 return this.videoCommentService
143 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
146 private addCommentThread (commentCreate: VideoCommentCreate) {
147 return this.videoCommentService
148 .addCommentThread(this.video.id, commentCreate)