From 39d901f78a65f13667d5eb07e96849cd8393dc7a Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Thu, 7 May 2020 00:33:59 +0300 Subject: [PATCH] refactor: tenant-box.component --- .../tenant-box/tenant-box.component.html | 104 ++++++++++-------- .../tenant-box/tenant-box.component.ts | 97 +++++++--------- 2 files changed, 98 insertions(+), 103 deletions(-) diff --git a/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.html b/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.html index 007f45f5a9..e8b2df61e1 100644 --- a/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.html +++ b/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.html @@ -1,51 +1,63 @@ -
-
-
-
- {{ - 'AbpUiMultiTenancy::Tenant' | abpLocalization - }}
-
- - {{ tenantName || ('AbpUiMultiTenancy::NotSelected' | abpLocalization) }} - -
-
-
- {{ 'AbpUiMultiTenancy::Switch' | abpLocalization }} + +
+
+
+
+ {{ + 'AbpUiMultiTenancy::Tenant' | abpLocalization + }}
+
+ {{ currentTenant.name || ('AbpUiMultiTenancy::NotSelected' | abpLocalization) }} +
+
+
-
- - -
Switch Tenant
-
- -
-
-
- - + + +
Switch Tenant
+
+ + +
+
+ + +
+

{{ 'AbpUiMultiTenancy::SwitchTenantHint' | abpLocalization }}

-

{{ 'AbpUiMultiTenancy::SwitchTenantHint' | abpLocalization }}

-
- - - - - - {{ 'AbpTenantManagement::Save' | abpLocalization }} - - - + + + + + + {{ 'AbpTenantManagement::Save' | abpLocalization }} + + + + diff --git a/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.ts b/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.ts index fd66913ca5..92d35fcf2e 100644 --- a/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.ts +++ b/npm/ng-packs/packages/account/src/lib/components/tenant-box/tenant-box.component.ts @@ -1,26 +1,26 @@ -import { ABP, SetTenant, SessionState, GetAppConfiguration } from '@abp/ng.core'; +import { ABP, GetAppConfiguration, SessionState, SetTenant } from '@abp/ng.core'; import { ToasterService } from '@abp/ng.theme.shared'; -import { Component, OnInit } from '@angular/core'; -import { Store } from '@ngxs/store'; -import { throwError } from 'rxjs'; -import { catchError, take, finalize, switchMap } from 'rxjs/operators'; -import snq from 'snq'; -import { AccountService } from '../../services/account.service'; +import { Component } from '@angular/core'; +import { Select, Store } from '@ngxs/store'; +import { Observable } from 'rxjs'; +import { finalize, take } from 'rxjs/operators'; import { Account } from '../../models/account'; +import { AccountService } from '../../services/account.service'; @Component({ selector: 'abp-tenant-box', templateUrl: './tenant-box.component.html', }) export class TenantBoxComponent - implements OnInit, Account.TenantBoxComponentInputs, Account.TenantBoxComponentOutputs { - tenant = {} as ABP.BasicItem; + implements Account.TenantBoxComponentInputs, Account.TenantBoxComponentOutputs { + @Select(SessionState.getTenant) + currentTenant$: Observable; - tenantName: string; + name: string; isModalVisible: boolean; - inProgress: boolean; + modalBusy: boolean; constructor( private store: Store, @@ -28,58 +28,41 @@ export class TenantBoxComponent private accountService: AccountService, ) {} - ngOnInit() { - this.tenant = this.store.selectSnapshot(SessionState.getTenant) || ({} as ABP.BasicItem); - this.tenantName = this.tenant.name || ''; - } - onSwitch() { + const tenant = this.store.selectSnapshot(SessionState.getTenant); + this.name = (tenant || ({} as ABP.BasicItem)).name; this.isModalVisible = true; } save() { - if (this.tenant.name && !this.inProgress) { - this.inProgress = true; - this.accountService - .findTenant(this.tenant.name) - .pipe( - finalize(() => (this.inProgress = false)), - take(1), - catchError(err => { - this.toasterService.error( - snq(() => err.error.error_description, 'AbpUi::DefaultErrorMessage'), - 'AbpUi::Error', - ); - return throwError(err); - }), - switchMap(({ success, tenantId }) => { - if (success) { - this.tenant = { - id: tenantId, - name: this.tenant.name, - }; - this.tenantName = this.tenant.name; - this.isModalVisible = false; - } else { - this.toasterService.error( - 'AbpUiMultiTenancy::GivenTenantIsNotAvailable', - 'AbpUi::Error', - { - messageLocalizationParams: [this.tenant.name], - }, - ); - this.tenant = {} as ABP.BasicItem; - this.tenantName = ''; - } - this.store.dispatch(new SetTenant(success ? this.tenant : null)); - return this.store.dispatch(new GetAppConfiguration()); - }), - ) - .subscribe(); - } else { - this.store.dispatch([new SetTenant(null), new GetAppConfiguration()]); - this.tenantName = null; + if (!this.name) { + this.setTenant(null); this.isModalVisible = false; + return; } + + this.modalBusy = true; + this.accountService + .findTenant(this.name) + .pipe(finalize(() => (this.modalBusy = false))) + .subscribe(({ success, tenantId: id, name }) => { + if (!success) { + this.showError(); + return; + } + + this.setTenant({ id, name }); + this.isModalVisible = false; + }); + } + + private setTenant(tenant: ABP.BasicItem) { + return this.store.dispatch([new SetTenant(tenant), new GetAppConfiguration()]); + } + + private showError() { + this.toasterService.error('AbpUiMultiTenancy::GivenTenantIsNotAvailable', 'AbpUi::Error', { + messageLocalizationParams: [this.name], + }); } }