import { Component, OnInit, EventEmitter } from '@angular/core'; import { FormGroup, FormBuilder, Validators } from '@angular/forms'; import { BsModalRef } from 'ngx-bootstrap'; import { ToastrService } from 'ngx-toastr'; import { AuthenticationService } from '../../../../services/authentication.service'; @Component({ selector: 'app-delete-user', templateUrl: './delete-user.component.html', styleUrls: ['./delete-user.component.css'] }) export class DeleteUserComponent implements OnInit { deleteUserForm: FormGroup; allUsers: any[]; public event: EventEmitter = new EventEmitter(); constructor(public bsModalRef: BsModalRef,private fb:FormBuilder,private toastr: ToastrService, private authenticationService: AuthenticationService){} ngOnInit(): void{ this.authenticationService.getAllUsers().subscribe(users =>{ this.allUsers = users['data'] as any[]; }); this.deleteUserForm = this.fb.group({ sso: [, Validators.required], }); } onDelete() { if(this.deleteUserForm.valid) { this.authenticationService.deleteUser(this.deleteUserForm.get('sso').value).subscribe((result: { status: string, message: string, data }) => { if (result.status === 'success') { this.toastr.success('Selected User is deleted'); this.event.emit(true); }else{ this.toastr.success('Selected User can not be deleted'); } this.bsModalRef.hide(); }, (error) => { if(error.error.message = "SequelizeForeignKeyConstraintError"){ this.toastr.error("User cannot be deleted as of Foreign Key Constraint"); }else this.toastr.error("Something Went Wrong! Please try after refresh"); this.bsModalRef.hide(); }, );} else this.deleteUserForm.get('sso').markAllAsTouched(); } get sso() { return this.deleteUserForm.get('sso') } }