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/account/account.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/account/account.component.ts')
-rw-r--r-- | client/src/app/account/account.component.ts | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/client/src/app/account/account.component.ts b/client/src/app/account/account.component.ts index a22738d3f..b503406c9 100644 --- a/client/src/app/account/account.component.ts +++ b/client/src/app/account/account.component.ts | |||
@@ -1,44 +1,64 @@ | |||
1 | import { } from '@angular/common'; | 1 | import { } from '@angular/common'; |
2 | import { Component, OnInit } from '@angular/core'; | 2 | import { Component, OnInit } from '@angular/core'; |
3 | import { FormControl, FormGroup, Validators } from '@angular/forms'; | 3 | import { FormBuilder, FormGroup } from '@angular/forms'; |
4 | import { Router } from '@angular/router'; | 4 | import { Router } from '@angular/router'; |
5 | 5 | ||
6 | import { AccountService } from './account.service'; | 6 | import { AccountService } from './account.service'; |
7 | import { FormReactive, USER_PASSWORD } from '../shared'; | ||
7 | 8 | ||
8 | @Component({ | 9 | @Component({ |
9 | selector: 'my-account', | 10 | selector: 'my-account', |
10 | template: require('./account.component.html') | 11 | template: require('./account.component.html') |
11 | }) | 12 | }) |
12 | 13 | ||
13 | export class AccountComponent implements OnInit { | 14 | export class AccountComponent extends FormReactive implements OnInit { |
14 | newPassword = ''; | ||
15 | newConfirmedPassword = ''; | ||
16 | changePasswordForm: FormGroup; | ||
17 | information: string = null; | 15 | information: string = null; |
18 | error: string = null; | 16 | error: string = null; |
19 | 17 | ||
18 | form: FormGroup; | ||
19 | formErrors = { | ||
20 | 'new-password': '', | ||
21 | 'new-confirmed-password': '' | ||
22 | }; | ||
23 | validationMessages = { | ||
24 | 'new-password': USER_PASSWORD.MESSAGES, | ||
25 | 'new-confirmed-password': USER_PASSWORD.MESSAGES | ||
26 | }; | ||
27 | |||
20 | constructor( | 28 | constructor( |
21 | private accountService: AccountService, | 29 | private accountService: AccountService, |
30 | private formBuilder: FormBuilder, | ||
22 | private router: Router | 31 | private router: Router |
23 | ) {} | 32 | ) { |
33 | super(); | ||
34 | } | ||
24 | 35 | ||
25 | ngOnInit() { | 36 | buildForm() { |
26 | this.changePasswordForm = new FormGroup({ | 37 | this.form = this.formBuilder.group({ |
27 | 'new-password': new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]), | 38 | 'new-password': [ '', USER_PASSWORD.VALIDATORS ], |
28 | 'new-confirmed-password': new FormControl('', [ <any>Validators.required, <any>Validators.minLength(6) ]), | 39 | 'new-confirmed-password': [ '', USER_PASSWORD.VALIDATORS ], |
29 | }); | 40 | }); |
41 | |||
42 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)); | ||
43 | } | ||
44 | |||
45 | ngOnInit() { | ||
46 | this.buildForm(); | ||
30 | } | 47 | } |
31 | 48 | ||
32 | changePassword() { | 49 | changePassword() { |
50 | const newPassword = this.form.value['new-password']; | ||
51 | const newConfirmedPassword = this.form.value['new-confirmed-password']; | ||
52 | |||
33 | this.information = null; | 53 | this.information = null; |
34 | this.error = null; | 54 | this.error = null; |
35 | 55 | ||
36 | if (this.newPassword !== this.newConfirmedPassword) { | 56 | if (newPassword !== newConfirmedPassword) { |
37 | this.error = 'The new password and the confirmed password do not correspond.'; | 57 | this.error = 'The new password and the confirmed password do not correspond.'; |
38 | return; | 58 | return; |
39 | } | 59 | } |
40 | 60 | ||
41 | this.accountService.changePassword(this.newPassword).subscribe( | 61 | this.accountService.changePassword(newPassword).subscribe( |
42 | ok => this.information = 'Password updated.', | 62 | ok => this.information = 'Password updated.', |
43 | 63 | ||
44 | err => this.error = err | 64 | err => this.error = err |