]>
Commit | Line | Data |
---|---|---|
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 { User } from '../../../shared/users' | |
7 | import { Video } from '../../../shared/video/video.model' | |
8 | import { VideoComment } from './video-comment.model' | |
9 | import { VideoCommentService } from './video-comment.service' | |
10 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
12 | import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.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 videoCommentValidatorsService: VideoCommentValidatorsService, | |
33 | private notificationsService: NotificationsService, | |
34 | private videoCommentService: VideoCommentService, | |
35 | private i18n: I18n | |
36 | ) { | |
37 | super() | |
38 | } | |
39 | ||
40 | ngOnInit () { | |
41 | this.buildForm({ | |
42 | text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT | |
43 | }) | |
44 | ||
45 | if (this.focusOnInit === true) { | |
46 | this.textareaElement.nativeElement.focus() | |
47 | } | |
48 | ||
49 | if (this.parentComment) { | |
50 | const mentions = this.parentComments | |
51 | .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
52 | .map(c => '@' + c.by) | |
53 | ||
54 | const mentionsSet = new Set(mentions) | |
55 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
56 | ||
57 | this.form.patchValue({ text: mentionsText }) | |
58 | } | |
59 | } | |
60 | ||
61 | onValidKey () { | |
62 | this.onValueChanged() | |
63 | if (!this.form.valid) return | |
64 | ||
65 | this.formValidated() | |
66 | } | |
67 | ||
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 | ||
84 | err => this.notificationsService.error(this.i18n('Error'), err.text) | |
85 | ) | |
86 | } | |
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 | } |