7be7aabc213ff4ce4ff595ce0676bd9dc958993b
[oweals/peertube.git] /
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
16   constructor (
17     protected formValidatorService: FormValidatorService,
18     private userValidatorsService: UserValidatorsService,
19     private notificationsService: NotificationsService,
20     private userService: UserService,
21     private i18n: I18n
22   ) {
23     super()
24   }
25
26   ngOnInit () {
27     this.buildForm({
28       'new-password': this.userValidatorsService.USER_PASSWORD,
29       'new-confirmed-password': this.userValidatorsService.USER_PASSWORD
30     })
31   }
32
33   changePassword () {
34     const newPassword = this.form.value['new-password']
35     const newConfirmedPassword = this.form.value['new-confirmed-password']
36
37     this.error = null
38
39     if (newPassword !== newConfirmedPassword) {
40       this.error = this.i18n('The new password and the confirmed password do not correspond.')
41       return
42     }
43
44     this.userService.changePassword(newPassword).subscribe(
45       () => this.notificationsService.success(this.i18n('Success'), this.i18n('Password updated.')),
46
47       err => this.error = err.message
48     )
49   }
50 }