Skip to content

Commit

Permalink
[PM-14952] - remove Edit button and show restore button when viewing …
Browse files Browse the repository at this point in the history
…item in trash (#12847)

* add restore function to vault item dialog

* update comment

* revert removing of delete button

* use canDeleteCipher$

* put canRestore in class property

* set showRestore after canDeleteCipher is set
  • Loading branch information
jaasen-livefront authored Jan 17, 2025
1 parent 457aa07 commit 3b4eb40
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@
</vault-cipher-form>
</div>
<ng-container bitDialogFooter>
<ng-container *ngIf="showCipherView">
<button *ngIf="showRestore" [bitAction]="restore" bitButton buttonType="primary" type="button">
{{ "restore" | i18n }}
</button>
<ng-container *ngIf="showCipherView && !showRestore">
<button
bitButton
[bitAction]="switchToEdit"
Expand All @@ -53,7 +56,7 @@
form="cipherForm"
buttonType="primary"
#submitBtn
[hidden]="showCipherView"
[hidden]="showCipherView || showRestore"
>
{{ "save" | i18n }}
</button>
Expand All @@ -62,7 +65,7 @@
type="button"
buttonType="secondary"
(click)="cancel()"
*ngIf="!showCipherView"
*ngIf="!showCipherView && !showRestore"
>
{{ "cancel" | i18n }}
</button>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import {
AttachmentDialogResult,
AttachmentsV2Component,
} from "../../individual-vault/attachments-v2.component";
import { RoutedVaultFilterService } from "../../individual-vault/vault-filter/services/routed-vault-filter.service";
import { RoutedVaultFilterModel } from "../../individual-vault/vault-filter/shared/models/routed-vault-filter.model";
import { WebCipherFormGenerationService } from "../../services/web-cipher-form-generation.service";
import { WebVaultPremiumUpgradePromptService } from "../../services/web-premium-upgrade-prompt.service";
import { WebViewPasswordHistoryService } from "../../services/web-view-password-history.service";
Expand Down Expand Up @@ -82,6 +84,11 @@ export interface VaultItemDialogParams {
* If true, the dialog is being opened from the admin console.
*/
isAdminConsoleAction?: boolean;

/**
* Function to restore a cipher from the trash.
*/
restore: (c: CipherView) => Promise<boolean>;
}

export enum VaultItemDialogResult {
Expand All @@ -99,6 +106,11 @@ export enum VaultItemDialogResult {
* The dialog was closed to navigate the user the premium upgrade page.
*/
PremiumUpgrade = "premiumUpgrade",

/**
* A cipher was restored
*/
Restored = "restored",
}

@Component({
Expand All @@ -121,6 +133,7 @@ export enum VaultItemDialogResult {
{ provide: PremiumUpgradePromptService, useClass: WebVaultPremiumUpgradePromptService },
{ provide: ViewPasswordHistoryService, useClass: WebViewPasswordHistoryService },
{ provide: CipherFormGenerationService, useClass: WebCipherFormGenerationService },
RoutedVaultFilterService,
],
})
export class VaultItemDialogComponent implements OnInit, OnDestroy {
Expand Down Expand Up @@ -191,6 +204,20 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy {
),
);

/**
* Determines if the user may restore the item.
* A user may restore items if they have delete permissions and the item is in the trash.
*/
protected async canUserRestore() {
return (
this.filter?.type === "trash" &&
this.cipher?.isDeleted &&
(await firstValueFrom(this.canDeleteCipher$))
);
}

protected showRestore: boolean;

protected get loadingForm() {
return this.loadForm && !this.formReady;
}
Expand Down Expand Up @@ -230,6 +257,8 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy {

protected canDeleteCipher$: Observable<boolean>;

protected filter: RoutedVaultFilterModel;

constructor(
@Inject(DIALOG_DATA) protected params: VaultItemDialogParams,
private dialogRef: DialogRef<VaultItemDialogResult>,
Expand All @@ -246,6 +275,7 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy {
private cipherAuthorizationService: CipherAuthorizationService,
private apiService: ApiService,
private eventCollectionService: EventCollectionService,
private routedVaultFilterService: RoutedVaultFilterService,
) {
this.updateTitle();
}
Expand Down Expand Up @@ -283,6 +313,9 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy {
);
}

this.filter = await firstValueFrom(this.routedVaultFilterService.filter$);

this.showRestore = await this.canUserRestore();
this.performingInitialLoad = false;
}

Expand Down Expand Up @@ -336,6 +369,11 @@ export class VaultItemDialogComponent implements OnInit, OnDestroy {
this._formReadySubject.next();
}

restore = async () => {
await this.params.restore(this.cipher);
this.dialogRef.close(VaultItemDialogResult.Restored);
};

delete = async () => {
if (!this.cipher) {
return;
Expand Down
1 change: 1 addition & 0 deletions apps/web/src/app/vault/individual-vault/vault.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,7 @@ export class VaultComponent implements OnInit, OnDestroy {
mode,
formConfig,
activeCollectionId,
restore: this.restore,
});

const result = await lastValueFrom(this.vaultItemDialogRef.closed);
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/app/vault/org-vault/vault.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ export class VaultComponent implements OnInit, OnDestroy {
disableForm,
activeCollectionId,
isAdminConsoleAction: true,
restore: this.restore,
});

const result = await lastValueFrom(this.vaultItemDialogRef.closed);
Expand Down Expand Up @@ -1033,7 +1034,7 @@ export class VaultComponent implements OnInit, OnDestroy {
});
}

async restore(c: CipherView): Promise<boolean> {
restore = async (c: CipherView): Promise<boolean> => {
if (!c.isDeleted) {
return;
}
Expand Down Expand Up @@ -1064,7 +1065,7 @@ export class VaultComponent implements OnInit, OnDestroy {
} catch (e) {
this.logService.error(e);
}
}
};

async bulkRestore(ciphers: CipherView[]) {
if (
Expand Down

0 comments on commit 3b4eb40

Please sign in to comment.