aboutsummaryrefslogblamecommitdiffhomepage
path: root/client/src/app/+admin/users/user-edit/user-password.component.ts
blob: 05d52b17f0679db2853deda66db76122d7a7c09a (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
                                                        
                                                 

                                                                                     
                                           





                                                 
                                                                           
               


                      

                         

                                                         
                               

                                    




                    
                                       
      

   






                                                                   
                                                                                     





                                     




                                          
                                          
   
 
import { Component, Input, OnInit } from '@angular/core'
import { Notifier, UserService } from '@app/core'
import { USER_PASSWORD_VALIDATOR } from '@app/shared/form-validators/user-validators'
import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
import { UserUpdate } from '@shared/models'

@Component({
  selector: 'my-user-password',
  templateUrl: './user-password.component.html',
  styleUrls: [ './user-password.component.scss' ]
})
export class UserPasswordComponent extends FormReactive implements OnInit {
  error: string
  username: string
  showPassword = false

  @Input() userId: number

  constructor (
    protected formValidatorService: FormValidatorService,
    private notifier: Notifier,
    private userService: UserService
    ) {
    super()
  }

  ngOnInit () {
    this.buildForm({
      password: USER_PASSWORD_VALIDATOR
    })
  }

  formValidated () {
    this.error = undefined

    const userUpdate: UserUpdate = this.form.value

    this.userService.updateUser(this.userId, userUpdate).subscribe(
      () => {
        this.notifier.success($localize`Password changed for user ${this.username}.`)
      },

      err => this.error = err.message
    )
  }

  togglePasswordVisibility () {
    this.showPassword = !this.showPassword
  }

  getFormButtonTitle () {
    return $localize`Update user password`
  }
}