import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild } from '@angular/core'; import { AppComponentBase } from '@shared/common/app-component-base'; import { CommonLookupServiceProxy, SubscribableEditionComboboxItemDto, TenantEditDto, TenantServiceProxy } from '@shared/service-proxies/service-proxies'; import * as _ from 'lodash'; import * as moment from 'moment'; import { ModalDirective } from 'ngx-bootstrap'; import { finalize } from 'rxjs/operators'; @Component({ selector: 'editTenantModal', templateUrl: './edit-tenant-modal.component.html' }) export class EditTenantModalComponent extends AppComponentBase { @ViewChild('nameInput', {static: true}) nameInput: ElementRef; @ViewChild('editModal', {static: true}) modal: ModalDirective; @ViewChild('SubscriptionEndDateUtc', {static: false}) subscriptionEndDateUtc: ElementRef; @Output() modalSave: EventEmitter = new EventEmitter(); active = false; saving = false; isUnlimited = false; subscriptionEndDateUtcIsValid = false; tenant: TenantEditDto = undefined; currentConnectionString: string; editions: SubscribableEditionComboboxItemDto[] = []; isSubscriptionFieldsVisible = false; constructor( injector: Injector, private _tenantService: TenantServiceProxy, private _commonLookupService: CommonLookupServiceProxy ) { super(injector); } show(tenantId: number): void { this.active = true; this._commonLookupService.getEditionsForCombobox(false).subscribe(editionsResult => { this.editions = editionsResult.items; let notSelectedEdition = new SubscribableEditionComboboxItemDto(); notSelectedEdition.displayText = this.l('NotAssigned'); notSelectedEdition.value = ''; this.editions.unshift(notSelectedEdition); this._tenantService.getTenantForEdit(tenantId).subscribe((tenantResult) => { this.tenant = tenantResult; this.currentConnectionString = tenantResult.connectionString; this.tenant.editionId = this.tenant.editionId || 0; this.isUnlimited = !this.tenant.subscriptionEndDateUtc; this.subscriptionEndDateUtcIsValid = this.isUnlimited || this.tenant.subscriptionEndDateUtc !== undefined; this.modal.show(); this.toggleSubscriptionFields(); }); }); } onShown(): void { document.getElementById('Name').focus(); if (this.tenant.subscriptionEndDateUtc) { (this.subscriptionEndDateUtc.nativeElement as any).value = this.tenant.subscriptionEndDateUtc.format('L'); } } subscriptionEndDateChange(e): void { this.subscriptionEndDateUtcIsValid = e && e.date !== false || this.isUnlimited; } selectedEditionIsFree(): boolean { if (!this.tenant.editionId) { return true; } let selectedEditions = _.filter(this.editions, { value: this.tenant.editionId + '' }); if (selectedEditions.length !== 1) { return true; } let selectedEdition = selectedEditions[0]; return selectedEdition.isFree; } save(): void { this.saving = true; if (this.tenant.editionId === 0) { this.tenant.editionId = null; } //take selected date as UTC if (this.isUnlimited || !this.tenant.editionId) { this.tenant.subscriptionEndDateUtc = null; } this._tenantService.updateTenant(this.tenant) .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(); } onEditionChange(): void { if (this.selectedEditionIsFree()) { this.tenant.isInTrialPeriod = false; } this.toggleSubscriptionFields(); } onUnlimitedChange(): void { if (this.isUnlimited) { this.tenant.subscriptionEndDateUtc = null; this.subscriptionEndDateUtcIsValid = true; } else { if (!this.tenant.subscriptionEndDateUtc) { this.subscriptionEndDateUtcIsValid = false; } } } toggleSubscriptionFields() { if (this.tenant.editionId > 0) { this.isSubscriptionFieldsVisible = true; } else { this.isSubscriptionFieldsVisible = false; } } }