diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-27 16:11:53 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-27 16:11:53 +0100 |
commit | 4635f59d7c3fea4b97029f10886c62fdf38b2084 (patch) | |
tree | d97357a00042bbfb33c4177ee24c01171d28dfce /client/src/app/videos/+video-watch/comment/video-comment-add.component.ts | |
parent | ea44f375f5d3da06ca0aebfe871b9f924a26ec29 (diff) | |
download | PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.tar.gz PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.tar.zst PeerTube-4635f59d7c3fea4b97029f10886c62fdf38b2084.zip |
Add video comment components
Diffstat (limited to 'client/src/app/videos/+video-watch/comment/video-comment-add.component.ts')
-rw-r--r-- | client/src/app/videos/+video-watch/comment/video-comment-add.component.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/client/src/app/videos/+video-watch/comment/video-comment-add.component.ts b/client/src/app/videos/+video-watch/comment/video-comment-add.component.ts new file mode 100644 index 000000000..5ad83fc47 --- /dev/null +++ b/client/src/app/videos/+video-watch/comment/video-comment-add.component.ts | |||
@@ -0,0 +1,84 @@ | |||
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 | } | ||