diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2016-09-09 22:16:51 +0200 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2016-09-09 22:16:51 +0200 |
commit | 4b2f33f3c6d109365090b08244d7f99ad4e69025 (patch) | |
tree | 700d3e8e14efc4172f754d75c041ec507100e897 /client/src/app/login/login.component.ts | |
parent | ab32b0fc805b92c5a1d7ac5901cb1a38e94622ca (diff) | |
download | PeerTube-4b2f33f3c6d109365090b08244d7f99ad4e69025.tar.gz PeerTube-4b2f33f3c6d109365090b08244d7f99ad4e69025.tar.zst PeerTube-4b2f33f3c6d109365090b08244d7f99ad4e69025.zip |
Client: reactive forms
Diffstat (limited to 'client/src/app/login/login.component.ts')
-rw-r--r-- | client/src/app/login/login.component.ts | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/client/src/app/login/login.component.ts b/client/src/app/login/login.component.ts index 7a4e15c2c..378714ca1 100644 --- a/client/src/app/login/login.component.ts +++ b/client/src/app/login/login.component.ts | |||
@@ -1,39 +1,60 @@ | |||
1 | import { Component, OnInit } from '@angular/core'; | 1 | import { Component, OnInit } from '@angular/core'; |
2 | import { FormControl, FormGroup, Validators } from '@angular/forms'; | 2 | import { FormBuilder, FormGroup, Validators } from '@angular/forms'; |
3 | import { Router } from '@angular/router'; | 3 | import { Router } from '@angular/router'; |
4 | 4 | ||
5 | import { AuthService } from '../shared'; | 5 | import { AuthService, FormReactive } from '../shared'; |
6 | 6 | ||
7 | @Component({ | 7 | @Component({ |
8 | selector: 'my-login', | 8 | selector: 'my-login', |
9 | template: require('./login.component.html') | 9 | template: require('./login.component.html') |
10 | }) | 10 | }) |
11 | 11 | ||
12 | export class LoginComponent implements OnInit { | 12 | export class LoginComponent extends FormReactive implements OnInit { |
13 | error: string = null; | 13 | error: string = null; |
14 | username = ''; | 14 | |
15 | password: ''; | 15 | form: FormGroup; |
16 | loginForm: FormGroup; | 16 | formErrors = { |
17 | 'username': '', | ||
18 | 'password': '' | ||
19 | }; | ||
20 | validationMessages = { | ||
21 | 'username': { | ||
22 | 'required': 'Username is required.', | ||
23 | }, | ||
24 | 'password': { | ||
25 | 'required': 'Password is required.' | ||
26 | } | ||
27 | }; | ||
17 | 28 | ||
18 | constructor( | 29 | constructor( |
19 | private authService: AuthService, | 30 | private authService: AuthService, |
31 | private formBuilder: FormBuilder, | ||
20 | private router: Router | 32 | private router: Router |
21 | ) {} | 33 | ) { |
34 | super(); | ||
35 | } | ||
22 | 36 | ||
23 | ngOnInit() { | 37 | buildForm() { |
24 | this.loginForm = new FormGroup({ | 38 | this.form = this.formBuilder.group({ |
25 | username: new FormControl('', [ <any>Validators.required ]), | 39 | username: [ '', Validators.required ], |
26 | password: new FormControl('', [ <any>Validators.required ]), | 40 | password: [ '', Validators.required ], |
27 | }); | 41 | }); |
42 | |||
43 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | ||
44 | } | ||
45 | |||
46 | ngOnInit() { | ||
47 | this.buildForm(); | ||
28 | } | 48 | } |
29 | 49 | ||
30 | login() { | 50 | login() { |
31 | this.authService.login(this.username, this.password).subscribe( | 51 | this.error = null; |
32 | result => { | 52 | |
33 | this.error = null; | 53 | const { username, password } = this.form.value; |
54 | |||
55 | this.authService.login(username, password).subscribe( | ||
56 | result => this.router.navigate(['/videos/list']), | ||
34 | 57 | ||
35 | this.router.navigate(['/videos/list']); | ||
36 | }, | ||
37 | error => { | 58 | error => { |
38 | console.error(error.json); | 59 | console.error(error.json); |
39 | 60 | ||