]>
Commit | Line | Data |
---|---|---|
7da18e44 | 1 | import { Component, OnInit } from '@angular/core'; |
4b2f33f3 | 2 | import { FormBuilder, FormGroup } from '@angular/forms'; |
7da18e44 C |
3 | import { Router } from '@angular/router'; |
4 | ||
7ddd02c9 C |
5 | import { NotificationsService } from 'angular2-notifications'; |
6 | ||
7da18e44 | 7 | import { UserService } from '../shared'; |
ad4a8a1c C |
8 | import { |
9 | FormReactive, | |
10 | USER_USERNAME, | |
11 | USER_EMAIL, | |
12 | USER_PASSWORD | |
13 | } from '../../../shared'; | |
7da18e44 C |
14 | |
15 | @Component({ | |
16 | selector: 'my-user-add', | |
ec8d8440 | 17 | templateUrl: './user-add.component.html' |
7da18e44 | 18 | }) |
4b2f33f3 | 19 | export class UserAddComponent extends FormReactive implements OnInit { |
7da18e44 C |
20 | error: string = null; |
21 | ||
4b2f33f3 C |
22 | form: FormGroup; |
23 | formErrors = { | |
24 | 'username': '', | |
ad4a8a1c | 25 | 'email': '', |
4b2f33f3 C |
26 | 'password': '' |
27 | }; | |
28 | validationMessages = { | |
29 | 'username': USER_USERNAME.MESSAGES, | |
ad4a8a1c | 30 | 'email': USER_EMAIL.MESSAGES, |
4b2f33f3 C |
31 | 'password': USER_PASSWORD.MESSAGES, |
32 | }; | |
7da18e44 | 33 | |
4b2f33f3 C |
34 | constructor( |
35 | private formBuilder: FormBuilder, | |
36 | private router: Router, | |
7ddd02c9 | 37 | private notificationsService: NotificationsService, |
4b2f33f3 C |
38 | private userService: UserService |
39 | ) { | |
40 | super(); | |
41 | } | |
42 | ||
43 | buildForm() { | |
44 | this.form = this.formBuilder.group({ | |
45 | username: [ '', USER_USERNAME.VALIDATORS ], | |
ad4a8a1c | 46 | email: [ '', USER_EMAIL.VALIDATORS ], |
4b2f33f3 | 47 | password: [ '', USER_PASSWORD.VALIDATORS ], |
7da18e44 | 48 | }); |
4b2f33f3 C |
49 | |
50 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | |
51 | } | |
52 | ||
53 | ngOnInit() { | |
54 | this.buildForm(); | |
7da18e44 C |
55 | } |
56 | ||
0f6da32b | 57 | addUser() { |
7da18e44 C |
58 | this.error = null; |
59 | ||
ad4a8a1c | 60 | const { username, password, email } = this.form.value; |
4b2f33f3 | 61 | |
ad4a8a1c | 62 | this.userService.addUser(username, password, email).subscribe( |
7ddd02c9 C |
63 | () => { |
64 | this.notificationsService.success('Success', `User ${username} created.`); | |
65 | this.router.navigate([ '/admin/users/list' ]); | |
66 | }, | |
7da18e44 | 67 | |
bf68dd75 | 68 | err => this.error = err.text |
7da18e44 C |
69 | ); |
70 | } | |
71 | } |