]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { filter } from 'rxjs/operators' |
df98563e | 2 | import { Component, OnInit } from '@angular/core' |
67ed6552 C |
3 | import { AuthService, Notifier, UserService } from '@app/core' |
4 | import { FormReactive, FormValidatorService, UserValidatorsService } from '@app/shared/shared-forms' | |
b1d40cff | 5 | import { I18n } from '@ngx-translate/i18n-polyfill' |
67ed6552 | 6 | import { User } from '@shared/models' |
af5e743b C |
7 | |
8 | @Component({ | |
9 | selector: 'my-account-change-password', | |
4bb6886d C |
10 | templateUrl: './my-account-change-password.component.html', |
11 | styleUrls: [ './my-account-change-password.component.scss' ] | |
af5e743b | 12 | }) |
4bb6886d | 13 | export class MyAccountChangePasswordComponent extends FormReactive implements OnInit { |
df98563e | 14 | error: string = null |
3805ce3f | 15 | user: User = null |
af5e743b | 16 | |
df98563e | 17 | constructor ( |
d18d6478 | 18 | protected formValidatorService: FormValidatorService, |
e309822b | 19 | private userValidatorsService: UserValidatorsService, |
f8b2c1b4 | 20 | private notifier: Notifier, |
3805ce3f | 21 | private authService: AuthService, |
b1d40cff C |
22 | private userService: UserService, |
23 | private i18n: I18n | |
af5e743b | 24 | ) { |
df98563e | 25 | super() |
af5e743b C |
26 | } |
27 | ||
df98563e | 28 | ngOnInit () { |
d18d6478 | 29 | this.buildForm({ |
a890d1e0 | 30 | 'current-password': this.userValidatorsService.USER_PASSWORD, |
e309822b | 31 | 'new-password': this.userValidatorsService.USER_PASSWORD, |
b0ee41df | 32 | 'new-confirmed-password': this.userValidatorsService.USER_CONFIRM_PASSWORD |
d18d6478 | 33 | }) |
af5e743b | 34 | |
3805ce3f B |
35 | this.user = this.authService.getUser() |
36 | ||
b0ee41df | 37 | const confirmPasswordControl = this.form.get('new-confirmed-password') |
af5e743b | 38 | |
b0ee41df C |
39 | confirmPasswordControl.valueChanges |
40 | .pipe(filter(v => v !== this.form.value[ 'new-password' ])) | |
41 | .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true })) | |
a94419a6 J |
42 | } |
43 | ||
a890d1e0 C |
44 | changePassword () { |
45 | const currentPassword = this.form.value[ 'current-password' ] | |
46 | const newPassword = this.form.value[ 'new-password' ] | |
3805ce3f | 47 | |
a890d1e0 | 48 | this.userService.changePassword(currentPassword, newPassword).subscribe( |
b0ee41df | 49 | () => { |
f8b2c1b4 | 50 | this.notifier.success(this.i18n('Password updated.')) |
af5e743b | 51 | |
b0ee41df | 52 | this.form.reset() |
a890d1e0 | 53 | this.error = null |
b0ee41df | 54 | }, |
af5e743b | 55 | |
a890d1e0 C |
56 | err => { |
57 | if (err.status === 401) { | |
58 | this.error = this.i18n('You current password is invalid.') | |
59 | return | |
60 | } | |
61 | ||
62 | this.error = err.message | |
63 | } | |
df98563e | 64 | ) |
af5e743b C |
65 | } |
66 | } |