bitwarden-estensione-browser/src/app/vault/folder-add-edit.component.ts

87 lines
3.0 KiB
TypeScript
Raw Normal View History

2018-01-30 21:40:06 +01:00
import * as template from './folder-add-edit.component.html';
import {
Component,
EventEmitter,
Input,
OnInit,
Output,
} from '@angular/core';
import { Angulartics2 } from 'angulartics2';
import { ToasterService } from 'angular2-toaster';
import { FolderService } from 'jslib/abstractions/folder.service';
import { I18nService } from 'jslib/abstractions/i18n.service';
2018-02-03 05:42:33 +01:00
import { PlatformUtilsService } from 'jslib/abstractions/platformUtils.service';
2018-01-30 21:40:06 +01:00
import { FolderView } from 'jslib/models/view/folderView';
@Component({
selector: 'app-folder-add-edit',
template: template,
})
export class FolderAddEditComponent implements OnInit {
@Input() folderId: string;
@Output() onSavedFolder = new EventEmitter<FolderView>();
@Output() onDeletedFolder = new EventEmitter<FolderView>();
editMode: boolean = false;
folder: FolderView = new FolderView();
2018-01-30 21:58:58 +01:00
title: string;
2018-01-31 18:52:12 +01:00
formPromise: Promise<any>;
deletePromise: Promise<any>;
2018-01-30 21:40:06 +01:00
constructor(private folderService: FolderService, private i18nService: I18nService,
2018-02-03 05:42:33 +01:00
private analytics: Angulartics2, private toasterService: ToasterService,
private platformUtilsService: PlatformUtilsService) { }
2018-01-30 21:40:06 +01:00
async ngOnInit() {
this.editMode = this.folderId != null;
if (this.editMode) {
this.editMode = true;
2018-01-30 21:58:58 +01:00
this.title = this.i18nService.t('editFolder');
2018-01-30 21:40:06 +01:00
const folder = await this.folderService.get(this.folderId);
this.folder = await folder.decrypt();
2018-01-30 21:58:58 +01:00
} else {
this.title = this.i18nService.t('addFolder');
2018-01-30 21:40:06 +01:00
}
}
2018-01-31 18:52:12 +01:00
async submit() {
2018-01-30 21:40:06 +01:00
if (this.folder.name == null || this.folder.name === '') {
this.toasterService.popAsync('error', this.i18nService.t('errorOccurred'),
this.i18nService.t('nameRequired'));
return;
}
2018-01-31 18:52:12 +01:00
try {
const folder = await this.folderService.encrypt(this.folder);
this.formPromise = this.folderService.saveWithServer(folder);;
await this.formPromise;
this.analytics.eventTrack.next({ action: this.editMode ? 'Edited Folder' : 'Added Folder' });
this.toasterService.popAsync('success', null,
this.i18nService.t(this.editMode ? 'editedFolder' : 'addedFolder'));
this.onSavedFolder.emit(this.folder);
} catch { }
2018-01-30 21:40:06 +01:00
}
async delete() {
2018-02-03 05:42:33 +01:00
const confirmed = await this.platformUtilsService.showDialog(
this.i18nService.t('deleteFolderConfirmation'), this.i18nService.t('deleteFolder'),
this.i18nService.t('yes'), this.i18nService.t('no'), 'warning')
if (!confirmed) {
2018-01-30 21:40:06 +01:00
return;
}
2018-01-31 18:52:12 +01:00
try {
this.deletePromise = this.folderService.deleteWithServer(this.folder.id);
await this.deletePromise;
this.analytics.eventTrack.next({ action: 'Deleted Folder' });
this.toasterService.popAsync('success', null, this.i18nService.t('deletedFolder'));
this.onDeletedFolder.emit(this.folder);
} catch { }
2018-01-30 21:40:06 +01:00
}
}