]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
eacf925e 1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
4635f59d
C
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { NotificationsService } from 'angular2-notifications'
db400f44 4import { Observable } from 'rxjs'
3d9eaae3 5import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
4635f59d
C
6import { FormReactive } from '../../../shared'
7import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment'
cf117aaa 8import { User } from '../../../shared/users'
4635f59d
C
9import { Video } from '../../../shared/video/video.model'
10import { VideoComment } from './video-comment.model'
11import { VideoCommentService } from './video-comment.service'
b1d40cff 12import { I18n } from '@ngx-translate/i18n-polyfill'
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
28 form: FormGroup
29 formErrors = {
30 'text': ''
31 }
32 validationMessages = {
33 'text': VIDEO_COMMENT_TEXT.MESSAGES
34 }
35
eacf925e
C
36 @ViewChild('textarea') private textareaElement: ElementRef
37
4635f59d
C
38 constructor (
39 private formBuilder: FormBuilder,
40 private notificationsService: NotificationsService,
b1d40cff
C
41 private videoCommentService: VideoCommentService,
42 private i18n: I18n
4635f59d
C
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()
eacf925e
C
57
58 if (this.focusOnInit === true) {
59 this.textareaElement.nativeElement.focus()
60 }
d7e70384
C
61
62 if (this.parentComment) {
63 const mentions = this.parentComments
e8cb4409 64 .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves
9add0051 65 .map(c => '@' + c.by)
d7e70384
C
66
67 const mentionsSet = new Set(mentions)
68 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
69
70 this.form.patchValue({ text: mentionsText })
71 }
4635f59d
C
72 }
73
1263fc4e
C
74 onValidKey () {
75 this.onValueChanged()
76 if (!this.form.valid) return
77
78 this.formValidated()
79 }
80
4635f59d
C
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
b1d40cff 97 err => this.notificationsService.error(this.i18n('Error'), err.text)
4635f59d 98 )
d50acfab 99 }
4635f59d
C
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}