[PM-6147]Using reactive methods on SendAddEditComponent load (#8013)

* Using SendService get$ on SendAddEditComponent load

* Calling reactive method on loadSend

* Improved code quality on send add edit

* removed unnecessary patch value

* Improved error message on send add-edit.component

* Add comment regarding promise.reject completing the BehaviourSubject

* Corrected send null validation
This commit is contained in:
aj-rosado 2024-03-08 16:25:23 +00:00 committed by GitHub
parent a803b67f4f
commit 7b7a442e4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 41 additions and 23 deletions

View File

@ -1,7 +1,7 @@
import { DatePipe } from "@angular/common"; import { DatePipe } from "@angular/common";
import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from "@angular/core"; import { Directive, EventEmitter, Input, OnDestroy, OnInit, Output } from "@angular/core";
import { FormBuilder, Validators } from "@angular/forms"; import { FormBuilder, Validators } from "@angular/forms";
import { Subject, map, takeUntil } from "rxjs"; import { BehaviorSubject, Subject, concatMap, firstValueFrom, map, takeUntil } from "rxjs";
import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction"; import { PolicyService } from "@bitwarden/common/admin-console/abstractions/policy/policy.service.abstraction";
import { PolicyType } from "@bitwarden/common/admin-console/enums"; import { PolicyType } from "@bitwarden/common/admin-console/enums";
@ -167,7 +167,7 @@ export class AddEditComponent implements OnInit, OnDestroy {
} }
}); });
this.formGroup.controls.type.valueChanges.subscribe((val) => { this.formGroup.controls.type.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((val) => {
this.type = val; this.type = val;
this.typeChanged(); this.typeChanged();
}); });
@ -210,28 +210,46 @@ export class AddEditComponent implements OnInit, OnDestroy {
this.type = !this.canAccessPremium || !this.emailVerified ? SendType.Text : SendType.File; this.type = !this.canAccessPremium || !this.emailVerified ? SendType.Text : SendType.File;
if (this.send == null) { if (this.send == null) {
if (this.editMode) { const send = new BehaviorSubject<SendView>(this.send);
const send = this.loadSend(); send.subscribe({
this.send = await send.decrypt(); next: (decryptedSend: SendView | undefined) => {
if (!(decryptedSend instanceof SendView)) {
return;
}
this.send = decryptedSend;
decryptedSend.type = decryptedSend.type ?? this.type;
this.type = this.send.type; this.type = this.send.type;
this.updateFormValues(); this.updateFormValues();
} else {
this.send = new SendView();
this.send.type = this.type;
this.send.file = new SendFileView();
this.send.text = new SendTextView();
this.send.deletionDate = new Date();
this.send.deletionDate.setDate(this.send.deletionDate.getDate() + 7);
this.formGroup.controls.type.patchValue(this.send.type);
this.formGroup.patchValue({
selectedDeletionDatePreset: DatePreset.SevenDays,
selectedExpirationDatePreset: DatePreset.Never,
});
}
}
this.hasPassword = this.send.password != null && this.send.password.trim() !== ""; this.hasPassword = this.send.password != null && this.send.password.trim() !== "";
},
error: (error: unknown) => {
const errorMessage = (error as Error).message ?? "An unknown error occurred";
this.logService.error("Failed to decrypt send: " + errorMessage);
},
});
if (this.editMode) {
this.sendService
.get$(this.sendId)
.pipe(
//Promise.reject will complete the BehaviourSubject, if desktop starts relying only on BehaviourSubject, this should be changed.
concatMap((s) =>
s instanceof Send ? s.decrypt() : Promise.reject(new Error("Failed to load send.")),
),
takeUntil(this.destroy$),
)
.subscribe(send);
} else {
const sendView = new SendView();
sendView.type = this.type;
sendView.file = new SendFileView();
sendView.text = new SendTextView();
sendView.deletionDate = new Date();
sendView.deletionDate.setDate(sendView.deletionDate.getDate() + 7);
send.next(sendView);
}
}
} }
async submit(): Promise<boolean> { async submit(): Promise<boolean> {
@ -376,8 +394,8 @@ export class AddEditComponent implements OnInit, OnDestroy {
this.showOptions = !this.showOptions; this.showOptions = !this.showOptions;
} }
protected loadSend(): Send { protected loadSend(): Promise<Send> {
return this.sendService.get(this.sendId); return firstValueFrom(this.sendService.get$(this.sendId));
} }
protected async encryptSend(file: File): Promise<[Send, EncArrayBuffer]> { protected async encryptSend(file: File): Promise<[Send, EncArrayBuffer]> {