]>
Commit | Line | Data |
---|---|---|
4635f59d C |
1 | import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core' |
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' | |
8 | import { Video } from '../../../shared/video/video.model' | |
9 | import { VideoComment } from './video-comment.model' | |
10 | import { VideoCommentService } from './video-comment.service' | |
11 | ||
12 | @Component({ | |
13 | selector: 'my-video-comment-add', | |
14 | templateUrl: './video-comment-add.component.html', | |
15 | styleUrls: ['./video-comment-add.component.scss'] | |
16 | }) | |
17 | export class VideoCommentAddComponent extends FormReactive implements OnInit { | |
18 | @Input() video: Video | |
19 | @Input() parentComment: VideoComment | |
20 | ||
21 | @Output() commentCreated = new EventEmitter<VideoCommentCreate>() | |
22 | ||
23 | form: FormGroup | |
24 | formErrors = { | |
25 | 'text': '' | |
26 | } | |
27 | validationMessages = { | |
28 | 'text': VIDEO_COMMENT_TEXT.MESSAGES | |
29 | } | |
30 | ||
31 | constructor ( | |
32 | private formBuilder: FormBuilder, | |
33 | private notificationsService: NotificationsService, | |
34 | private videoCommentService: VideoCommentService | |
35 | ) { | |
36 | super() | |
37 | } | |
38 | ||
39 | buildForm () { | |
40 | this.form = this.formBuilder.group({ | |
41 | text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ] | |
42 | }) | |
43 | ||
44 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
45 | } | |
46 | ||
47 | ngOnInit () { | |
48 | this.buildForm() | |
49 | } | |
50 | ||
51 | formValidated () { | |
52 | const commentCreate: VideoCommentCreate = this.form.value | |
53 | let obs: Observable<any> | |
54 | ||
55 | if (this.parentComment) { | |
56 | obs = this.addCommentReply(commentCreate) | |
57 | } else { | |
58 | obs = this.addCommentThread(commentCreate) | |
59 | } | |
60 | ||
61 | obs.subscribe( | |
62 | comment => { | |
63 | this.commentCreated.emit(comment) | |
64 | this.form.reset() | |
65 | }, | |
66 | ||
67 | err => this.notificationsService.error('Error', err.text) | |
68 | ) | |
69 | } | |
70 | ||
71 | isAddButtonDisplayed () { | |
72 | return this.form.value['text'] | |
73 | } | |
74 | ||
75 | private addCommentReply (commentCreate: VideoCommentCreate) { | |
76 | return this.videoCommentService | |
77 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
78 | } | |
79 | ||
80 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
81 | return this.videoCommentService | |
82 | .addCommentThread(this.video.id, commentCreate) | |
83 | } | |
84 | } |