]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/comment/video-comment-add.component.ts
Add i18n attributes
[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 { FormBuilder, FormGroup } from '@angular/forms'
3 import { NotificationsService } from 'angular2-notifications'
4 import { Observable } from 'rxjs'
5 import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
6 import { FormReactive } from '../../../shared'
7 import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment'
8 import { User } from '../../../shared/users'
9 import { Video } from '../../../shared/video/video.model'
10 import { VideoComment } from './video-comment.model'
11 import { VideoCommentService } from './video-comment.service'
12 import { I18n } from '@ngx-translate/i18n-polyfill'
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 form: FormGroup
29 formErrors = {
30 'text': ''
31 }
32 validationMessages = {
33 'text': VIDEO_COMMENT_TEXT.MESSAGES
34 }
35
36 @ViewChild('textarea') private textareaElement: ElementRef
37
38 constructor (
39 private formBuilder: FormBuilder,
40 private notificationsService: NotificationsService,
41 private videoCommentService: VideoCommentService,
42 private i18n: I18n
43 ) {
44 super()
45 }
46
47 buildForm () {
48 this.form = this.formBuilder.group({
49 text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ]
50 })
51
52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
53 }
54
55 ngOnInit () {
56 this.buildForm()
57
58 if (this.focusOnInit === true) {
59 this.textareaElement.nativeElement.focus()
60 }
61
62 if (this.parentComment) {
63 const mentions = this.parentComments
64 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
65 .map(c => '@' + c.by)
66
67 const mentionsSet = new Set(mentions)
68 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
69
70 this.form.patchValue({ text: mentionsText })
71 }
72 }
73
74 onValidKey () {
75 this.onValueChanged()
76 if (!this.form.valid) return
77
78 this.formValidated()
79 }
80
81 formValidated () {
82 const commentCreate: VideoCommentCreate = this.form.value
83 let obs: Observable<any>
84
85 if (this.parentComment) {
86 obs = this.addCommentReply(commentCreate)
87 } else {
88 obs = this.addCommentThread(commentCreate)
89 }
90
91 obs.subscribe(
92 comment => {
93 this.commentCreated.emit(comment)
94 this.form.reset()
95 },
96
97 err => this.notificationsService.error(this.i18n('Error'), err.text)
98 )
99 }
100
101 isAddButtonDisplayed () {
102 return this.form.value['text']
103 }
104
105 private addCommentReply (commentCreate: VideoCommentCreate) {
106 return this.videoCommentService
107 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
108 }
109
110 private addCommentThread (commentCreate: VideoCommentCreate) {
111 return this.videoCommentService
112 .addCommentThread(this.video.id, commentCreate)
113 }
114 }