From 26c69017d9655d82a9d50b08f2acdc9da0d3895a Mon Sep 17 00:00:00 2001 From: TheDiaval Date: Fri, 2 Aug 2019 17:27:31 +0300 Subject: [PATCH] fix(account): tenant-box selection --- .../tenant-box/tenant-box.component.html | 6 +- .../tenant-box/tenant-box.component.ts | 65 +++++++++++-------- 2 files changed, 42 insertions(+), 29 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 4082e1e99a..0e233fd038 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 @@ -4,7 +4,7 @@ > {{ 'AbpUiMultiTenancy::Tenant' | abpLocalization }}: - {{ selected?.name ? selected.name : ('AbpUiMultiTenancy::NotSelected' | abpLocalization) }} + {{ tenantName ? tenantName : ('AbpUiMultiTenancy::NotSelected' | abpLocalization) }} ({{ 'AbpUiMultiTenancy::Switch' | abpLocalization @@ -17,11 +17,11 @@
Switch Tenant
-
+
- +

{{ 'AbpUiMultiTenancy::SwitchTenantHint' | 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 2052d135f4..a4cd379c26 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,6 +1,6 @@ -import { ABP, SessionSetTenantId } from '@abp/ng.core'; +import { ABP, SessionSetTenant, SessionState } from '@abp/ng.core'; import { ToasterService } from '@abp/ng.theme.shared'; -import { Component, TemplateRef, ViewChild } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Store } from '@ngxs/store'; import { throwError } from 'rxjs'; import { catchError, take } from 'rxjs/operators'; @@ -11,40 +11,53 @@ import { AccountService } from '../../services/account.service'; selector: 'abp-tenant-box', templateUrl: './tenant-box.component.html', }) -export class TenantBoxComponent { +export class TenantBoxComponent implements OnInit { constructor(private store: Store, private toasterService: ToasterService, private accountService: AccountService) {} - selected = {} as ABP.BasicItem; + tenant = {} as ABP.BasicItem; + + tenantName: string; isModalVisible: boolean; - @ViewChild('modalContent', { static: false }) - modalContent: TemplateRef; + ngOnInit() { + this.tenant = this.store.selectSnapshot(SessionState.getTenant) || ({} as ABP.BasicItem); + this.tenantName = this.tenant.name || ''; + } onSwitch() { this.isModalVisible = true; } save() { - this.selected.name = this.selected.name || ''; - - this.accountService - .findTenant(this.selected.name) - .pipe( - take(1), - catchError(err => { - this.toasterService.error(snq(() => err.error.error_description, 'An error occured.'), 'Error'); - return throwError(err); - }), - ) - .subscribe(({ success, tenantId }) => { - if (success) { - this.isModalVisible = false; - } else { - this.toasterService.error(`Given tenant is not available: ${this.selected.name}`, 'Error'); - this.selected = {} as ABP.BasicItem; - } - this.store.dispatch(new SessionSetTenantId(success ? tenantId : null)); - }); + if (this.tenant.name) { + this.accountService + .findTenant(this.tenant.name) + .pipe( + take(1), + catchError(err => { + this.toasterService.error(snq(() => err.error.error_description, 'An error occured.'), 'Error'); + return throwError(err); + }), + ) + .subscribe(({ success, tenantId }) => { + if (success) { + this.tenant = { + id: tenantId, + name: this.tenant.name, + }; + this.tenantName = this.tenant.name; + this.isModalVisible = false; + } else { + this.toasterService.error(`Given tenant is not available: ${this.tenant.name}`, 'Error'); + this.tenant = {} as ABP.BasicItem; + } + this.store.dispatch(new SessionSetTenant(success ? this.tenant : null)); + }); + } else { + this.store.dispatch(new SessionSetTenant(null)); + this.tenantName = null; + this.isModalVisible = false; + } } }