]>
Commit | Line | Data |
---|---|---|
eacf925e | 1 | import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' |
4635f59d C |
2 | import { FormBuilder, FormGroup } from '@angular/forms' |
3 | import { NotificationsService } from 'angular2-notifications' | |
db400f44 | 4 | import { Observable } from 'rxjs' |
3d9eaae3 | 5 | import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model' |
4635f59d C |
6 | import { FormReactive } from '../../../shared' |
7 | import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment' | |
cf117aaa | 8 | import { User } from '../../../shared/users' |
4635f59d C |
9 | import { Video } from '../../../shared/video/video.model' |
10 | import { VideoComment } from './video-comment.model' | |
11 | import { VideoCommentService } from './video-comment.service' | |
12 | ||
13 | @Component({ | |
14 | selector: 'my-video-comment-add', | |
15 | templateUrl: './video-comment-add.component.html', | |
16 | styleUrls: ['./video-comment-add.component.scss'] | |
17 | }) | |
18 | export class VideoCommentAddComponent extends FormReactive implements OnInit { | |
cf117aaa | 19 | @Input() user: User |
4635f59d C |
20 | @Input() video: Video |
21 | @Input() parentComment: VideoComment | |
d7e70384 | 22 | @Input() parentComments: VideoComment[] |
eacf925e | 23 | @Input() focusOnInit = false |
4635f59d C |
24 | |
25 | @Output() commentCreated = new EventEmitter<VideoCommentCreate>() | |
26 | ||
27 | form: FormGroup | |
28 | formErrors = { | |
29 | 'text': '' | |
30 | } | |
31 | validationMessages = { | |
32 | 'text': VIDEO_COMMENT_TEXT.MESSAGES | |
33 | } | |
34 | ||
eacf925e C |
35 | @ViewChild('textarea') private textareaElement: ElementRef |
36 | ||
4635f59d C |
37 | constructor ( |
38 | private formBuilder: FormBuilder, | |
39 | private notificationsService: NotificationsService, | |
40 | private videoCommentService: VideoCommentService | |
41 | ) { | |
42 | super() | |
43 | } | |
44 | ||
45 | buildForm () { | |
46 | this.form = this.formBuilder.group({ | |
47 | text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ] | |
48 | }) | |
49 | ||
50 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
51 | } | |
52 | ||
53 | ngOnInit () { | |
54 | this.buildForm() | |
eacf925e C |
55 | |
56 | if (this.focusOnInit === true) { | |
57 | this.textareaElement.nativeElement.focus() | |
58 | } | |
d7e70384 C |
59 | |
60 | if (this.parentComment) { | |
61 | const mentions = this.parentComments | |
e8cb4409 | 62 | .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves |
9add0051 | 63 | .map(c => '@' + c.by) |
d7e70384 C |
64 | |
65 | const mentionsSet = new Set(mentions) | |
66 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
67 | ||
68 | this.form.patchValue({ text: mentionsText }) | |
69 | } | |
4635f59d C |
70 | } |
71 | ||
1263fc4e C |
72 | onValidKey () { |
73 | this.onValueChanged() | |
74 | if (!this.form.valid) return | |
75 | ||
76 | this.formValidated() | |
77 | } | |
78 | ||
4635f59d C |
79 | formValidated () { |
80 | const commentCreate: VideoCommentCreate = this.form.value | |
81 | let obs: Observable<any> | |
82 | ||
83 | if (this.parentComment) { | |
84 | obs = this.addCommentReply(commentCreate) | |
85 | } else { | |
86 | obs = this.addCommentThread(commentCreate) | |
87 | } | |
88 | ||
89 | obs.subscribe( | |
90 | comment => { | |
91 | this.commentCreated.emit(comment) | |
92 | this.form.reset() | |
93 | }, | |
94 | ||
95 | err => this.notificationsService.error('Error', err.text) | |
96 | ) | |
d50acfab | 97 | } |
4635f59d C |
98 | |
99 | isAddButtonDisplayed () { | |
100 | return this.form.value['text'] | |
101 | } | |
102 | ||
103 | private addCommentReply (commentCreate: VideoCommentCreate) { | |
104 | return this.videoCommentService | |
105 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
106 | } | |
107 | ||
108 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
109 | return this.videoCommentService | |
110 | .addCommentThread(this.video.id, commentCreate) | |
111 | } | |
112 | } |