1 import { Component, OnDestroy, OnInit, Input } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { Subscription } from 'rxjs'
4 import * as generator from 'generate-password-browser'
5 import { NotificationsService } from 'angular2-notifications'
6 import { UserService } from '@app/shared/users/user.service'
7 import { ServerService } from '../../../core'
8 import { User, UserUpdate } from '../../../../../../shared'
9 import { I18n } from '@ngx-translate/i18n-polyfill'
10 import { FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
11 import { UserValidatorsService } from '@app/shared/forms/form-validators/user-validators.service'
12 import { ConfigService } from '@app/+admin/config/shared/config.service'
13 import { FormReactive } from '../../../shared'
16 selector: 'my-user-password',
17 templateUrl: './user-password.component.html',
18 styleUrls: [ './user-password.component.scss' ]
20 export class UserPasswordComponent extends FormReactive implements OnInit, OnDestroy {
25 @Input() userId: number
27 private paramsSub: Subscription
30 protected formValidatorService: FormValidatorService,
31 protected serverService: ServerService,
32 protected configService: ConfigService,
33 private userValidatorsService: UserValidatorsService,
34 private route: ActivatedRoute,
35 private router: Router,
36 private notificationsService: NotificationsService,
37 private userService: UserService,
45 password: this.userValidatorsService.USER_PASSWORD
50 this.paramsSub.unsubscribe()
54 this.error = undefined
56 const userUpdate: UserUpdate = this.form.value
58 this.userService.updateUser(this.userId, userUpdate).subscribe(
60 this.notificationsService.success(
62 this.i18n('Password changed for user {{username}}.', { username: this.username })
66 err => this.error = err.message
71 this.form.patchValue({
72 password: generator.generate({
74 excludeSimilarCharacters: true,
80 togglePasswordVisibility () {
81 this.showPassword = !this.showPassword
84 getFormButtonTitle () {
85 return this.i18n('Update user password')
88 private onUserFetched (userJson: User) {
89 this.userId = userJson.id
90 this.username = userJson.username