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