github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/user/user-form.component.ts (about)

     1  import { Component, Input, OnInit }     from '@angular/core';
     2  import { Router }                       from '@angular/router';
     3  import {
     4      FormGroup,
     5      FormControl,
     6      Validators,
     7      FormBuilder }                       from '@angular/forms';
     8  
     9  import { User }                         from './user';
    10  import { UserService }                  from './user.service';
    11  
    12  @Component({
    13      moduleId: module.id,
    14      selector: 'lcp-user-form',
    15      templateUrl: 'user-form.component.html'
    16  })
    17  
    18  export class UserFormComponent implements OnInit {
    19      @Input()
    20      user: User;
    21  
    22      edit: boolean = false;
    23      submitButtonLabel: string = "Add";
    24      form: FormGroup;
    25  
    26      private submitted = false;
    27  
    28      constructor(
    29          private fb: FormBuilder,
    30          private router: Router,
    31          private userService: UserService) {
    32      }
    33  
    34      ngOnInit(): void {
    35          if (this.user == null) {
    36              this.user = new User();
    37              this.submitButtonLabel = "Add";
    38              this.form = this.fb.group({
    39                  "name": ["", Validators.required],
    40                  "email": ["", Validators.required],
    41                  "password": ["", Validators.required],
    42                  "hint": ["", Validators.required]
    43              });
    44          } else {
    45              this.edit = true;
    46              this.submitButtonLabel = "Save";
    47              this.form = this.fb.group({
    48                  "name": [this.user.name, Validators.required],
    49                  "email": [this.user.email, Validators.required],
    50                  "password": "",
    51                  "hint": [this.user.hint, Validators.required]
    52              });
    53          }
    54      }
    55  
    56      gotoList() {
    57          this.router.navigate(['/users']);
    58      }
    59  
    60      onCancel() {
    61          this.gotoList();
    62      }
    63  
    64      onSubmit() {
    65          this.bindForm();
    66  
    67          if (this.edit) {
    68              this.userService.update(
    69                  this.user
    70              ).then(
    71                  user => {
    72                      this.gotoList();
    73                  }
    74              );
    75          } else {
    76              this.userService.add(this.user).then(
    77                  user => {
    78                      this.gotoList();
    79                  }
    80              );
    81          }
    82  
    83          this.submitted = true;
    84      }
    85  
    86      // Bind form to user
    87      bindForm(): void {
    88          this.user.name = this.form.value['name'];
    89          this.user.email = this.form.value['email'];
    90          this.user.hint = this.form.value['hint'];
    91  
    92          let newPassword: string = this.form.value['password'];
    93          newPassword = newPassword.trim();
    94  
    95          if (newPassword.length > 0) {
    96              this.user.clearPassword = newPassword;
    97          }
    98      }
    99  }