import { Component, EventEmitter, Inject, Input, Output } from '@angular/core'; import { FormControl } from '@angular/forms'; import { Subject } from "rxjs/Rx"; import { Profile_CONSTANTS } from '../shared/profile.constants'; import { ManageRoleListService } from "../manage-role-list/manage-role-list.service"; import { TokenAuthenticationService } from '../shared/token-authentication.service'; @Component({ selector: 'rss-collection', templateUrl: './collection.component.html', styleUrls: ['./collection.component.scss'], providers: [ManageRoleListService] }) export class CollectionComponent { @Input('environment') environment: any; @Input('loggedInUser') loggedInUser: any; @Input('collectionConfig') collectionConfig: any; @Output('switchUser') switchUser: EventEmitter = new EventEmitter(); public createDialogConfig: any; public newGroup: Subject = new Subject(); public tenantURL: string; public campusURL: string; public dropdownTenantControl: FormControl = new FormControl(); public dropdownTenantConfig = { valueKey: "code", displayKey: "name", label: "select Tenant" }; public dropdownCampusControl: FormControl = new FormControl(); public dropdownCampusConfig = { valueKey: "campusCd", displayKey: "campusNm", label: "select Campus" } public unsubscribe: Subject = new Subject(); public campusChange: Subject = new Subject(); public tenantCode: string; public campusCode: string; public pageInit: boolean = true; constructor( @Inject(Profile_CONSTANTS) private constants, private tokenAuthenticationService: TokenAuthenticationService, public manageRoleListService: ManageRoleListService) { } ngOnInit() { this.createDialogConfig = { buttonText: this.constants.collection.createDialog.buttonText, placeholderText: this.constants.collection.createDialog.placeholderText } this.bindTenantAndCampus(); } public switchUserEmit(user: any) { user.campus = this.campusCode; user.tenant = this.tenantCode; this.switchUser.emit(user); } public bindTenantAndCampus() { this.tenantCode = this.loggedInUser.tenant; this.campusCode = this.loggedInUser.campus; if(!this.campusCode || !this.tenantCode){ this.tokenAuthenticationService.getPersonProfile().subscribe((res)=>{ this.campusCode = res.campusCode; this.tenantCode = res.tenantCode; }); } this.manageRoleListService.setEnvironment(this.environment); this.tenantURL = this.manageRoleListService.getTenantUrl(); this.campusURL = this.manageRoleListService.getCampusUrl(this.tenantCode); this.dropdownTenantControl.setValue({ code: this.tenantCode }); this.dropdownCampusControl.setValue({ campusCd: this.campusCode }); this.dropdownTenantControl.valueChanges .takeUntil(this.unsubscribe) .filter(tenant => !!tenant) .subscribe(tenant => { this.campusURL = this.manageRoleListService.getCampusUrl(tenant.code); }); this.dropdownCampusControl.valueChanges.debounceTime(800) .takeUntil(this.unsubscribe) .filter(campus => !!campus) .subscribe(campus => { if (campus && !this.pageInit) { this.campusChange.next(campus.campusCd); } this.pageInit = false; }); } public onDialogAdd(groupName) { this.newGroup.next(groupName); } public collectionAlreadyExits(message) { this.createDialogConfig.errorMessage = message; } public closeCreateDialog(close) { this.createDialogConfig.close = true; } }