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