]>
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' | |
4 | import { Observable } from 'rxjs/Observable' | |
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' | |
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 | |
eacf925e | 22 | @Input() focusOnInit = false |
4635f59d C |
23 | |
24 | @Output() commentCreated = new EventEmitter<VideoCommentCreate>() | |
25 | ||
26 | form: FormGroup | |
27 | formErrors = { | |
28 | 'text': '' | |
29 | } | |
30 | validationMessages = { | |
31 | 'text': VIDEO_COMMENT_TEXT.MESSAGES | |
32 | } | |
33 | ||
eacf925e C |
34 | @ViewChild('textarea') private textareaElement: ElementRef |
35 | ||
4635f59d C |
36 | constructor ( |
37 | private formBuilder: FormBuilder, | |
38 | private notificationsService: NotificationsService, | |
39 | private videoCommentService: VideoCommentService | |
40 | ) { | |
41 | super() | |
42 | } | |
43 | ||
44 | buildForm () { | |
45 | this.form = this.formBuilder.group({ | |
46 | text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ] | |
47 | }) | |
48 | ||
49 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
50 | } | |
51 | ||
52 | ngOnInit () { | |
53 | this.buildForm() | |
eacf925e C |
54 | |
55 | if (this.focusOnInit === true) { | |
56 | this.textareaElement.nativeElement.focus() | |
57 | } | |
4635f59d C |
58 | } |
59 | ||
60 | formValidated () { | |
61 | const commentCreate: VideoCommentCreate = this.form.value | |
62 | let obs: Observable<any> | |
63 | ||
64 | if (this.parentComment) { | |
65 | obs = this.addCommentReply(commentCreate) | |
66 | } else { | |
67 | obs = this.addCommentThread(commentCreate) | |
68 | } | |
69 | ||
70 | obs.subscribe( | |
71 | comment => { | |
72 | this.commentCreated.emit(comment) | |
73 | this.form.reset() | |
74 | }, | |
75 | ||
76 | err => this.notificationsService.error('Error', err.text) | |
77 | ) | |
d50acfab | 78 | } |
4635f59d C |
79 | |
80 | isAddButtonDisplayed () { | |
81 | return this.form.value['text'] | |
82 | } | |
83 | ||
cf117aaa C |
84 | getUserAvatarUrl () { |
85 | return this.user.getAvatarUrl() | |
86 | } | |
87 | ||
4635f59d C |
88 | private addCommentReply (commentCreate: VideoCommentCreate) { |
89 | return this.videoCommentService | |
90 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
91 | } | |
92 | ||
93 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
94 | return this.videoCommentService | |
95 | .addCommentThread(this.video.id, commentCreate) | |
96 | } | |
97 | } |