1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-10 05:30:01 +00:00

Populate empty form with managed environment values

This commit is contained in:
Alec Rippberger
2025-03-25 16:32:53 -05:00
parent 59a483696b
commit 0aef37fc30

View File

@@ -1,5 +1,3 @@
// FIXME: Update this file to be type safe and remove this and next line
// @ts-strict-ignore
import { DialogRef } from "@angular/cdk/dialog";
import { CommonModule } from "@angular/common";
import { Component, OnDestroy, OnInit } from "@angular/core";
@@ -12,12 +10,13 @@ import {
ValidationErrors,
ValidatorFn,
} from "@angular/forms";
import { Subject, firstValueFrom, take, filter, takeUntil } from "rxjs";
import { Subject, firstValueFrom, filter, takeUntil } from "rxjs";
import { JslibModule } from "@bitwarden/angular/jslib.module";
import {
EnvironmentService,
Region,
Urls,
} from "@bitwarden/common/platform/abstractions/environment.service";
import {
AsyncActionsModule,
@@ -81,19 +80,17 @@ export class SelfHostedEnvConfigDialogComponent implements OnInit, OnDestroy {
disableClose: false,
});
const dialogResult = await firstValueFrom(dialogRef.closed);
return dialogResult;
return (await firstValueFrom(dialogRef.closed)) ?? false;
}
formGroup = this.formBuilder.group(
{
baseUrl: [null],
webVaultUrl: [null],
apiUrl: [null],
identityUrl: [null],
iconsUrl: [null],
notificationsUrl: [null],
baseUrl: [""],
webVaultUrl: [""],
apiUrl: [""],
identityUrl: [""],
iconsUrl: [""],
notificationsUrl: [""],
},
{ validators: selfHostedEnvSettingsFormValidator() },
);
@@ -133,32 +130,43 @@ export class SelfHostedEnvConfigDialogComponent implements OnInit, OnDestroy {
private environmentService: EnvironmentService,
) {}
ngOnInit() {
/**
* Populate the form with the current self-hosted environment settings.
*/
async ngOnInit() {
this.environmentService.environment$
.pipe(
take(1),
filter((env) => {
const region = env.getRegion();
return region === Region.SelfHosted;
}),
takeUntil(this.destroy$),
)
.subscribe({
next: (env) => {
const urls = env.getUrls();
this.formGroup.patchValue({
baseUrl: urls.base || "",
webVaultUrl: urls.webVault || "",
apiUrl: urls.api || "",
identityUrl: urls.identity || "",
iconsUrl: urls.icons || "",
notificationsUrl: urls.notifications || "",
});
},
.subscribe((env) => {
const urls = env.getUrls();
this.formGroup.patchValue({
baseUrl: urls.base || "",
webVaultUrl: urls.webVault || "",
apiUrl: urls.api || "",
identityUrl: urls.identity || "",
iconsUrl: urls.icons || "",
notificationsUrl: urls.notifications || "",
});
});
// Check if all form fields are empty
const allFieldsEmpty =
!this.baseUrl.value &&
!this.webVaultUrl.value &&
!this.apiUrl.value &&
!this.identityUrl.value &&
!this.iconsUrl.value &&
!this.notificationsUrl.value;
// If all fields are empty, set the environment to the managed environment as a fallback
if (allFieldsEmpty) {
const managedEnvironment = await this.environmentService.getManagedEnvironment<Urls>();
if (managedEnvironment) {
await this.environmentService.setEnvironment(Region.SelfHosted, managedEnvironment);
}
}
}
submit = async () => {