]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
Fix removing scheduled update
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / comment / video-comment-add.component.ts
CommitLineData
eacf925e 1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
4635f59d 2import { NotificationsService } from 'angular2-notifications'
db400f44 3import { Observable } from 'rxjs'
3d9eaae3 4import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
4635f59d 5import { FormReactive } from '../../../shared'
cf117aaa 6import { User } from '../../../shared/users'
4635f59d
C
7import { Video } from '../../../shared/video/video.model'
8import { VideoComment } from './video-comment.model'
9import { VideoCommentService } from './video-comment.service'
b1d40cff 10import { I18n } from '@ngx-translate/i18n-polyfill'
d18d6478 11import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
e309822b 12import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service'
4635f59d
C
13
14@Component({
15 selector: 'my-video-comment-add',
16 templateUrl: './video-comment-add.component.html',
17 styleUrls: ['./video-comment-add.component.scss']
18})
19export class VideoCommentAddComponent extends FormReactive implements OnInit {
cf117aaa 20 @Input() user: User
4635f59d
C
21 @Input() video: Video
22 @Input() parentComment: VideoComment
d7e70384 23 @Input() parentComments: VideoComment[]
eacf925e 24 @Input() focusOnInit = false
4635f59d
C
25
26 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
27
eacf925e
C
28 @ViewChild('textarea') private textareaElement: ElementRef
29
4635f59d 30 constructor (
d18d6478 31 protected formValidatorService: FormValidatorService,
e309822b 32 private videoCommentValidatorsService: VideoCommentValidatorsService,
4635f59d 33 private notificationsService: NotificationsService,
b1d40cff
C
34 private videoCommentService: VideoCommentService,
35 private i18n: I18n
4635f59d
C
36 ) {
37 super()
38 }
39
4635f59d 40 ngOnInit () {
d18d6478 41 this.buildForm({
e309822b 42 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
d18d6478 43 })
eacf925e
C
44
45 if (this.focusOnInit === true) {
46 this.textareaElement.nativeElement.focus()
47 }
d7e70384
C
48
49 if (this.parentComment) {
50 const mentions = this.parentComments
e8cb4409 51 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
9add0051 52 .map(c => '@' + c.by)
d7e70384
C
53
54 const mentionsSet = new Set(mentions)
55 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
56
57 this.form.patchValue({ text: mentionsText })
58 }
4635f59d
C
59 }
60
1263fc4e
C
61 onValidKey () {
62 this.onValueChanged()
63 if (!this.form.valid) return
64
65 this.formValidated()
66 }
67
4635f59d
C
68 formValidated () {
69 const commentCreate: VideoCommentCreate = this.form.value
70 let obs: Observable<any>
71
72 if (this.parentComment) {
73 obs = this.addCommentReply(commentCreate)
74 } else {
75 obs = this.addCommentThread(commentCreate)
76 }
77
78 obs.subscribe(
79 comment => {
80 this.commentCreated.emit(comment)
81 this.form.reset()
82 },
83
b1d40cff 84 err => this.notificationsService.error(this.i18n('Error'), err.text)
4635f59d 85 )
d50acfab 86 }
4635f59d
C
87
88 isAddButtonDisplayed () {
89 return this.form.value['text']
90 }
91
92 private addCommentReply (commentCreate: VideoCommentCreate) {
93 return this.videoCommentService
94 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
95 }
96
97 private addCommentThread (commentCreate: VideoCommentCreate) {
98 return this.videoCommentService
99 .addCommentThread(this.video.id, commentCreate)
100 }
101}