red-ui/apps/red-ui/src/app/components/tenants-menu/tenants-menu.component.ts
2023-10-05 16:44:54 +03:00

36 lines
1.2 KiB
TypeScript

import { Component, inject } from '@angular/core';
import { IStoredTenantId, KeycloakStatusService, TenantsService } from '@iqser/common-ui/lib/tenants';
interface ITenant extends IStoredTenantId {
readonly isCurrent: boolean;
}
@Component({
selector: 'app-tenants-menu',
templateUrl: './tenants-menu.component.html',
styleUrls: ['./tenants-menu.component.scss'],
})
export class TenantsMenuComponent {
readonly tenantsService = inject(TenantsService);
readonly storedTenants: ITenant[];
readonly #keycloakStatusService = inject(KeycloakStatusService);
constructor() {
this.storedTenants = this.#getStoredTenants();
}
trackBy(_index: number, item: ITenant) {
return item.tenantId;
}
async switchTenant(tenantId?: string) {
await this.#keycloakStatusService.switchTenant(tenantId);
}
#getStoredTenants(): ITenant[] {
const storedTenants = this.tenantsService.getStoredTenants();
const sorted = storedTenants.sort((a, b) => a.tenantId.localeCompare(b.tenantId));
return sorted.map(tenant => ({ ...tenant, isCurrent: tenant.tenantId === this.tenantsService.activeTenantId }));
}
}