| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- import { DeleteAlertComponent } from './delete-alert/delete-alert.component';
- import { NewPasswordComponent } from './new-password/new-password.component';
- import { MatSnackBar } from '@angular/material/snack-bar';
- import { UsersResponse, UserResponse } from './../../../interfaces/users.interface';
- import { UsersProfilesService } from './../../../services/users-profiles.service';
- import { Component, ElementRef, OnInit, ViewChild } from '@angular/core';
- import { Router } from '@angular/router';
- import { MatDialog } from '@angular/material/dialog';
- @Component({
- selector: 'app-users-admin',
- templateUrl: './users-admin.component.html',
- styleUrls: ['./users-admin.component.css']
- })
- export class UsersAdminComponent implements OnInit {
- @ViewChild('searchInput', { static: false }) searchInput: ElementRef<HTMLInputElement> = {} as ElementRef;
- users: Array<UserResponse>;
- isLoading: boolean;
- hasError: boolean;
- errorStr: string;
- searching: boolean;
- usersFinded: Array<UserResponse>;
- constructor(
- private usersProfilesService: UsersProfilesService,
- private _snackBar: MatSnackBar,
- private router: Router,
- private dialog: MatDialog
- ) {
- this.users = [];
- this.isLoading = true;
- this.hasError = false;
- this.errorStr = "";
- this.searching = false;
- this.usersFinded = [];
- this.searchInput.nativeElement
- }
- ngOnInit(): void {
- this.getData();
- }
- getData(){
- this.users = [];
- this.usersProfilesService.getUsers().subscribe((data: UsersResponse) => {
- if(data.error){
- this.isLoading = false;
- this.hasError = true;
- this.errorStr = data.msg;
- }else{
- this.isLoading = false;
- data.response.forEach((item: UserResponse) => {
- let usr: UserResponse = {
- IDUSUARIO: item.IDUSUARIO,
- NOMBRE: item.NOMBRE,
- APEPAT: item.APEPAT,
- APEMAT: item.APEMAT == null ? "" : item.APEMAT,
- PERFIL: item.PERFIL,
- EMAIL: item.EMAIL,
- ESTATUS: item.ESTATUS
- };
- this.users.push(usr);
- this.usersFinded = this.users;
- })
- }
- }, error => {
- this.isLoading = false;
- this.hasError = true;
- this.errorStr = error.error.msg;
- });
- }
- openDialog(usr: string){
- const dialogRef = this.dialog.open(NewPasswordComponent, {
- data: {
- id: usr
- }
- });
- dialogRef.afterClosed().subscribe(result => {
- if(result != ""){
- this.usersProfilesService.updatePwd(result).subscribe(data => {
- this.openSnackBar(data.msg);
- }, error => {
- this.openSnackBar(error.error.msg);
- })
- }
- });
- }
- openAlert(usr: string){
- const dialogRef = this.dialog.open(DeleteAlertComponent, {
- data: {
- id: usr
- }
- });
- dialogRef.afterClosed().subscribe(result => {
- if(result != undefined){
- if(result) this.usersProfilesService.deleteUser({id: usr}).subscribe(data => {
- this.openSnackBar(data.msg);
- this.isLoading = true;
- this.getData();
- }, error => {
- this.openSnackBar(error.error.msg);
- })
- }
- });
- }
- openSnackBar(message: string){
- this._snackBar.open(message, "Cerrar", {
- duration: 1500
- });
- }
- search(value: string){
- if(value.length == 0){
- this.openSnackBar("Ingresa una palabra para comenzar la búsqueda.");
- }else{
- this.searchInput.nativeElement.value = "";
- this.searching = true;
- this.usersFinded = this.users.filter(user => user.IDUSUARIO.toLowerCase().includes(value.toLowerCase()) ||
- user.NOMBRE.toLowerCase().includes(value.toLowerCase()) || user.APEPAT.toLowerCase().includes(value.toLowerCase()) ||
- user.APEMAT!.toLowerCase().includes(value.toLowerCase()) || user.PERFIL.toLowerCase().includes(value.toLowerCase()) ||
- user.EMAIL.toLowerCase().includes(value.toLowerCase()));
- }
- }
- blockUser(user: string){
- let userToBlock = this.users.filter(item => item.IDUSUARIO == user);
- let userToBlockStatus = userToBlock[0].ESTATUS;
- let newEstatus = userToBlockStatus == "Activo" ? "Inactivo" : "Activo";
- this.usersProfilesService.blockUser({
- id: user,
- estatus: newEstatus
- }).subscribe(data => {
- this.isLoading = true;
- this.getData();
- this.openSnackBar(data.msg);
- }, error => {
- this.openSnackBar(error.error.msg);
- })
- }
- clearSearch(){
- this.searching = false;
- this.usersFinded = this.users;
- this.searchInput.nativeElement.value = "";
- }
- openUser(action: string, user: string){
- this.router.navigate(['new-user'], {
- queryParams: {
- action: action,
- user: user
- }
- });
- }
- }
|