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