user-profile > added "switch tenant" option

This commit is contained in:
Sofia Papacharalampous 2024-04-03 15:55:22 +03:00
parent ca4e7e6feb
commit 6f7ed58523
2 changed files with 68 additions and 4 deletions

View File

@ -3,7 +3,31 @@
<div class="profile">
<div class="container-fluid">
<div *ngIf="user | async as userProfile; else loading" class="user-profile">
<div class="col user-profile-title">{{'USER-DIALOG.USER-PROFILE' | translate}}</div>
<div class="row user-profile-title">
<div class="col mb-2">
{{'USER-DIALOG.USER-PROFILE' | translate}}
</div>
<div *ngIf="tenantFormGroup" class="col-auto mr-5">
<div class="row">
<div class="col">
<mat-form-field class="w-100">
<mat-label>Tenant</mat-label>
<mat-select placeholder="Tenant" [formControl]="this.tenantFormGroup.get('tenantCode')">
<ng-container *ngFor="let tenant of tenants | async">
<mat-option [value]="tenant">{{ tenant }}</mat-option>
</ng-container>
</mat-select>
</mat-form-field>
</div>
<div class="col-12 col-lg-auto mt-1">
<button mat-mini-fab (click)="switchTenant()">
<mat-icon class="mat-mini-fab-icon">refresh</mat-icon>
</button>
</div>
</div>
</div>
</div>
<div class="row profile-content" [formGroup]="formGroup">
<div class="col">
<div class="row mb-4">

View File

@ -1,6 +1,6 @@
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { UntypedFormArray, UntypedFormGroup } from '@angular/forms';
import { FormBuilder, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Params, Router } from '@angular/router';
import { RoleOrganizationType } from '@app/core/common/enum/role-organization-type';
@ -22,12 +22,16 @@ import { FormValidationErrorsDialogComponent } from '@common/forms/form-validati
import { ConfirmationDialogComponent } from '@common/modules/confirmation-dialog/confirmation-dialog.component';
import { TranslateService } from '@ngx-translate/core';
import * as moment from 'moment-timezone';
import { Observable, of } from 'rxjs';
import { Observable, from, of } from 'rxjs';
import { map, takeUntil } from 'rxjs/operators';
import { AddAccountDialogComponent } from './add-account/add-account-dialog.component';
import { UserProfileEditorModel } from './user-profile-editor.model';
import { nameof } from 'ts-simple-nameof';
import { Guid } from '@common/types/guid';
import { BaseHttpParams } from '@common/http/base-http-params';
import { InterceptorType } from '@common/http/interceptors/interceptor-type';
import { PrincipalService } from '@app/core/services/http/principal.service';
import { KeycloakService } from 'keycloak-angular';
@Component({
selector: 'app-user-profile',
@ -51,6 +55,7 @@ export class UserProfileComponent extends BaseComponent implements OnInit, OnDes
errorMessages = [];
nestedCount = [];
nestedIndex = 0;
tenants: Observable<Array<string>>;
organisationsAutoCompleteConfiguration: MultipleAutoCompleteConfiguration = {
filterFn: this.filterOrganisations.bind(this),
@ -61,6 +66,8 @@ export class UserProfileComponent extends BaseComponent implements OnInit, OnDes
};
formGroup: UntypedFormGroup;
tenantFormGroup: UntypedFormGroup;
constructor(
private userService: UserService,
private route: ActivatedRoute,
@ -75,7 +82,10 @@ export class UserProfileComponent extends BaseComponent implements OnInit, OnDes
private dialog: MatDialog,
public enumUtils: EnumUtils,
private httpClient: HttpClient,
private matomoService: MatomoService
private matomoService: MatomoService,
private formBuilder: UntypedFormBuilder,
private keycloakService: KeycloakService,
private principalService: PrincipalService
) {
super();
this.languages = this.languageService.getAvailableLanguagesCodes();
@ -100,6 +110,9 @@ export class UserProfileComponent extends BaseComponent implements OnInit, OnDes
}
ngOnInit() {
this.tenantFormGroup = this.formBuilder.group({
tenantCode: [this.authService.selectedTenant(), [Validators.required]]
});
this.matomoService.trackPageView('User Profile');
this.route.params
.pipe(takeUntil(this._destroyed))
@ -149,6 +162,8 @@ export class UserProfileComponent extends BaseComponent implements OnInit, OnDes
.pipe(takeUntil(this._destroyed))
.subscribe(x => { if (x) { this.cultures = this._filterCulture(x); } });
// this.initializeDisabledFormGroup();
this.tenants = this.loadUserTenants();
this.unlock();
return result;
}));
@ -407,4 +422,29 @@ export class UserProfileComponent extends BaseComponent implements OnInit, OnDes
}
}
// Switch Tenant
loadUserTenants(): Observable<Array<string>> {
const params = new BaseHttpParams();
params.interceptorContext = {
excludedInterceptors: [InterceptorType.TenantHeaderInterceptor]
};
return this.principalService.myTenants({ params: params });
}
switchTenant(): void {
if (this.tenantFormGroup.valid === false) return;
const selectedTenant = this.tenantFormGroup.get('tenantCode').value;
this.formSubmit(selectedTenant);
this.loadUser();
}
formSubmit(selectedTenant: string): void {
this.authService.selectedTenant(selectedTenant);
}
loadUser(): void {
const returnUrl = '/profile';
this.authService.prepareAuthRequest(from(this.keycloakService.getToken()), {}).pipe(takeUntil(this._destroyed)).subscribe(() => this.authService.onAuthenticateSuccess(returnUrl), (error) => this.authService.onAuthenticateError(error));
}
}