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