users-admin.component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { DeleteAlertComponent } from './delete-alert/delete-alert.component';
  2. import { NewPasswordComponent } from './new-password/new-password.component';
  3. import { MatSnackBar } from '@angular/material/snack-bar';
  4. import { UsersResponse, UserResponse } from './../../../interfaces/users.interface';
  5. import { UsersProfilesService } from './../../../services/users-profiles.service';
  6. import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
  7. import { Router } from '@angular/router';
  8. import { MatDialog } from '@angular/material/dialog';
  9. @Component({
  10. selector: 'app-users-admin',
  11. templateUrl: './users-admin.component.html',
  12. styleUrls: ['./users-admin.component.css']
  13. })
  14. export class UsersAdminComponent implements OnInit {
  15. @ViewChild('searchInput', { static: false }) searchInput: ElementRef<HTMLInputElement> = {} as ElementRef;
  16. users: Array<UserResponse>;
  17. isLoading: boolean;
  18. hasError: boolean;
  19. errorStr: string;
  20. searching: boolean;
  21. usersFinded: Array<UserResponse>;
  22. constructor(
  23. private usersProfilesService: UsersProfilesService,
  24. private _snackBar: MatSnackBar,
  25. private router: Router,
  26. private dialog: MatDialog
  27. ) {
  28. this.users = [];
  29. this.isLoading = true;
  30. this.hasError = false;
  31. this.errorStr = "";
  32. this.searching = false;
  33. this.usersFinded = [];
  34. this.searchInput.nativeElement
  35. }
  36. ngOnInit(): void {
  37. this.getData();
  38. }
  39. getData(){
  40. this.users = [];
  41. this.usersProfilesService.getUsers().subscribe((data: UsersResponse) => {
  42. if(data.error){
  43. this.isLoading = false;
  44. this.hasError = true;
  45. this.errorStr = data.msg;
  46. }else{
  47. this.isLoading = false;
  48. data.response.forEach((item: UserResponse) => {
  49. let usr: UserResponse = {
  50. IDUSUARIO: item.IDUSUARIO,
  51. NOMBRE: item.NOMBRE,
  52. APEPAT: item.APEPAT,
  53. APEMAT: item.APEMAT == null ? "" : item.APEMAT,
  54. PERFIL: item.PERFIL,
  55. EMAIL: item.EMAIL,
  56. ESTATUS: item.ESTATUS
  57. };
  58. this.users.push(usr);
  59. this.usersFinded = this.users;
  60. })
  61. }
  62. }, error => {
  63. this.isLoading = false;
  64. this.hasError = true;
  65. this.errorStr = error.error.msg;
  66. });
  67. }
  68. openDialog(usr: string){
  69. const dialogRef = this.dialog.open(NewPasswordComponent, {
  70. data: {
  71. id: usr
  72. }
  73. });
  74. dialogRef.afterClosed().subscribe(result => {
  75. if(result != ""){
  76. this.usersProfilesService.updatePwd(result).subscribe(data => {
  77. this.openSnackBar(data.msg);
  78. }, error => {
  79. this.openSnackBar(error.error.msg);
  80. })
  81. }
  82. });
  83. }
  84. openAlert(usr: string){
  85. const dialogRef = this.dialog.open(DeleteAlertComponent, {
  86. data: {
  87. id: usr
  88. }
  89. });
  90. dialogRef.afterClosed().subscribe(result => {
  91. if(result != undefined){
  92. if(result) this.usersProfilesService.deleteUser({id: usr}).subscribe(data => {
  93. this.openSnackBar(data.msg);
  94. this.isLoading = true;
  95. this.getData();
  96. }, error => {
  97. this.openSnackBar(error.error.msg);
  98. })
  99. }
  100. });
  101. }
  102. openSnackBar(message: string){
  103. this._snackBar.open(message, "Cerrar", {
  104. duration: 1500
  105. });
  106. }
  107. search(value: string){
  108. if(value.length == 0){
  109. this.openSnackBar("Ingresa una palabra para comenzar la búsqueda.");
  110. }else{
  111. this.searchInput.nativeElement.value = "";
  112. this.searching = true;
  113. this.usersFinded = this.users.filter(user => user.IDUSUARIO.toLowerCase().includes(value.toLowerCase()) ||
  114. user.NOMBRE.toLowerCase().includes(value.toLowerCase()) || user.APEPAT.toLowerCase().includes(value.toLowerCase()) ||
  115. user.APEMAT!.toLowerCase().includes(value.toLowerCase()) || user.PERFIL.toLowerCase().includes(value.toLowerCase()) ||
  116. user.EMAIL.toLowerCase().includes(value.toLowerCase()));
  117. }
  118. }
  119. blockUser(user: string){
  120. let userToBlock = this.users.filter(item => item.IDUSUARIO == user);
  121. let userToBlockStatus = userToBlock[0].ESTATUS;
  122. let newEstatus = userToBlockStatus == "Activo" ? "Inactivo" : "Activo";
  123. this.usersProfilesService.blockUser({
  124. id: user,
  125. estatus: newEstatus
  126. }).subscribe(data => {
  127. this.isLoading = true;
  128. this.getData();
  129. this.openSnackBar(data.msg);
  130. }, error => {
  131. this.openSnackBar(error.error.msg);
  132. })
  133. }
  134. clearSearch(){
  135. this.searching = false;
  136. this.usersFinded = this.users;
  137. this.searchInput.nativeElement.value = "";
  138. }
  139. openUser(action: string, user: string){
  140. this.router.navigate(['new-user'], {
  141. queryParams: {
  142. action: action,
  143. user: user
  144. }
  145. });
  146. }
  147. }