]>
Commit | Line | Data |
---|---|---|
eacf925e | 1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' |
4635f59d | 2 | import { NotificationsService } from 'angular2-notifications' |
db400f44 | 3 | import { Observable } from 'rxjs' |
3d9eaae3 | 4 | import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model' |
4635f59d | 5 | import { FormReactive } from '../../../shared' |
cf117aaa | 6 | import { User } from '../../../shared/users' |
4635f59d C |
7 | import { Video } from '../../../shared/video/video.model' |
8 | import { VideoComment } from './video-comment.model' | |
9 | import { VideoCommentService } from './video-comment.service' | |
b1d40cff | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
d18d6478 | 11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' |
e309822b | 12 | import { 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 | }) | |
19 | export 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 | ||
ff336427 C |
30 | private addingComment = false |
31 | ||
4635f59d | 32 | constructor ( |
d18d6478 | 33 | protected formValidatorService: FormValidatorService, |
e309822b | 34 | private videoCommentValidatorsService: VideoCommentValidatorsService, |
4635f59d | 35 | private notificationsService: NotificationsService, |
b1d40cff C |
36 | private videoCommentService: VideoCommentService, |
37 | private i18n: I18n | |
4635f59d C |
38 | ) { |
39 | super() | |
40 | } | |
41 | ||
4635f59d | 42 | ngOnInit () { |
d18d6478 | 43 | this.buildForm({ |
e309822b | 44 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT |
d18d6478 | 45 | }) |
eacf925e C |
46 | |
47 | if (this.focusOnInit === true) { | |
48 | this.textareaElement.nativeElement.focus() | |
49 | } | |
d7e70384 C |
50 | |
51 | if (this.parentComment) { | |
52 | const mentions = this.parentComments | |
e8cb4409 | 53 | .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves |
9add0051 | 54 | .map(c => '@' + c.by) |
d7e70384 C |
55 | |
56 | const mentionsSet = new Set(mentions) | |
57 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
58 | ||
59 | this.form.patchValue({ text: mentionsText }) | |
60 | } | |
4635f59d C |
61 | } |
62 | ||
1263fc4e C |
63 | onValidKey () { |
64 | this.onValueChanged() | |
65 | if (!this.form.valid) return | |
66 | ||
67 | this.formValidated() | |
68 | } | |
69 | ||
4635f59d | 70 | formValidated () { |
ff336427 C |
71 | // If we validate very quickly the comment form, we might comment twice |
72 | if (this.addingComment) return | |
73 | ||
74 | this.addingComment = true | |
75 | ||
4635f59d C |
76 | const commentCreate: VideoCommentCreate = this.form.value |
77 | let obs: Observable<any> | |
78 | ||
79 | if (this.parentComment) { | |
80 | obs = this.addCommentReply(commentCreate) | |
81 | } else { | |
82 | obs = this.addCommentThread(commentCreate) | |
83 | } | |
84 | ||
85 | obs.subscribe( | |
86 | comment => { | |
ff336427 | 87 | this.addingComment = false |
4635f59d C |
88 | this.commentCreated.emit(comment) |
89 | this.form.reset() | |
90 | }, | |
91 | ||
ff336427 C |
92 | err => { |
93 | this.addingComment = false | |
94 | ||
95 | this.notificationsService.error(this.i18n('Error'), err.text) | |
96 | } | |
4635f59d | 97 | ) |
d50acfab | 98 | } |
4635f59d C |
99 | |
100 | isAddButtonDisplayed () { | |
101 | return this.form.value['text'] | |
102 | } | |
103 | ||
104 | private addCommentReply (commentCreate: VideoCommentCreate) { | |
105 | return this.videoCommentService | |
106 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
107 | } | |
108 | ||
109 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
110 | return this.videoCommentService | |
111 | .addCommentThread(this.video.id, commentCreate) | |
112 | } | |
113 | } |