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 | |
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')
-rw-r--r-- | client/src/app/login/login.component.html | 20 | ||||
-rw-r--r-- | client/src/app/login/login.component.ts | 53 |
2 files changed, 47 insertions, 26 deletions
diff --git a/client/src/app/login/login.component.html b/client/src/app/login/login.component.html index 636872942..94a405405 100644 --- a/client/src/app/login/login.component.html +++ b/client/src/app/login/login.component.html | |||
@@ -2,28 +2,28 @@ | |||
2 | 2 | ||
3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> | 3 | <div *ngIf="error" class="alert alert-danger">{{ error }}</div> |
4 | 4 | ||
5 | <form role="form" (ngSubmit)="login()" [formGroup]="loginForm"> | 5 | <form role="form" (ngSubmit)="login()" [formGroup]="form"> |
6 | <div class="form-group"> | 6 | <div class="form-group"> |
7 | <label for="username">Username</label> | 7 | <label for="username">Username</label> |
8 | <input | 8 | <input |
9 | type="text" class="form-control" name="username" id="username" placeholder="Username" | 9 | type="text" class="form-control" id="username" placeholder="Username" required |
10 | [(ngModel)]="username" | 10 | formControlName="username" |
11 | > | 11 | > |
12 | <div [hidden]="loginForm.controls.username.valid || loginForm.controls.username.pristine" class="alert alert-danger"> | 12 | <div *ngIf="formErrors.username" class="alert alert-danger"> |
13 | Username is required | 13 | {{ formErrors.username }} |
14 | </div> | 14 | </div> |
15 | </div> | 15 | </div> |
16 | 16 | ||
17 | <div class="form-group"> | 17 | <div class="form-group"> |
18 | <label for="password">Password</label> | 18 | <label for="password">Password</label> |
19 | <input | 19 | <input |
20 | type="password" class="form-control" name="password" id="password" placeholder="Password" | 20 | type="password" class="form-control" name="password" id="password" placeholder="Password" required |
21 | [(ngModel)]="password" | 21 | formControlName="password" |
22 | > | 22 | > |
23 | <div [hidden]="loginForm.controls.password.valid || loginForm.controls.password.pristine" class="alert alert-danger"> | 23 | <div *ngIf="formErrors.password" class="alert alert-danger"> |
24 | Password is required | 24 | {{ formErrors.password }} |
25 | </div> | 25 | </div> |
26 | </div> | 26 | </div> |
27 | 27 | ||
28 | <input type="submit" value="Login" class="btn btn-default" [disabled]="!loginForm.valid"> | 28 | <input type="submit" value="Login" class="btn btn-default" [disabled]="!form.valid"> |
29 | </form> | 29 | </form> |
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 | ||