common-ui/src/lib/tenants/tenant-select/tenant-select.component.ts

92 lines
3.7 KiB
TypeScript

import { AfterViewInit, ChangeDetectionStrategy, Component, inject, Input, OnInit } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Title } from '@angular/platform-browser';
import { KeycloakService } from 'keycloak-angular';
import { NGXLogger } from 'ngx-logger';
import { LoadingService } from '../../loading';
import { getConfig } from '../../services';
import { selectTenantTranslations } from '../../translations/select-tenant-translations';
import { UI_ROOT } from '../../utils';
import { getKeycloakOptions } from '../keycloak-options';
import { IStoredTenantId, TenantsService } from '../services';
import { KeycloakStatusService } from '../services/keycloak-status.service';
import { CacheApiService } from '../../caching';
@Component({
templateUrl: './tenant-select.component.html',
styleUrls: ['./tenant-select.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: false,
})
export class TenantSelectComponent implements OnInit {
readonly #uiRoot = inject(UI_ROOT);
readonly #cacheApiService = inject(CacheApiService);
protected readonly logger = inject(NGXLogger);
protected readonly tenantsService = inject(TenantsService);
protected storedTenants: IStoredTenantId[] = [];
protected readonly titleService = inject(Title);
protected readonly config = getConfig();
protected readonly loadingService = inject(LoadingService);
protected readonly keycloakService = inject(KeycloakService);
protected readonly keycloakStatusService = inject(KeycloakStatusService);
protected readonly form = inject(FormBuilder).group({
// eslint-disable-next-line @typescript-eslint/unbound-method
tenantId: ['', Validators.required],
});
protected readonly translations = selectTenantTranslations;
@Input() isLoggedOut = false;
@Input() noRoleLogOut = false;
async ngOnInit() {
await this.#loadStoredTenants();
}
updateTenantSelection() {
const tenantId = this.form.controls.tenantId.value;
if (!tenantId) {
throw new Error('No tenant selected!');
}
this.loadingService.start();
return this.select(tenantId);
}
async select(tenantId: string) {
try {
this.logger.info('[KEYCLOAK] Initializing keycloak for tenant', tenantId);
await this.keycloakService.init(getKeycloakOptions(this.#uiRoot, this.config, tenantId));
} catch (e) {
this.logger.info('[KEYCLOAK] Init failed. Logout');
return this.keycloakService.logout();
}
const url = this.keycloakService.getKeycloakInstance().createLoginUrl({
redirectUri: this.keycloakStatusService.createLoginUrl(tenantId),
idpHint: this.config.OAUTH_IDP_HINT,
});
this.logger.info('[KEYCLOAK] Init succeeded. Logout and redirect to', url);
return this.keycloakService.logout(url);
}
async removeStored(tenantId: string) {
this.tenantsService.removeStored(tenantId);
await this.#loadStoredTenants();
}
protected tenantIcon(tenant: IStoredTenantId) {
return `red:${tenant.documine ? 'documine' : 'redaction'}-logo`;
}
async #loadStoredTenants() {
const lastVersionAppVersion = await this.#cacheApiService.getCachedValue('FRONTEND_APP_VERSION');
const currentAppVersion = this.config.FRONTEND_APP_VERSION;
console.log(lastVersionAppVersion);
console.log(currentAppVersion);
if (lastVersionAppVersion !== currentAppVersion) {
this.tenantsService.clearStoredTenants();
}
this.storedTenants = this.tenantsService.getStoredTenants().sort((a, b) => a.tenantId.localeCompare(b.tenantId));
}
}