Files
clients/libs/angular/src/vault/components/folder-add-edit.component.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-04-13 00:05:58 -04:00
import { Directive, EventEmitter, Input, OnInit, Output } from "@angular/core";
import { Validators, FormBuilder } from "@angular/forms";
2018-04-13 00:05:58 -04:00
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
[SG-998] and [SG-999] Vault and Autofill team refactor (#4542) * Move DeprecatedVaultFilterService to vault folder * [libs] move VaultItemsComponent * [libs] move AddEditComponent * [libs] move AddEditCustomFields * [libs] move attachmentsComponent * [libs] folderAddEditComponent * [libs] IconComponent * [libs] PasswordRepormptComponent * [libs] PremiumComponent * [libs] ViewCustomFieldsComponent * [libs] ViewComponent * [libs] PasswordRepromptService * [libs] Move FolderService and FolderApiService abstractions * [libs] FolderService imports * [libs] PasswordHistoryComponent * [libs] move Sync and SyncNotifier abstractions * [libs] SyncService imports * [libs] fix file casing for passwordReprompt abstraction * [libs] SyncNotifier import fix * [libs] CipherServiceAbstraction * [libs] PasswordRepromptService abstraction * [libs] Fix file casing for angular passwordReprompt service * [libs] fix file casing for SyncNotifierService * [libs] CipherRepromptType * [libs] rename CipherRepromptType * [libs] CipherType * [libs] Rename CipherType * [libs] CipherData * [libs] FolderData * [libs] PasswordHistoryData * [libs] AttachmentData * [libs] CardData * [libs] FieldData * [libs] IdentityData * [libs] LocalData * [libs] LoginData * [libs] SecureNoteData * [libs] LoginUriData * [libs] Domain classes * [libs] SecureNote * [libs] Request models * [libs] Response models * [libs] View part 1 * [libs] Views part 2 * [libs] Move folder services * [libs] Views fixes * [libs] Move sync services * [libs] cipher service * [libs] Types * [libs] Sync file casing * [libs] Fix folder service import * [libs] Move spec files * [libs] casing fixes on spec files * [browser] Autofill background, clipboard, commands * [browser] Fix ContextMenusBackground casing * [browser] Rename fix * [browser] Autofill content * [browser] autofill.js * [libs] enpass importer spec fix * [browser] autofill models * [browser] autofill manifest path updates * [browser] Autofill notification files * [browser] autofill services * [browser] Fix file casing * [browser] Vault popup loose components * [browser] Vault components * [browser] Manifest fixes * [browser] Vault services * [cli] vault commands and models * [browser] File capitilization fixes * [desktop] Vault components and services * [web] vault loose components * [web] Vault components * [browser] Fix misc-utils import * [libs] Fix psono spec imports * [fix] Add comments to address lint rules
2023-01-31 16:08:37 -05:00
import { FolderApiServiceAbstraction } from "@bitwarden/common/vault/abstractions/folder/folder-api.service.abstraction";
import { FolderService } from "@bitwarden/common/vault/abstractions/folder/folder.service.abstraction";
import { FolderView } from "@bitwarden/common/vault/models/view/folder.view";
import { DialogService } from "@bitwarden/components";
@Directive()
2018-04-13 00:05:58 -04:00
export class FolderAddEditComponent implements OnInit {
@Input() folderId: string;
@Output() onSavedFolder = new EventEmitter<FolderView>();
@Output() onDeletedFolder = new EventEmitter<FolderView>();
2022-02-22 15:39:11 +01:00
editMode = false;
2018-04-13 00:05:58 -04:00
folder: FolderView = new FolderView();
title: string;
formPromise: Promise<any>;
deletePromise: Promise<any>;
protected componentName = "";
2018-04-13 00:05:58 -04:00
formGroup = this.formBuilder.group({
name: ["", [Validators.required]],
});
2018-04-13 00:05:58 -04:00
constructor(
protected folderService: FolderService,
protected folderApiService: FolderApiServiceAbstraction,
2018-04-13 00:05:58 -04:00
protected i18nService: I18nService,
protected platformUtilsService: PlatformUtilsService,
protected logService: LogService,
protected dialogService: DialogService,
protected formBuilder: FormBuilder,
) {}
2018-04-13 00:05:58 -04:00
async ngOnInit() {
2019-03-06 14:31:32 -05:00
await this.init();
2018-04-13 00:05:58 -04:00
}
async submit(): Promise<boolean> {
this.folder.name = this.formGroup.controls.name.value;
2018-04-13 00:05:58 -04:00
if (this.folder.name == null || this.folder.name === "") {
2018-10-02 23:09:19 -04:00
this.platformUtilsService.showToast(
"error",
2018-04-13 00:05:58 -04:00
this.i18nService.t("errorOccurred"),
this.i18nService.t("nameRequired"),
);
return false;
}
2018-04-13 00:05:58 -04:00
2021-12-16 13:36:21 +01:00
try {
2018-04-13 00:05:58 -04:00
const folder = await this.folderService.encrypt(this.folder);
this.formPromise = this.folderApiService.save(folder);
2018-04-13 00:05:58 -04:00
await this.formPromise;
this.platformUtilsService.showToast(
2021-12-16 13:36:21 +01:00
"success",
null,
2018-04-13 00:05:58 -04:00
this.i18nService.t(this.editMode ? "editedFolder" : "addedFolder"),
2021-12-16 13:36:21 +01:00
);
2018-04-13 00:05:58 -04:00
this.onSavedFolder.emit(this.folder);
return true;
} catch (e) {
this.logService.error(e);
2018-04-13 00:05:58 -04:00
}
return false;
}
async delete(): Promise<boolean> {
const confirmed = await this.dialogService.openSimpleDialog({
title: { key: "deleteFolder" },
content: { key: "deleteFolderConfirmation" },
type: "warning",
});
2018-04-13 00:05:58 -04:00
if (!confirmed) {
return false;
}
2018-04-13 00:05:58 -04:00
try {
this.deletePromise = this.folderApiService.delete(this.folder.id);
2018-04-13 00:05:58 -04:00
await this.deletePromise;
2018-10-02 23:09:19 -04:00
this.platformUtilsService.showToast("success", null, this.i18nService.t("deletedFolder"));
2018-04-13 00:05:58 -04:00
this.onDeletedFolder.emit(this.folder);
} catch (e) {
this.logService.error(e);
2018-04-13 00:05:58 -04:00
}
2019-03-06 14:31:32 -05:00
2021-12-16 13:36:21 +01:00
return true;
}
2019-03-06 14:31:32 -05:00
protected async init() {
this.editMode = this.folderId != null;
if (this.editMode) {
this.editMode = true;
this.title = this.i18nService.t("editFolder");
const folder = await this.folderService.get(this.folderId);
this.folder = await folder.decrypt();
} else {
this.title = this.i18nService.t("addFolder");
}
this.formGroup.controls.name.setValue(this.folder.name);
2021-12-16 13:36:21 +01:00
}
2018-04-13 00:05:58 -04:00
}