1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-15 15:53:27 +00:00

Squash commits (#4382)

Co-authored-by: Hinton <hinton@users.noreply.github.com>
This commit is contained in:
cd-bitwarden
2023-01-05 13:31:57 -05:00
committed by GitHub
parent 049940d04b
commit 0a5f96c560
9 changed files with 148 additions and 19 deletions

View File

@@ -1,11 +1,15 @@
import { DialogRef, DIALOG_DATA } from "@angular/cdk/dialog";
import { Component, Inject, OnInit } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { Subject, takeUntil } from "rxjs";
import { I18nService } from "@bitwarden/common/abstractions/i18n.service";
import { PlatformUtilsService } from "@bitwarden/common/abstractions/platformUtils.service";
import { ProjectListView } from "../../models/view/project-list.view";
import { SecretProjectView } from "../../models/view/secret-project.view";
import { SecretView } from "../../models/view/secret.view";
import { ProjectService } from "../../projects/project.service";
import { SecretService } from "../secret.service";
export enum OperationType {
@@ -29,37 +33,87 @@ export class SecretDialogComponent implements OnInit {
name: new FormControl("", [Validators.required]),
value: new FormControl("", [Validators.required]),
notes: new FormControl(""),
project: new FormControl(""),
});
protected loading = false;
protected loading = false;
projects: ProjectListView[];
selectedProjects: SecretProjectView[] = [];
private destroy$ = new Subject<void>();
constructor(
public dialogRef: DialogRef,
@Inject(DIALOG_DATA) private data: SecretOperation,
private secretService: SecretService,
private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService
private platformUtilsService: PlatformUtilsService,
private projectService: ProjectService
) {}
async ngOnInit() {
this.projects = await this.projectService.getProjects(this.data.organizationId);
if (this.data.operation === OperationType.Edit && this.data.secretId) {
await this.loadData();
} else if (this.data.operation !== OperationType.Add) {
this.dialogRef.close();
throw new Error(`The secret dialog was not called with the appropriate operation values.`);
}
this.formGroup
.get("project")
.valueChanges.pipe(takeUntil(this.destroy$))
.subscribe(() => this.updateProjectList());
}
async loadData() {
this.loading = true;
const secret: SecretView = await this.secretService.getBySecretId(this.data.secretId);
this.loading = false;
this.formGroup.setValue({ name: secret.name, value: secret.value, notes: secret.note });
this.selectedProjects = secret.projects;
this.loading = false;
this.formGroup.setValue({
name: secret.name,
value: secret.value,
notes: secret.note,
project: "",
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
get title() {
return this.data.operation === OperationType.Add ? "newSecret" : "editSecret";
}
async removeProjectAssociation(id: string) {
this.selectedProjects = this.selectedProjects.filter((e) => e.id != id);
this.formGroup.get("project").setValue("");
}
updateProjectList() {
const newList: SecretProjectView[] = [];
const projectId = this.formGroup.get("project").value;
if (projectId) {
const selectedProject = this.projects?.filter((p) => p.id == projectId)[0];
if (selectedProject != undefined) {
const projectSecretView = new SecretProjectView();
projectSecretView.id = selectedProject.id;
projectSecretView.name = selectedProject.name;
newList.push(projectSecretView);
}
}
this.selectedProjects = newList;
}
submit = async () => {
this.formGroup.markAllAsTouched();
@@ -69,7 +123,7 @@ export class SecretDialogComponent implements OnInit {
const secretView = this.getSecretView();
if (this.data.operation === OperationType.Add) {
await this.createSecret(secretView, this.data.projectId);
await this.createSecret(secretView);
} else {
secretView.id = this.data.secretId;
await this.updateSecret(secretView);
@@ -77,8 +131,8 @@ export class SecretDialogComponent implements OnInit {
this.dialogRef.close();
};
private async createSecret(secretView: SecretView, projectId?: string) {
await this.secretService.create(this.data.organizationId, secretView, projectId);
private async createSecret(secretView: SecretView) {
await this.secretService.create(this.data.organizationId, secretView);
this.platformUtilsService.showToast("success", null, this.i18nService.t("secretCreated"));
}
@@ -88,11 +142,14 @@ export class SecretDialogComponent implements OnInit {
}
private getSecretView() {
const emptyProjects: SecretProjectView[] = [];
const secretView = new SecretView();
secretView.organizationId = this.data.organizationId;
secretView.name = this.formGroup.value.name;
secretView.value = this.formGroup.value.value;
secretView.note = this.formGroup.value.notes;
secretView.projects = this.selectedProjects ? this.selectedProjects : emptyProjects;
return secretView;
}
}