]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { FormBuilder, FormGroup } from '@angular/forms' | |
3 | import { NotificationsService } from 'angular2-notifications' | |
4 | import { FormReactive, USER_PASSWORD, UserService } from '../../../shared' | |
5 | ||
6 | @Component({ | |
7 | selector: 'my-account-change-password', | |
8 | templateUrl: './account-change-password.component.html', | |
9 | styleUrls: [ './account-change-password.component.scss' ] | |
10 | }) | |
11 | export class AccountChangePasswordComponent extends FormReactive implements OnInit { | |
12 | error: string = null | |
13 | ||
14 | form: FormGroup | |
15 | formErrors = { | |
16 | 'new-password': '', | |
17 | 'new-confirmed-password': '' | |
18 | } | |
19 | validationMessages = { | |
20 | 'new-password': USER_PASSWORD.MESSAGES, | |
21 | 'new-confirmed-password': USER_PASSWORD.MESSAGES | |
22 | } | |
23 | ||
24 | constructor ( | |
25 | private formBuilder: FormBuilder, | |
26 | private notificationsService: NotificationsService, | |
27 | private userService: UserService | |
28 | ) { | |
29 | super() | |
30 | } | |
31 | ||
32 | buildForm () { | |
33 | this.form = this.formBuilder.group({ | |
34 | 'new-password': [ '', USER_PASSWORD.VALIDATORS ], | |
35 | 'new-confirmed-password': [ '', USER_PASSWORD.VALIDATORS ] | |
36 | }) | |
37 | ||
38 | this.form.valueChanges.subscribe(data => this.onValueChanged(data)) | |
39 | } | |
40 | ||
41 | ngOnInit () { | |
42 | this.buildForm() | |
43 | } | |
44 | ||
45 | changePassword () { | |
46 | const newPassword = this.form.value['new-password'] | |
47 | const newConfirmedPassword = this.form.value['new-confirmed-password'] | |
48 | ||
49 | this.error = null | |
50 | ||
51 | if (newPassword !== newConfirmedPassword) { | |
52 | this.error = 'The new password and the confirmed password do not correspond.' | |
53 | return | |
54 | } | |
55 | ||
56 | this.userService.changePassword(newPassword).subscribe( | |
57 | () => this.notificationsService.success('Success', 'Password updated.'), | |
58 | ||
59 | err => this.error = err.message | |
60 | ) | |
61 | } | |
62 | } |