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/users | |
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/users')
3 files changed, 24 insertions, 58 deletions
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'] |