import { Component, EventEmitter, Input, Inject, Output, QueryList, ViewChild, ViewChildren } from '@angular/core'; import { FormControl } from '@angular/forms'; import { MatAutocompleteTrigger } from '@angular/material'; import { Subject } from "rxjs/Rx"; import { Stream } from 'stream'; import { ManageRoleService } from "./manage-role.service"; import { Profile_CONSTANTS } from '../shared/profile.constants'; @Component({ selector: 'rss-manage-role', templateUrl: './manage-role.component.html', styleUrls: ['../shared/shared.component.scss'], providers: [ManageRoleService] }) export class ManageRoleComponent { @Input('environment') environment: any; @Input('loggedInUser') loggedInUser: any; @Input('roleConfig') roleConfig: any; @Input() campusChange: Subject; @ViewChild(MatAutocompleteTrigger) matAutoCompleteTrigger: MatAutocompleteTrigger; @Output() switchUser: EventEmitter = new EventEmitter(); public PeopleAutoSearch = new FormControl(); public httpSubscription: any; public listPeople: any; public showLoading: boolean = true; public showPeopleList: boolean = true; public personAlreadyExists: boolean = false; public peopleList: any; public campusCode: string; constructor(public manageRoleService: ManageRoleService, @Inject(Profile_CONSTANTS) private constants) { this.PeopleAutoSearch = new FormControl(); this.PeopleAutoSearch.setValue(""); this.getAutosuggestPeople(); } private getAutosuggestPeople() { this.showHideAutosuggestLoading(); this.PeopleAutoSearch.valueChanges.debounceTime(300).subscribe(val => { if (val.length > 1) { let campus = !this.loggedInUser.roles.includes(this.constants.roles.ServiceDesk.value) && (this.loggedInUser.roles.includes(this.constants.roles.EHSAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.BiosafetyAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.RadiationAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.ControlAreaAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.ChemicalAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.CLIAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.BSASAdmin.value) || this.loggedInUser.roles.includes(this.constants.roles.NotificationManager.value) || this.loggedInUser.roles.includes(this.constants.roles.NFPAAdmin.value) // || this.loggedInUser.roles.includes(this.constants.roles.ProceduresAdminRole.value) // || this.loggedInUser.roles.includes(this.constants.roles.ProceduresTemplateAdminRole.value) || this.loggedInUser.roles.includes(this.constants.roles.Developer.value)) ? "" : this.campusCode; this.httpSubscription = this.manageRoleService.getSearchResultsByCampus(this.PeopleAutoSearch.value, campus).subscribe(res => { this.listPeople = res; this.showLoading = false; this.showPeopleList = true; this.matAutoCompleteTrigger.closePanel(); this.matAutoCompleteTrigger.openPanel(); }); } }); } public onRemoveRole = (user: any) => { this.manageRoleService.removeRole(user.userId, this.campusCode, this.roleConfig.roleValue) .subscribe(res => { this.peopleList = this.peopleList.filter(item => (item.userId !== user.userId)); }); } public addPerson = (person: any) => { this.matAutoCompleteTrigger.writeValue(""); this.PeopleAutoSearch.setValue(""); this.listPeople = []; this.matAutoCompleteTrigger.closePanel(); if (this.isPersonMember(person)) { this.showPersonExistMessage(); } else { this.personAlreadyExists = false; this.manageRoleService.addRole(person.userId, this.campusCode, this.roleConfig.roleValue) .subscribe(res => { this.peopleList.push(person); }); } } public isPersonMember = (person) => { return (this.peopleList.find((item) => (item.userId === person.userId || item.email === person.email)) !== undefined); } public showPersonExistMessage = () => { this.personAlreadyExists = true; } private highlightSearchText(people: any) { return this.manageRoleService.highlightSearchText(people, this.PeopleAutoSearch.value); } private showHideAutosuggestLoading() { this.PeopleAutoSearch.valueChanges.subscribe(val => { if (this.httpSubscription) this.httpSubscription.unsubscribe(); if (val.length > 0) this.hidePersonExistMessage(); if (val.length > 1) { this.listPeople = null; setTimeout(() => { if (!this.listPeople) this.showLoading = true }, 1000);//Delay loading for one second this.showPeopleList = false; } else { this.showPeopleList = false; this.showLoading = false; } }); } public hidePersonExistMessage = () => { this.personAlreadyExists = false; } ngOnInit() { this.campusCode = this.roleConfig.campus; this.manageRoleService.setEnvironment(this.environment); this.getRoleMembers(); if (this.campusChange) this.campusChange.subscribe(event => { if (event) { this.campusCode = event; this.peopleList = null; this.getRoleMembers(); } }); } public getRoleMembers() { this.manageRoleService.getRoleMembers(this.campusCode, this.roleConfig.roleValue) .subscribe((res: any) => { this.peopleList = res.members; }); } public onViewProfile = (user) => { this.switchUser.emit(user); } }