]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+my-account/my-account-settings/my-account-change-password/my-account-change-password.component.ts
57a706b0f090c4dc25f7a7c5022b624d000a632b
[github/Chocobozzz/PeerTube.git] / client / src / app / +my-account / my-account-settings / my-account-change-password / my-account-change-password.component.ts
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 import { filter } from 'rxjs/operators'
8
9 @Component({
10 selector: 'my-account-change-password',
11 templateUrl: './my-account-change-password.component.html',
12 styleUrls: [ './my-account-change-password.component.scss' ]
13 })
14 export class MyAccountChangePasswordComponent extends FormReactive implements OnInit {
15 error: string = null
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_CONFIRM_PASSWORD
31 })
32
33 const confirmPasswordControl = this.form.get('new-confirmed-password')
34
35 confirmPasswordControl.valueChanges
36 .pipe(filter(v => v !== this.form.value[ 'new-password' ]))
37 .subscribe(() => confirmPasswordControl.setErrors({ matchPassword: true }))
38 }
39
40 changePassword () {
41 this.userService.changePassword(this.form.value[ 'new-password' ]).subscribe(
42 () => {
43 this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.'))
44
45 this.form.reset()
46 },
47
48 err => this.error = err.message
49 )
50 }
51 }