diff options
author | Chocobozzz <me@florianbigard.com> | 2018-06-05 10:58:45 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-06-05 10:58:45 +0200 |
commit | d18d64787b3ea174f7dc2740c8c8c9555625047e (patch) | |
tree | e65089e0ca81117c1ada981b9b8a524afa8d70f5 /client/src/app/+admin | |
parent | 25acef90a85c1584880dec96aa402f896af8364a (diff) | |
download | PeerTube-d18d64787b3ea174f7dc2740c8c8c9555625047e.tar.gz PeerTube-d18d64787b3ea174f7dc2740c8c8c9555625047e.tar.zst PeerTube-d18d64787b3ea174f7dc2740c8c8c9555625047e.zip |
Form validators refractoring
Diffstat (limited to 'client/src/app/+admin')
4 files changed, 48 insertions, 112 deletions
diff --git a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts index 73ff4b7bb..f2a3464cb 100644 --- a/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts +++ b/client/src/app/+admin/config/edit-custom-config/edit-custom-config.component.ts | |||
@@ -17,6 +17,7 @@ import { | |||
17 | import { NotificationsService } from 'angular2-notifications' | 17 | import { NotificationsService } from 'angular2-notifications' |
18 | import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model' | 18 | import { CustomConfig } from '../../../../../../shared/models/server/custom-config.model' |
19 | import { I18n } from '@ngx-translate/i18n-polyfill' | 19 | import { I18n } from '@ngx-translate/i18n-polyfill' |
20 | import { BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | ||
20 | 21 | ||
21 | @Component({ | 22 | @Component({ |
22 | selector: 'my-edit-custom-config', | 23 | selector: 'my-edit-custom-config', |
@@ -44,38 +45,11 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { | |||
44 | { value: 8, label: '8' } | 45 | { value: 8, label: '8' } |
45 | ] | 46 | ] |
46 | 47 | ||
47 | form: FormGroup | ||
48 | formErrors = { | ||
49 | instanceName: '', | ||
50 | instanceShortDescription: '', | ||
51 | instanceDescription: '', | ||
52 | instanceTerms: '', | ||
53 | instanceDefaultClientRoute: '', | ||
54 | instanceDefaultNSFWPolicy: '', | ||
55 | servicesTwitterUsername: '', | ||
56 | cachePreviewsSize: '', | ||
57 | signupLimit: '', | ||
58 | adminEmail: '', | ||
59 | userVideoQuota: '', | ||
60 | transcodingThreads: '', | ||
61 | customizationJavascript: '', | ||
62 | customizationCSS: '' | ||
63 | } | ||
64 | validationMessages = { | ||
65 | instanceShortDescription: INSTANCE_SHORT_DESCRIPTION.MESSAGES, | ||
66 | instanceName: INSTANCE_NAME.MESSAGES, | ||
67 | servicesTwitterUsername: SERVICES_TWITTER_USERNAME, | ||
68 | cachePreviewsSize: CACHE_PREVIEWS_SIZE.MESSAGES, | ||
69 | signupLimit: SIGNUP_LIMIT.MESSAGES, | ||
70 | adminEmail: ADMIN_EMAIL.MESSAGES, | ||
71 | userVideoQuota: USER_VIDEO_QUOTA.MESSAGES | ||
72 | } | ||
73 | |||
74 | private oldCustomJavascript: string | 48 | private oldCustomJavascript: string |
75 | private oldCustomCSS: string | 49 | private oldCustomCSS: string |
76 | 50 | ||
77 | constructor ( | 51 | constructor ( |
78 | private formBuilder: FormBuilder, | 52 | protected formValidatorService: FormValidatorService, |
79 | private router: Router, | 53 | private router: Router, |
80 | private notificationsService: NotificationsService, | 54 | private notificationsService: NotificationsService, |
81 | private configService: ConfigService, | 55 | private configService: ConfigService, |
@@ -90,39 +64,35 @@ export class EditCustomConfigComponent extends FormReactive implements OnInit { | |||
90 | return 'transcodingResolution' + resolution | 64 | return 'transcodingResolution' + resolution |
91 | } | 65 | } |
92 | 66 | ||
93 | buildForm () { | 67 | ngOnInit () { |
94 | const formGroupData = { | 68 | const formGroupData = { |
95 | instanceName: [ '', INSTANCE_NAME.VALIDATORS ], | 69 | instanceName: INSTANCE_NAME, |
96 | instanceShortDescription: [ '', INSTANCE_SHORT_DESCRIPTION.VALIDATORS ], | 70 | instanceShortDescription: INSTANCE_SHORT_DESCRIPTION, |
97 | instanceDescription: [ '' ], | 71 | instanceDescription: null, |
98 | instanceTerms: [ '' ], | 72 | instanceTerms: null, |
99 | instanceDefaultClientRoute: [ '' ], | 73 | instanceDefaultClientRoute: null, |
100 | instanceDefaultNSFWPolicy: [ '' ], | 74 | instanceDefaultNSFWPolicy: null, |
101 | servicesTwitterUsername: [ '', SERVICES_TWITTER_USERNAME.VALIDATORS ], | 75 | servicesTwitterUsername: SERVICES_TWITTER_USERNAME, |
102 | servicesTwitterWhitelisted: [ ], | 76 | servicesTwitterWhitelisted: null, |
103 | cachePreviewsSize: [ '', CACHE_PREVIEWS_SIZE.VALIDATORS ], | 77 | cachePreviewsSize: CACHE_PREVIEWS_SIZE, |
104 | signupEnabled: [ ], | 78 | signupEnabled: null, |
105 | signupLimit: [ '', SIGNUP_LIMIT.VALIDATORS ], | 79 | signupLimit: SIGNUP_LIMIT, |
106 | adminEmail: [ '', ADMIN_EMAIL.VALIDATORS ], | 80 | adminEmail: ADMIN_EMAIL, |
107 | userVideoQuota: [ '', USER_VIDEO_QUOTA.VALIDATORS ], | 81 | userVideoQuota: USER_VIDEO_QUOTA, |
108 | transcodingThreads: [ '', TRANSCODING_THREADS.VALIDATORS ], | 82 | transcodingThreads: TRANSCODING_THREADS, |
109 | transcodingEnabled: [ ], | 83 | transcodingEnabled: null, |
110 | customizationJavascript: [ '' ], | 84 | customizationJavascript: null, |
111 | customizationCSS: [ '' ] | 85 | customizationCSS: null |
112 | } | 86 | } |
113 | 87 | ||
88 | const defaultValues: BuildFormDefaultValues = {} | ||
114 | for (const resolution of this.resolutions) { | 89 | for (const resolution of this.resolutions) { |
115 | const key = this.getResolutionKey(resolution) | 90 | const key = this.getResolutionKey(resolution) |
116 | formGroupData[key] = [ false ] | 91 | defaultValues[key] = 'false' |
92 | formGroupData[key] = null | ||
117 | } | 93 | } |
118 | 94 | ||
119 | this.form = this.formBuilder.group(formGroupData) | 95 | this.buildForm(formGroupData) |
120 | |||
121 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | ||
122 | } | ||
123 | |||
124 | ngOnInit () { | ||
125 | this.buildForm() | ||
126 | 96 | ||
127 | this.configService.getCustomConfig() | 97 | this.configService.getCustomConfig() |
128 | .subscribe( | 98 | .subscribe( |
diff --git a/client/src/app/+admin/users/user-edit/user-create.component.ts b/client/src/app/+admin/users/user-edit/user-create.component.ts index 8478a7692..e5f0903b6 100644 --- a/client/src/app/+admin/users/user-edit/user-create.component.ts +++ b/client/src/app/+admin/users/user-edit/user-create.component.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | import { Component, OnInit } from '@angular/core' | 1 | import { Component, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | ||
3 | import { Router } from '@angular/router' | 2 | import { Router } from '@angular/router' |
4 | import { NotificationsService } from 'angular2-notifications' | 3 | import { NotificationsService } from 'angular2-notifications' |
5 | import { UserService } from '../shared' | 4 | import { UserService } from '../shared' |
@@ -8,6 +7,7 @@ import { ServerService } from '../../../core' | |||
8 | import { UserCreate, UserRole } from '../../../../../../shared' | 7 | import { UserCreate, UserRole } from '../../../../../../shared' |
9 | import { UserEdit } from './user-edit' | 8 | import { UserEdit } from './user-edit' |
10 | import { I18n } from '@ngx-translate/i18n-polyfill' | 9 | import { I18n } from '@ngx-translate/i18n-polyfill' |
10 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | ||
11 | 11 | ||
12 | @Component({ | 12 | @Component({ |
13 | selector: 'my-user-create', | 13 | selector: 'my-user-create', |
@@ -17,25 +17,9 @@ import { I18n } from '@ngx-translate/i18n-polyfill' | |||
17 | export class UserCreateComponent extends UserEdit implements OnInit { | 17 | export class UserCreateComponent extends UserEdit implements OnInit { |
18 | error: string | 18 | error: string |
19 | 19 | ||
20 | form: FormGroup | ||
21 | formErrors = { | ||
22 | 'username': '', | ||
23 | 'email': '', | ||
24 | 'password': '', | ||
25 | 'role': '', | ||
26 | 'videoQuota': '' | ||
27 | } | ||
28 | validationMessages = { | ||
29 | 'username': USER_USERNAME.MESSAGES, | ||
30 | 'email': USER_EMAIL.MESSAGES, | ||
31 | 'password': USER_PASSWORD.MESSAGES, | ||
32 | 'role': USER_ROLE.MESSAGES, | ||
33 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES | ||
34 | } | ||
35 | |||
36 | constructor ( | 20 | constructor ( |
37 | protected serverService: ServerService, | 21 | protected serverService: ServerService, |
38 | private formBuilder: FormBuilder, | 22 | protected formValidatorService: FormValidatorService, |
39 | private router: Router, | 23 | private router: Router, |
40 | private notificationsService: NotificationsService, | 24 | private notificationsService: NotificationsService, |
41 | private userService: UserService, | 25 | private userService: UserService, |
@@ -44,20 +28,19 @@ export class UserCreateComponent extends UserEdit implements OnInit { | |||
44 | super() | 28 | super() |
45 | } | 29 | } |
46 | 30 | ||
47 | buildForm () { | ||
48 | this.form = this.formBuilder.group({ | ||
49 | username: [ '', USER_USERNAME.VALIDATORS ], | ||
50 | email: [ '', USER_EMAIL.VALIDATORS ], | ||
51 | password: [ '', USER_PASSWORD.VALIDATORS ], | ||
52 | role: [ UserRole.USER, USER_ROLE.VALIDATORS ], | ||
53 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] | ||
54 | }) | ||
55 | |||
56 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | ||
57 | } | ||
58 | |||
59 | ngOnInit () { | 31 | ngOnInit () { |
60 | this.buildForm() | 32 | const defaultValues = { |
33 | role: UserRole.USER.toString(), | ||
34 | videoQuota: '-1' | ||
35 | } | ||
36 | |||
37 | this.buildForm({ | ||
38 | username: USER_USERNAME, | ||
39 | email: USER_EMAIL, | ||
40 | password: USER_PASSWORD, | ||
41 | role: USER_ROLE, | ||
42 | videoQuota: USER_VIDEO_QUOTA | ||
43 | }, defaultValues) | ||
61 | } | 44 | } |
62 | 45 | ||
63 | formValidated () { | 46 | formValidated () { |
diff --git a/client/src/app/+admin/users/user-edit/user-edit.ts b/client/src/app/+admin/users/user-edit/user-edit.ts index 2b47c685c..ea8c733c3 100644 --- a/client/src/app/+admin/users/user-edit/user-edit.ts +++ b/client/src/app/+admin/users/user-edit/user-edit.ts | |||
@@ -12,9 +12,9 @@ export abstract class UserEdit extends FormReactive { | |||
12 | { value: 5 * 1024 * 1024 * 1024, label: '5GB' }, | 12 | { value: 5 * 1024 * 1024 * 1024, label: '5GB' }, |
13 | { value: 20 * 1024 * 1024 * 1024, label: '20GB' }, | 13 | { value: 20 * 1024 * 1024 * 1024, label: '20GB' }, |
14 | { value: 50 * 1024 * 1024 * 1024, label: '50GB' } | 14 | { value: 50 * 1024 * 1024 * 1024, label: '50GB' } |
15 | ] | 15 | ].map(q => ({ value: q.value.toString(), label: q.label })) // Used by a HTML select, so convert key into strings |
16 | 16 | ||
17 | roles = Object.keys(USER_ROLE_LABELS).map(key => ({ value: key, label: USER_ROLE_LABELS[key] })) | 17 | roles = Object.keys(USER_ROLE_LABELS).map(key => ({ value: key.toString(), label: USER_ROLE_LABELS[key] })) |
18 | 18 | ||
19 | protected abstract serverService: ServerService | 19 | protected abstract serverService: ServerService |
20 | abstract isCreation (): boolean | 20 | abstract isCreation (): boolean |
diff --git a/client/src/app/+admin/users/user-edit/user-update.component.ts b/client/src/app/+admin/users/user-edit/user-update.component.ts index 5689aab2f..f8073c928 100644 --- a/client/src/app/+admin/users/user-edit/user-update.component.ts +++ b/client/src/app/+admin/users/user-edit/user-update.component.ts | |||
@@ -1,5 +1,4 @@ | |||
1 | import { Component, OnDestroy, OnInit } from '@angular/core' | 1 | import { Component, OnDestroy, OnInit } from '@angular/core' |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | ||
3 | import { ActivatedRoute, Router } from '@angular/router' | 2 | import { ActivatedRoute, Router } from '@angular/router' |
4 | import { Subscription } from 'rxjs' | 3 | import { Subscription } from 'rxjs' |
5 | import { NotificationsService } from 'angular2-notifications' | 4 | import { NotificationsService } from 'angular2-notifications' |
@@ -9,6 +8,7 @@ import { ServerService } from '../../../core' | |||
9 | import { UserEdit } from './user-edit' | 8 | import { UserEdit } from './user-edit' |
10 | import { UserUpdate } from '../../../../../../shared' | 9 | import { UserUpdate } from '../../../../../../shared' |
11 | import { I18n } from '@ngx-translate/i18n-polyfill' | 10 | import { I18n } from '@ngx-translate/i18n-polyfill' |
11 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | ||
12 | 12 | ||
13 | @Component({ | 13 | @Component({ |
14 | selector: 'my-user-update', | 14 | selector: 'my-user-update', |
@@ -20,44 +20,27 @@ export class UserUpdateComponent extends UserEdit implements OnInit, OnDestroy { | |||
20 | userId: number | 20 | userId: number |
21 | username: string | 21 | username: string |
22 | 22 | ||
23 | form: FormGroup | ||
24 | formErrors = { | ||
25 | 'email': '', | ||
26 | 'role': '', | ||
27 | 'videoQuota': '' | ||
28 | } | ||
29 | validationMessages = { | ||
30 | 'email': USER_EMAIL.MESSAGES, | ||
31 | 'role': USER_ROLE.MESSAGES, | ||
32 | 'videoQuota': USER_VIDEO_QUOTA.MESSAGES | ||
33 | } | ||
34 | |||
35 | private paramsSub: Subscription | 23 | private paramsSub: Subscription |
36 | 24 | ||
37 | constructor ( | 25 | constructor ( |
26 | protected formValidatorService: FormValidatorService, | ||
38 | protected serverService: ServerService, | 27 | protected serverService: ServerService, |
39 | private route: ActivatedRoute, | 28 | private route: ActivatedRoute, |
40 | private router: Router, | 29 | private router: Router, |
41 | private notificationsService: NotificationsService, | 30 | private notificationsService: NotificationsService, |
42 | private formBuilder: FormBuilder, | ||
43 | private userService: UserService, | 31 | private userService: UserService, |
44 | private i18n: I18n | 32 | private i18n: I18n |
45 | ) { | 33 | ) { |
46 | super() | 34 | super() |
47 | } | 35 | } |
48 | 36 | ||
49 | buildForm () { | ||
50 | this.form = this.formBuilder.group({ | ||
51 | email: [ '', USER_EMAIL.VALIDATORS ], | ||
52 | role: [ '', USER_ROLE.VALIDATORS ], | ||
53 | videoQuota: [ '-1', USER_VIDEO_QUOTA.VALIDATORS ] | ||
54 | }) | ||
55 | |||
56 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | ||
57 | } | ||
58 | |||
59 | ngOnInit () { | 37 | ngOnInit () { |
60 | this.buildForm() | 38 | const defaultValues = { videoQuota: '-1' } |
39 | this.buildForm({ | ||
40 | email: USER_EMAIL, | ||
41 | role: USER_ROLE, | ||
42 | videoQuota: USER_VIDEO_QUOTA | ||
43 | }, defaultValues) | ||
61 | 44 | ||
62 | this.paramsSub = this.route.params.subscribe(routeParams => { | 45 | this.paramsSub = this.route.params.subscribe(routeParams => { |
63 | const userId = routeParams['id'] | 46 | const userId = routeParams['id'] |