Advertisement
Guest User

Untitled

a guest
Jan 20th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. import { Component, OnInit, Inject } from '@angular/core';
  2. import { FormGroup, FormControl, Validators, ValidatorFn, ValidationErrors } from '@angular/forms';
  3. import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material';
  4. import { map } from 'rxjs/operators';
  5. import { of } from 'rxjs';
  6. // import { ApiService } from 'src/app/shared/services/api.service';
  7. import { AdminUserService } from '../admin-user.service';
  8.  
  9. @Component({
  10. selector: 'app-create-update-user',
  11. templateUrl: './create-update-user.component.html',
  12. styleUrls: ['./create-update-user.component.scss']
  13. })
  14. export class CreateUpdateUserComponent implements OnInit {
  15. public userForm = new FormGroup({
  16. username: new FormControl(
  17. this.data ? this.data.username : '',
  18. [Validators.required],
  19. this.getUniqueValidator('username', 'checkUsername')),
  20. // this.getUniqueValidator('username', 'getEntity', 'checkUserName')),
  21. password: new FormControl('', this.data ? undefined : [Validators.required]),
  22. password_confirm: new FormControl('', this.data ? undefined : [Validators.required]),
  23. email: new FormControl(
  24. this.data ? this.data.email : '',
  25. [Validators.required, Validators.email],
  26. this.getUniqueValidator('email', 'checkUserEmail')),
  27. // this.getUniqueValidator('email', 'getEntity', 'checkEmailAddress')),
  28. });
  29. hide = true;
  30. constructor(
  31. @Inject(MAT_DIALOG_DATA) public data: any,
  32. public dialogRef: MatDialogRef<CreateUpdateUserComponent>,
  33. private apiService: AdminUserService,
  34. ) { }
  35.  
  36. ngOnInit() {
  37. this.userForm.setValidators(this.comparisonValidator())
  38. }
  39. addAdminHandler() {
  40. this.dialogRef.close({ ...this.userForm.value, id: this.data ? this.data.id : null });
  41. }
  42.  
  43. cancelCreateAdminHandler() {
  44. this.dialogRef.close();
  45. }
  46.  
  47. comparisonValidator(): ValidatorFn {
  48. return (group: FormGroup): ValidationErrors => {
  49. const control1 = group.get('password');
  50. const control2 = group.get('password_confirm');
  51. if (control1.value !== control2.value) {
  52. control2.setErrors({ notEquivalent: true });
  53. } else {
  54. control2.setErrors(null);
  55. }
  56. return;
  57. };
  58. }
  59.  
  60. getUniqueValidator(prop, method) {
  61. // getUniqueValidator(prop,action, method) {
  62. return (control: FormControl) => {
  63. if (this.data && this.data[prop] === control.value) {
  64. return of(null);
  65. }
  66. return this.apiService[method](control.value)
  67. // return this.apiService[method]('AdminUser', action, control.value)
  68. .pipe(
  69. map((result: any) => {
  70. return result.response ? { propertyIsNotUnique: true } : null;
  71. })
  72. )
  73. }
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement