import { Component, ViewChild, Injector, Output, EventEmitter, OnInit} from '@angular/core'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; import { OrderSourceConnectionsServiceProxy, CreateOrEditOrderSourceConnectionDto, ConnectionDto, ConnectionType } from '@shared/service-proxies/service-proxies'; import { AppComponentBase } from '@shared/common/app-component-base'; import * as moment from 'moment'; @Component({ selector: 'createOrEditOrderSourceConnectionModal', templateUrl: './create-or-edit-orderSourceConnection-modal.component.html' }) export class CreateOrEditOrderSourceConnectionModalComponent extends AppComponentBase implements OnInit{ @ViewChild('createOrEditModal', { static: true }) modal: ModalDirective; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; connectionType isActive : boolean = true; connection : ConnectionDto = new ConnectionDto(); connectionId; orderSourceConnection: CreateOrEditOrderSourceConnectionDto = new CreateOrEditOrderSourceConnectionDto(); constructor( injector: Injector, private _orderSourceConnectionsServiceProxy: OrderSourceConnectionsServiceProxy ) { super(injector); } ngOnInit(): void { } onShown(){ $('.kt-select2').select2(); $("#connectionType").select2({ minimumResultsForSearch: Infinity }); let setConnectionType = (val) => { return this.connectionType = val } setConnectionType('FTPS'); $('#connectionType').on('select2:select', function (e) { if($('#connectionType').val() == 'FTPS'){ setConnectionType('FTPS') } else if($('#connectionType').val() == 'FTP'){ setConnectionType('FTP') } else{ setConnectionType('API') } }); if(this.connectionId){ $("#connectionType").prop("disabled", true); $("#connectionType").trigger('change.select2'); }else{ $("#connectionType").prop("disabled", false); $("#connectionType").trigger('change.select2'); } } show(orderSourceId?: number,connectionId? : number,connectionType? : ConnectionType): void { this.connection.isActive = true; this.connectionType = connectionType; this.connectionId = connectionId; if (!connectionId) { this.orderSourceConnection = new CreateOrEditOrderSourceConnectionDto(); this.orderSourceConnection.orderSourceId = orderSourceId; this.connection = new ConnectionDto(); this.connection.isActive = true; this.active = true; this.modal.show(); } else { this._orderSourceConnectionsServiceProxy.getConnectionForEdit(orderSourceId,connectionId,this.connectionType).subscribe(result => { this.orderSourceConnection = result.orderSourceConnection; this.connection = result.orderSourceConnection.connection; this.connectionType = result.orderSourceConnection.connectionType; this.active = true; this.modal.show(); }); } } save(): void { this.saving = true; this.orderSourceConnection.connectionType = this.connectionType; this.orderSourceConnection.connection = this.connection if(this.connectionType == 'API'){ this.saving = false; this.message.info('API is not available at the moment') return; } this._orderSourceConnectionsServiceProxy.createOrEdit(this.orderSourceConnection) .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(); } }