import { Component, ViewChild, Injector, Output, EventEmitter} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { SecondaryConnectionServiceProxy, CreateSecondaryConnectionInput, UpdateSecondaryConnectionDto, TenantList } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; declare var $: any; @Component({ selector: 'createOrEditSecondaryConnectionModal', templateUrl: './create-or-edit-secondary-connection-modal.component.html' }) export class CreateOrEditSecondaryConnectionModalComponent extends AppComponentBase { @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @ViewChild('modalDirective', { static: false }) modalDirective: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); @Output() showLoaderEvent = new EventEmitter(); @Output() modalSaveEvent = new EventEmitter(); active = false; saving = false; //selectedStartDay:string; //selectedEndDay:string; createInput: CreateSecondaryConnectionInput = new CreateSecondaryConnectionInput(); editInput: UpdateSecondaryConnectionDto = new UpdateSecondaryConnectionDto(); edit = false; tenantList: Array = []; selectedTenant: TenantList; tenantId: number; tenantName: any; connectionString: any; constructor( injector: Injector, private _secondaryConnectionServiceProxy: SecondaryConnectionServiceProxy ) { super(injector); } show(id?: number, tenantName?: string, connectionString?: any): void { if(!id) { this.createInput = new CreateSecondaryConnectionInput(); this.getTenantListFromApi(() => { }); this.edit = false; this.active = true; this.modal.show(); }else { this.tenantName = tenantName; this.editInput.connectionString = connectionString; this.editInput.id = id; this.edit = true; this.active = true; this.modal.show(); } } ngOnInit(): void { } getTenantListFromApi(onFinish: () => void): void { this.showLoaderEvent.emit(true); this.tenantList = []; this._secondaryConnectionServiceProxy.getTenants() .subscribe(result => { this.showLoaderEvent.emit(false); if (result) { this.tenantList = result; } console.log(result); onFinish(); }); } selectedTenantChange($event): void { this.tenantId = null; if (this.selectedTenant != null) { this.tenantId = this.selectedTenant.tenancyId; } } save(): void { this.saving = true; if(!this.edit) { this.createInput.tenantId = this.tenantId; this._secondaryConnectionServiceProxy.createSecondaryConnection(this.createInput) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } else { this._secondaryConnectionServiceProxy.updateSecondaryConnection(this.editInput) .pipe(finalize(() => { this.saving = false;})) .subscribe(() => { this.notify.info(this.l('SavedSuccessfully')); this.close(); this.modalSave.emit(null); }); } } close(): void { this.active = false; this.modal.hide(); } }