X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=client%2Fsrc%2Fapp%2Fvideos%2F%2Bvideo-watch%2Fcomment%2Fvideo-comment-add.component.ts;h=9998685e8fda24ebea4496b38a2aea7dc99b231c;hb=ff33642709d7d6d3953ca8e6ce11fe4ba4a56593;hp=3e064efcbb67d738ce024bc312312d6485a2b737;hpb=d7e70384a360cda51fe23712331110a5c8b1124c;p=github%2FChocobozzz%2FPeerTube.git 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 index 3e064efcb..9998685e8 100644 --- 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 @@ -1,14 +1,15 @@ import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' -import { FormBuilder, FormGroup } from '@angular/forms' import { NotificationsService } from 'angular2-notifications' -import { Observable } from 'rxjs/Observable' -import { VideoCommentCreate, VideoCommentThreadTree } from '../../../../../../shared/models/videos/video-comment.model' +import { Observable } from 'rxjs' +import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model' import { FormReactive } from '../../../shared' -import { VIDEO_COMMENT_TEXT } from '../../../shared/forms/form-validators/video-comment' import { User } from '../../../shared/users' import { Video } from '../../../shared/video/video.model' import { VideoComment } from './video-comment.model' import { VideoCommentService } from './video-comment.service' +import { I18n } from '@ngx-translate/i18n-polyfill' +import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' +import { VideoCommentValidatorsService } from '@app/shared/forms/form-validators/video-comment-validators.service' @Component({ selector: 'my-video-comment-add', @@ -24,34 +25,24 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit { @Output() commentCreated = new EventEmitter() - form: FormGroup - formErrors = { - 'text': '' - } - validationMessages = { - 'text': VIDEO_COMMENT_TEXT.MESSAGES - } - @ViewChild('textarea') private textareaElement: ElementRef + private addingComment = false + constructor ( - private formBuilder: FormBuilder, + protected formValidatorService: FormValidatorService, + private videoCommentValidatorsService: VideoCommentValidatorsService, private notificationsService: NotificationsService, - private videoCommentService: VideoCommentService + private videoCommentService: VideoCommentService, + private i18n: I18n ) { super() } - buildForm () { - this.form = this.formBuilder.group({ - text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ] - }) - - this.form.valueChanges.subscribe(data => this.onValueChanged(data)) - } - ngOnInit () { - this.buildForm() + this.buildForm({ + text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT + }) if (this.focusOnInit === true) { this.textareaElement.nativeElement.focus() @@ -59,8 +50,8 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit { if (this.parentComment) { const mentions = this.parentComments - .filter(c => c.account.id !== this.user.account.id) - .map(c => '@' + c.account.name) + .filter(c => c.account.id !== this.user.account.id) // Don't add mention of ourselves + .map(c => '@' + c.by) const mentionsSet = new Set(mentions) const mentionsText = Array.from(mentionsSet).join(' ') + ' ' @@ -69,7 +60,19 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit { } } + onValidKey () { + this.onValueChanged() + if (!this.form.valid) return + + this.formValidated() + } + formValidated () { + // If we validate very quickly the comment form, we might comment twice + if (this.addingComment) return + + this.addingComment = true + const commentCreate: VideoCommentCreate = this.form.value let obs: Observable @@ -81,11 +84,16 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit { obs.subscribe( comment => { + this.addingComment = false this.commentCreated.emit(comment) this.form.reset() }, - err => this.notificationsService.error('Error', err.text) + err => { + this.addingComment = false + + this.notificationsService.error(this.i18n('Error'), err.text) + } ) } @@ -93,10 +101,6 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit { return this.form.value['text'] } - getUserAvatarUrl () { - return this.user.getAvatarUrl() - } - private addCommentReply (commentCreate: VideoCommentCreate) { return this.videoCommentService .addCommentReply(this.video.id, this.parentComment.id, commentCreate)