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<VideoCommentCreate>()
30 @ViewChild('visitorModal') visitorModal: NgbModal
31 @ViewChild('textarea') textareaElement: ElementRef
36 protected formValidatorService: FormValidatorService,
37 private videoCommentValidatorsService: VideoCommentValidatorsService,
38 private notifier: Notifier,
39 private videoCommentService: VideoCommentService,
40 private authService: AuthService,
41 private modalService: NgbModal,
42 private router: Router
49 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
53 if (this.focusOnInit === true) {
54 this.textareaElement.nativeElement.focus()
57 if (this.parentComment) {
58 const mentions = this.parentComments
59 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
62 const mentionsSet = new Set(mentions)
63 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
65 this.form.patchValue({ text: mentionsText })
72 if (!this.form.valid) return
77 openVisitorModal (event: any) {
78 if (this.user === null) { // we only open it for visitors
79 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
80 event.srcElement.blur()
81 event.preventDefault()
83 this.modalService.open(this.visitorModal)
88 this.modalService.dismissAll()
92 // If we validate very quickly the comment form, we might comment twice
93 if (this.addingComment) return
95 this.addingComment = true
97 const commentCreate: VideoCommentCreate = this.form.value
98 let obs: Observable<any>
100 if (this.parentComment) {
101 obs = this.addCommentReply(commentCreate)
103 obs = this.addCommentThread(commentCreate)
108 this.addingComment = false
109 this.commentCreated.emit(comment)
114 this.addingComment = false
116 this.notifier.error(err.text)
121 isAddButtonDisplayed () {
122 return this.form.value['text']
126 return window.location.href
130 if (this.user) return this.user.accountAvatarUrl
131 return window.location.origin + '/client/assets/images/default-avatar.png'
135 this.hideVisitorModal()
136 this.router.navigate([ '/login' ])
139 private addCommentReply (commentCreate: VideoCommentCreate) {
140 return this.videoCommentService
141 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
144 private addCommentThread (commentCreate: VideoCommentCreate) {
145 return this.videoCommentService
146 .addCommentThread(this.video.id, commentCreate)