aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/videos
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-06-05 10:58:45 +0200
committerChocobozzz <me@florianbigard.com>2018-06-05 10:58:45 +0200
commitd18d64787b3ea174f7dc2740c8c8c9555625047e (patch)
treee65089e0ca81117c1ada981b9b8a524afa8d70f5 /client/src/app/videos
parent25acef90a85c1584880dec96aa402f896af8364a (diff)
downloadPeerTube-d18d64787b3ea174f7dc2740c8c8c9555625047e.tar.gz
PeerTube-d18d64787b3ea174f7dc2740c8c8c9555625047e.tar.zst
PeerTube-d18d64787b3ea174f7dc2740c8c8c9555625047e.zip
Form validators refractoring
Diffstat (limited to 'client/src/app/videos')
-rw-r--r--client/src/app/videos/+video-edit/shared/video-edit.component.ts71
-rw-r--r--client/src/app/videos/+video-edit/video-add.component.ts16
-rw-r--r--client/src/app/videos/+video-edit/video-update.component.ts15
-rw-r--r--client/src/app/videos/+video-watch/comment/video-comment-add.component.ts24
-rw-r--r--client/src/app/videos/+video-watch/modal/video-report.component.ts21
5 files changed, 49 insertions, 98 deletions
diff --git a/client/src/app/videos/+video-edit/shared/video-edit.component.ts b/client/src/app/videos/+video-edit/shared/video-edit.component.ts
index ccfae5fcc..cd2a26ae3 100644
--- a/client/src/app/videos/+video-edit/shared/video-edit.component.ts
+++ b/client/src/app/videos/+video-edit/shared/video-edit.component.ts
@@ -1,7 +1,7 @@
1import { Component, Input, OnInit } from '@angular/core' 1import { Component, Input, OnInit } from '@angular/core'
2import { FormBuilder, FormControl, FormGroup } from '@angular/forms' 2import { FormGroup } from '@angular/forms'
3import { ActivatedRoute, Router } from '@angular/router' 3import { ActivatedRoute, Router } from '@angular/router'
4import { VIDEO_IMAGE, VIDEO_SUPPORT } from '@app/shared' 4import { VIDEO_SUPPORT } from '@app/shared'
5import { NotificationsService } from 'angular2-notifications' 5import { NotificationsService } from 'angular2-notifications'
6import { ServerService } from '../../../core/server' 6import { ServerService } from '../../../core/server'
7import { VIDEO_CHANNEL } from '../../../shared/forms/form-validators' 7import { VIDEO_CHANNEL } from '../../../shared/forms/form-validators'
@@ -17,6 +17,7 @@ import {
17} from '../../../shared/forms/form-validators/video' 17} from '../../../shared/forms/form-validators/video'
18import { VideoEdit } from '../../../shared/video/video-edit.model' 18import { VideoEdit } from '../../../shared/video/video-edit.model'
19import { map } from 'rxjs/operators' 19import { map } from 'rxjs/operators'
20import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
20 21
21@Component({ 22@Component({
22 selector: 'my-video-edit', 23 selector: 'my-video-edit',
@@ -42,7 +43,7 @@ export class VideoEditComponent implements OnInit {
42 error: string = null 43 error: string = null
43 44
44 constructor ( 45 constructor (
45 private formBuilder: FormBuilder, 46 private formValidatorService: FormValidatorService,
46 private route: ActivatedRoute, 47 private route: ActivatedRoute,
47 private router: Router, 48 private router: Router,
48 private notificationsService: NotificationsService, 49 private notificationsService: NotificationsService,
@@ -50,41 +51,34 @@ export class VideoEditComponent implements OnInit {
50 ) { } 51 ) { }
51 52
52 updateForm () { 53 updateForm () {
53 this.formErrors['name'] = '' 54 const defaultValues = {
54 this.formErrors['privacy'] = '' 55 nsfw: 'false',
55 this.formErrors['channelId'] = '' 56 commentsEnabled: 'true',
56 this.formErrors['category'] = '' 57 tags: []
57 this.formErrors['licence'] = '' 58 }
58 this.formErrors['language'] = '' 59 const obj = {
59 this.formErrors['description'] = '' 60 name: VIDEO_NAME,
60 this.formErrors['thumbnailfile'] = '' 61 privacy: VIDEO_PRIVACY,
61 this.formErrors['previewfile'] = '' 62 channelId: VIDEO_CHANNEL,
62 this.formErrors['support'] = '' 63 nsfw: null,
63 64 commentsEnabled: null,
64 this.validationMessages['name'] = VIDEO_NAME.MESSAGES 65 category: VIDEO_CATEGORY,
65 this.validationMessages['privacy'] = VIDEO_PRIVACY.MESSAGES 66 licence: VIDEO_LICENCE,
66 this.validationMessages['channelId'] = VIDEO_CHANNEL.MESSAGES 67 language: VIDEO_LANGUAGE,
67 this.validationMessages['category'] = VIDEO_CATEGORY.MESSAGES 68 description: VIDEO_DESCRIPTION,
68 this.validationMessages['licence'] = VIDEO_LICENCE.MESSAGES 69 tags: null,
69 this.validationMessages['language'] = VIDEO_LANGUAGE.MESSAGES 70 thumbnailfile: null,
70 this.validationMessages['description'] = VIDEO_DESCRIPTION.MESSAGES 71 previewfile: null,
71 this.validationMessages['thumbnailfile'] = VIDEO_IMAGE.MESSAGES 72 support: VIDEO_SUPPORT
72 this.validationMessages['previewfile'] = VIDEO_IMAGE.MESSAGES 73 }
73 this.validationMessages['support'] = VIDEO_SUPPORT.MESSAGES 74
74 75 this.formValidatorService.updateForm(
75 this.form.addControl('name', new FormControl('', VIDEO_NAME.VALIDATORS)) 76 this.form,
76 this.form.addControl('privacy', new FormControl('', VIDEO_PRIVACY.VALIDATORS)) 77 this.formErrors,
77 this.form.addControl('channelId', new FormControl('', VIDEO_CHANNEL.VALIDATORS)) 78 this.validationMessages,
78 this.form.addControl('nsfw', new FormControl(false)) 79 obj,
79 this.form.addControl('commentsEnabled', new FormControl(true)) 80 defaultValues
80 this.form.addControl('category', new FormControl('', VIDEO_CATEGORY.VALIDATORS)) 81 )
81 this.form.addControl('licence', new FormControl('', VIDEO_LICENCE.VALIDATORS))
82 this.form.addControl('language', new FormControl('', VIDEO_LANGUAGE.VALIDATORS))
83 this.form.addControl('description', new FormControl('', VIDEO_DESCRIPTION.VALIDATORS))
84 this.form.addControl('tags', new FormControl([]))
85 this.form.addControl('thumbnailfile', new FormControl(''))
86 this.form.addControl('previewfile', new FormControl(''))
87 this.form.addControl('support', new FormControl('', VIDEO_SUPPORT.VALIDATORS))
88 82
89 // We will update the "support" field depending on the channel 83 // We will update the "support" field depending on the channel
90 this.form.controls['channelId'] 84 this.form.controls['channelId']
@@ -98,6 +92,7 @@ export class VideoEditComponent implements OnInit {
98 // Not initialized yet 92 // Not initialized yet
99 if (isNaN(newChannelId)) return 93 if (isNaN(newChannelId)) return
100 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId) 94 const newChannel = this.userVideoChannels.find(c => c.id === newChannelId)
95 if (!newChannel) return
101 96
102 // First time we set the channel? 97 // First time we set the channel?
103 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support) 98 if (isNaN(oldChannelId)) return this.updateSupportField(newChannel.support)
diff --git a/client/src/app/videos/+video-edit/video-add.component.ts b/client/src/app/videos/+video-edit/video-add.component.ts
index a615fd92c..332f757d7 100644
--- a/client/src/app/videos/+video-edit/video-add.component.ts
+++ b/client/src/app/videos/+video-edit/video-add.component.ts
@@ -1,6 +1,5 @@
1import { HttpEventType, HttpResponse } from '@angular/common/http' 1import { HttpEventType, HttpResponse } from '@angular/common/http'
2import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core' 2import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core'
3import { FormBuilder, FormGroup } from '@angular/forms'
4import { Router } from '@angular/router' 3import { Router } from '@angular/router'
5import { UserService } from '@app/shared' 4import { UserService } from '@app/shared'
6import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service' 5import { CanComponentDeactivate } from '@app/shared/guards/can-deactivate-guard.service'
@@ -11,11 +10,11 @@ import { Subscription } from 'rxjs'
11import { VideoPrivacy } from '../../../../../shared/models/videos' 10import { VideoPrivacy } from '../../../../../shared/models/videos'
12import { AuthService, ServerService } from '../../core' 11import { AuthService, ServerService } from '../../core'
13import { FormReactive } from '../../shared' 12import { FormReactive } from '../../shared'
14import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
15import { populateAsyncUserVideoChannels } from '../../shared/misc/utils' 13import { populateAsyncUserVideoChannels } from '../../shared/misc/utils'
16import { VideoEdit } from '../../shared/video/video-edit.model' 14import { VideoEdit } from '../../shared/video/video-edit.model'
17import { VideoService } from '../../shared/video/video.service' 15import { VideoService } from '../../shared/video/video.service'
18import { I18n } from '@ngx-translate/i18n-polyfill' 16import { I18n } from '@ngx-translate/i18n-polyfill'
17import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
19 18
20@Component({ 19@Component({
21 selector: 'my-videos-add', 20 selector: 'my-videos-add',
@@ -39,10 +38,6 @@ export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy
39 } 38 }
40 videoFileName: string 39 videoFileName: string
41 40
42 form: FormGroup
43 formErrors: { [ id: string ]: string } = {}
44 validationMessages: ValidatorMessage = {}
45
46 userVideoChannels: { id: number, label: string, support: string }[] = [] 41 userVideoChannels: { id: number, label: string, support: string }[] = []
47 userVideoQuotaUsed = 0 42 userVideoQuotaUsed = 0
48 videoPrivacies = [] 43 videoPrivacies = []
@@ -50,7 +45,7 @@ export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy
50 firstStepChannelId = 0 45 firstStepChannelId = 0
51 46
52 constructor ( 47 constructor (
53 private formBuilder: FormBuilder, 48 protected formValidatorService: FormValidatorService,
54 private router: Router, 49 private router: Router,
55 private notificationsService: NotificationsService, 50 private notificationsService: NotificationsService,
56 private authService: AuthService, 51 private authService: AuthService,
@@ -67,13 +62,8 @@ export class VideoAddComponent extends FormReactive implements OnInit, OnDestroy
67 return this.serverService.getConfig().video.file.extensions.join(',') 62 return this.serverService.getConfig().video.file.extensions.join(',')
68 } 63 }
69 64
70 buildForm () {
71 this.form = this.formBuilder.group({})
72 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
73 }
74
75 ngOnInit () { 65 ngOnInit () {
76 this.buildForm() 66 this.buildForm({})
77 67
78 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels) 68 populateAsyncUserVideoChannels(this.authService, this.userVideoChannels)
79 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id) 69 .then(() => this.firstStepChannelId = this.userVideoChannels[0].id)
diff --git a/client/src/app/videos/+video-edit/video-update.component.ts b/client/src/app/videos/+video-edit/video-update.component.ts
index e37dd526f..0266164af 100644
--- a/client/src/app/videos/+video-edit/video-update.component.ts
+++ b/client/src/app/videos/+video-edit/video-update.component.ts
@@ -1,6 +1,5 @@
1import { map, switchMap } from 'rxjs/operators' 1import { map, switchMap } from 'rxjs/operators'
2import { Component, OnInit } from '@angular/core' 2import { Component, OnInit } from '@angular/core'
3import { FormBuilder, FormGroup } from '@angular/forms'
4import { ActivatedRoute, Router } from '@angular/router' 3import { ActivatedRoute, Router } from '@angular/router'
5import { LoadingBarService } from '@ngx-loading-bar/core' 4import { LoadingBarService } from '@ngx-loading-bar/core'
6import { NotificationsService } from 'angular2-notifications' 5import { NotificationsService } from 'angular2-notifications'
@@ -8,11 +7,11 @@ import { VideoPrivacy } from '../../../../../shared/models/videos'
8import { ServerService } from '../../core' 7import { ServerService } from '../../core'
9import { AuthService } from '../../core/auth' 8import { AuthService } from '../../core/auth'
10import { FormReactive } from '../../shared' 9import { FormReactive } from '../../shared'
11import { ValidatorMessage } from '../../shared/forms/form-validators/validator-message'
12import { VideoEdit } from '../../shared/video/video-edit.model' 10import { VideoEdit } from '../../shared/video/video-edit.model'
13import { VideoService } from '../../shared/video/video.service' 11import { VideoService } from '../../shared/video/video.service'
14import { VideoChannelService } from '@app/shared/video-channel/video-channel.service' 12import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
15import { I18n } from '@ngx-translate/i18n-polyfill' 13import { I18n } from '@ngx-translate/i18n-polyfill'
14import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
16 15
17@Component({ 16@Component({
18 selector: 'my-videos-update', 17 selector: 'my-videos-update',
@@ -23,14 +22,11 @@ export class VideoUpdateComponent extends FormReactive implements OnInit {
23 video: VideoEdit 22 video: VideoEdit
24 23
25 isUpdatingVideo = false 24 isUpdatingVideo = false
26 form: FormGroup
27 formErrors: { [ id: string ]: string } = {}
28 validationMessages: ValidatorMessage = {}
29 videoPrivacies = [] 25 videoPrivacies = []
30 userVideoChannels = [] 26 userVideoChannels = []
31 27
32 constructor ( 28 constructor (
33 private formBuilder: FormBuilder, 29 protected formValidatorService: FormValidatorService,
34 private route: ActivatedRoute, 30 private route: ActivatedRoute,
35 private router: Router, 31 private router: Router,
36 private notificationsService: NotificationsService, 32 private notificationsService: NotificationsService,
@@ -44,13 +40,8 @@ export class VideoUpdateComponent extends FormReactive implements OnInit {
44 super() 40 super()
45 } 41 }
46 42
47 buildForm () {
48 this.form = this.formBuilder.group({})
49 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
50 }
51
52 ngOnInit () { 43 ngOnInit () {
53 this.buildForm() 44 this.buildForm({})
54 45
55 this.serverService.videoPrivaciesLoaded 46 this.serverService.videoPrivaciesLoaded
56 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies()) 47 .subscribe(() => this.videoPrivacies = this.serverService.getVideoPrivacies())
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 2ee5f5ef1..c70e544a3 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,5 +1,4 @@
1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core' 1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2import { FormBuilder, FormGroup } from '@angular/forms'
3import { NotificationsService } from 'angular2-notifications' 2import { NotificationsService } from 'angular2-notifications'
4import { Observable } from 'rxjs' 3import { Observable } from 'rxjs'
5import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model' 4import { VideoCommentCreate } from '../../../../../../shared/models/videos/video-comment.model'
@@ -10,6 +9,7 @@ import { Video } from '../../../shared/video/video.model'
10import { VideoComment } from './video-comment.model' 9import { VideoComment } from './video-comment.model'
11import { VideoCommentService } from './video-comment.service' 10import { VideoCommentService } from './video-comment.service'
12import { I18n } from '@ngx-translate/i18n-polyfill' 11import { I18n } from '@ngx-translate/i18n-polyfill'
12import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
13 13
14@Component({ 14@Component({
15 selector: 'my-video-comment-add', 15 selector: 'my-video-comment-add',
@@ -25,18 +25,10 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit {
25 25
26 @Output() commentCreated = new EventEmitter<VideoCommentCreate>() 26 @Output() commentCreated = new EventEmitter<VideoCommentCreate>()
27 27
28 form: FormGroup
29 formErrors = {
30 'text': ''
31 }
32 validationMessages = {
33 'text': VIDEO_COMMENT_TEXT.MESSAGES
34 }
35
36 @ViewChild('textarea') private textareaElement: ElementRef 28 @ViewChild('textarea') private textareaElement: ElementRef
37 29
38 constructor ( 30 constructor (
39 private formBuilder: FormBuilder, 31 protected formValidatorService: FormValidatorService,
40 private notificationsService: NotificationsService, 32 private notificationsService: NotificationsService,
41 private videoCommentService: VideoCommentService, 33 private videoCommentService: VideoCommentService,
42 private i18n: I18n 34 private i18n: I18n
@@ -44,16 +36,10 @@ export class VideoCommentAddComponent extends FormReactive implements OnInit {
44 super() 36 super()
45 } 37 }
46 38
47 buildForm () {
48 this.form = this.formBuilder.group({
49 text: [ '', VIDEO_COMMENT_TEXT.VALIDATORS ]
50 })
51
52 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
53 }
54
55 ngOnInit () { 39 ngOnInit () {
56 this.buildForm() 40 this.buildForm({
41 text: VIDEO_COMMENT_TEXT
42 })
57 43
58 if (this.focusOnInit === true) { 44 if (this.focusOnInit === true) {
59 this.textareaElement.nativeElement.focus() 45 this.textareaElement.nativeElement.focus()
diff --git a/client/src/app/videos/+video-watch/modal/video-report.component.ts b/client/src/app/videos/+video-watch/modal/video-report.component.ts
index 1bbd30226..8641e8dfb 100644
--- a/client/src/app/videos/+video-watch/modal/video-report.component.ts
+++ b/client/src/app/videos/+video-watch/modal/video-report.component.ts
@@ -5,6 +5,8 @@ import { ModalDirective } from 'ngx-bootstrap/modal'
5import { FormReactive, VIDEO_ABUSE_REASON, VideoAbuseService } from '../../../shared/index' 5import { FormReactive, VIDEO_ABUSE_REASON, VideoAbuseService } from '../../../shared/index'
6import { VideoDetails } from '../../../shared/video/video-details.model' 6import { VideoDetails } from '../../../shared/video/video-details.model'
7import { I18n } from '@ngx-translate/i18n-polyfill' 7import { I18n } from '@ngx-translate/i18n-polyfill'
8import { FormReactiveErrors, FormReactiveValidationMessages } from '@app/shared'
9import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
8 10
9@Component({ 11@Component({
10 selector: 'my-video-report', 12 selector: 'my-video-report',
@@ -17,16 +19,9 @@ export class VideoReportComponent extends FormReactive implements OnInit {
17 @ViewChild('modal') modal: ModalDirective 19 @ViewChild('modal') modal: ModalDirective
18 20
19 error: string = null 21 error: string = null
20 form: FormGroup
21 formErrors = {
22 reason: ''
23 }
24 validationMessages = {
25 reason: VIDEO_ABUSE_REASON.MESSAGES
26 }
27 22
28 constructor ( 23 constructor (
29 private formBuilder: FormBuilder, 24 protected formValidatorService: FormValidatorService,
30 private videoAbuseService: VideoAbuseService, 25 private videoAbuseService: VideoAbuseService,
31 private notificationsService: NotificationsService, 26 private notificationsService: NotificationsService,
32 private i18n: I18n 27 private i18n: I18n
@@ -35,15 +30,9 @@ export class VideoReportComponent extends FormReactive implements OnInit {
35 } 30 }
36 31
37 ngOnInit () { 32 ngOnInit () {
38 this.buildForm() 33 this.buildForm({
39 } 34 reason: VIDEO_ABUSE_REASON
40
41 buildForm () {
42 this.form = this.formBuilder.group({
43 reason: [ '', VIDEO_ABUSE_REASON.VALIDATORS ]
44 }) 35 })
45
46 this.form.valueChanges.subscribe(data => this.onValueChanged(data))
47 } 36 }
48 37
49 show () { 38 show () {