X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;ds=inline;f=client%2Fsrc%2Fapp%2Fadmin%2Fusers%2Fuser-add%2Fuser-add.component.ts;h=b79437795e9b2edc5be1f6d401f9f4d69aed0766;hb=4b2f33f3c6d109365090b08244d7f99ad4e69025;hp=b7efd3a8004c68a794c7d538a5f2ba3412747af8;hpb=0f6da32b148c0f4146b2ae9ad1add9a9f00cc339;p=github%2FChocobozzz%2FPeerTube.git diff --git a/client/src/app/admin/users/user-add/user-add.component.ts b/client/src/app/admin/users/user-add/user-add.component.ts index b7efd3a80..b79437795 100644 --- a/client/src/app/admin/users/user-add/user-add.component.ts +++ b/client/src/app/admin/users/user-add/user-add.component.ts @@ -1,37 +1,57 @@ -import { Validators } from '@angular/common'; import { Component, OnInit } from '@angular/core'; -import { FormGroup, FormControl, REACTIVE_FORM_DIRECTIVES } from '@angular/forms'; +import { FormBuilder, FormGroup } from '@angular/forms'; import { Router } from '@angular/router'; import { UserService } from '../shared'; +import { FormReactive, USER_USERNAME, USER_PASSWORD } from '../../../shared'; @Component({ selector: 'my-user-add', - template: require('./user-add.component.html'), - directives: [ REACTIVE_FORM_DIRECTIVES ] + template: require('./user-add.component.html') }) -export class UserAddComponent implements OnInit { - userAddForm: FormGroup; +export class UserAddComponent extends FormReactive implements OnInit { error: string = null; - username = ''; - password = ''; - constructor(private router: Router, private userService: UserService) {} + form: FormGroup; + formErrors = { + 'username': '', + 'password': '' + }; + validationMessages = { + 'username': USER_USERNAME.MESSAGES, + 'password': USER_PASSWORD.MESSAGES, + }; - ngOnInit() { - this.userAddForm = new FormGroup({ - username: new FormControl('', [ Validators.required, Validators.minLength(3), Validators.maxLength(20) ]), - password: new FormControl('', [ Validators.required, Validators.minLength(6) ]), + constructor( + private formBuilder: FormBuilder, + private router: Router, + private userService: UserService + ) { + super(); + } + + buildForm() { + this.form = this.formBuilder.group({ + username: [ '', USER_USERNAME.VALIDATORS ], + password: [ '', USER_PASSWORD.VALIDATORS ], }); + + this.form.valueChanges.subscribe(data => this.onValueChanged(data)); + } + + ngOnInit() { + this.buildForm(); } addUser() { this.error = null; - this.userService.addUser(this.username, this.password).subscribe( + const { username, password } = this.form.value; + + this.userService.addUser(username, password).subscribe( ok => this.router.navigate([ '/admin/users/list' ]), - err => this.error = err + err => this.error = err.text ); } }