1 import { Component, OnDestroy, OnInit, Input } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import * as generator from 'generate-password-browser'
4 import { NotificationsService } from 'angular2-notifications'
5 import { UserService } from '@app/shared/users/user.service'
6 import { ServerService } from '../../../core'
7 import { User, UserUpdate } from '../../../../../../shared'
8 import { I18n } from '@ngx-translate/i18n-polyfill'
9 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
10 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
11 import { ConfigService } from '@app/+admin/config/shared/config.service'
12 import { FormReactive } from '../../../shared'
15 selector: 'my-user-password',
16 templateUrl: './user-password.component.html',
17 styleUrls: [ './user-password.component.scss' ]
19 export class UserPasswordComponent extends FormReactive implements OnInit, OnDestroy {
24 @Input() userId: number
27 protected formValidatorService: FormValidatorService,
28 protected serverService: ServerService,
29 protected configService: ConfigService,
30 private userValidatorsService: UserValidatorsService,
31 private route: ActivatedRoute,
32 private router: Router,
33 private notificationsService: NotificationsService,
34 private userService: UserService,
42 password: this.userValidatorsService.USER_PASSWORD
51 this.error = undefined
53 const userUpdate: UserUpdate = this.form.value
55 this.userService.updateUser(this.userId, userUpdate).subscribe(
57 this.notificationsService.success(
59 this.i18n('Password changed for user {{username}}.', { username: this.username })
63 err => this.error = err.message
68 this.form.patchValue({
69 password: generator.generate({
71 excludeSimilarCharacters: true,
77 togglePasswordVisibility () {
78 this.showPassword = !this.showPassword
81 getFormButtonTitle () {
82 return this.i18n('Update user password')
85 private onUserFetched (userJson: User) {
86 this.userId = userJson.id
87 this.username = userJson.username