]>
Commit | Line | Data |
---|---|---|
1 | import { Component, OnInit } from '@angular/core' | |
2 | import { NotificationsService } from 'angular2-notifications' | |
3 | import { FormReactive, UserService } from '../../../shared' | |
4 | import { I18n } from '@ngx-translate/i18n-polyfill' | |
5 | import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service' | |
6 | import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service' | |
7 | ||
8 | @Component({ | |
9 | selector: 'my-account-change-password', | |
10 | templateUrl: './my-account-change-password.component.html', | |
11 | styleUrls: [ './my-account-change-password.component.scss' ] | |
12 | }) | |
13 | export class MyAccountChangePasswordComponent extends FormReactive implements OnInit { | |
14 | error: string = null | |
15 | unsendable = true // default to true to not have to not the if in change password | |
16 | ||
17 | constructor ( | |
18 | protected formValidatorService: FormValidatorService, | |
19 | private userValidatorsService: UserValidatorsService, | |
20 | private notificationsService: NotificationsService, | |
21 | private userService: UserService, | |
22 | private i18n: I18n | |
23 | ) { | |
24 | super() | |
25 | } | |
26 | ||
27 | ngOnInit () { | |
28 | this.buildForm({ | |
29 | 'new-password': this.userValidatorsService.USER_PASSWORD, | |
30 | 'new-confirmed-password': this.userValidatorsService.USER_PASSWORD | |
31 | }) | |
32 | } | |
33 | ||
34 | validateNewPassword () { | |
35 | if (this.form.value['new-password'] && this.form.value['new-confirmed-password']) { | |
36 | if (this.form.value['new-password'] === this.form.value['new-confirmed-password']) { | |
37 | this.error = null | |
38 | this.unsendable = false | |
39 | return | |
40 | } | |
41 | } | |
42 | this.unsendable = true | |
43 | } | |
44 | ||
45 | printAnError () { | |
46 | console.log(this.unsendable) | |
47 | this.validateNewPassword() | |
48 | if (this.unsendable) { | |
49 | this.error = this.i18n('The new password and the confirmed password do not correspond.') | |
50 | } | |
51 | } | |
52 | ||
53 | changePassword () { | |
54 | if (this.unsendable) { | |
55 | return | |
56 | } | |
57 | ||
58 | this.userService.changePassword(this.form.value['new-password']).subscribe( | |
59 | () => this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.')), | |
60 | ||
61 | err => this.error = err.message | |
62 | ) | |
63 | } | |
64 | } |