]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core'; | |
2 | import { FormBuilder, FormGroup } from '@angular/forms'; | |
3 | import { Router } from '@angular/router'; | |
4 | ||
5 | import { UserService } from '../shared'; | |
6 | import { FormReactive, USER_USERNAME, USER_PASSWORD } from '../../../shared'; | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-user-add', | |
10 | templateUrl: './user-add.component.html' | |
11 | }) | |
12 | export class UserAddComponent extends FormReactive implements OnInit { | |
13 | error: string = null; | |
14 | ||
15 | form: FormGroup; | |
16 | formErrors = { | |
17 | 'username': '', | |
18 | 'password': '' | |
19 | }; | |
20 | validationMessages = { | |
21 | 'username': USER_USERNAME.MESSAGES, | |
22 | 'password': USER_PASSWORD.MESSAGES, | |
23 | }; | |
24 | ||
25 | constructor( | |
26 | private formBuilder: FormBuilder, | |
27 | private router: Router, | |
28 | private userService: UserService | |
29 | ) { | |
30 | super(); | |
31 | } | |
32 | ||
33 | buildForm() { | |
34 | this.form = this.formBuilder.group({ | |
35 | username: [ '', USER_USERNAME.VALIDATORS ], | |
36 | password: [ '', USER_PASSWORD.VALIDATORS ], | |
37 | }); | |
38 | ||
39 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | |
40 | } | |
41 | ||
42 | ngOnInit() { | |
43 | this.buildForm(); | |
44 | } | |
45 | ||
46 | addUser() { | |
47 | this.error = null; | |
48 | ||
49 | const { username, password } = this.form.value; | |
50 | ||
51 | this.userService.addUser(username, password).subscribe( | |
52 | ok => this.router.navigate([ '/admin/users/list' ]), | |
53 | ||
54 | err => this.error = err.text | |
55 | ); | |
56 | } | |
57 | } |