mirror of
https://github.com/bitwarden/browser.git
synced 2024-09-27 04:03:00 +02:00
9f7bf1132b
* Bug fix: "vaule" -> "value" * Bug fix: "aria-descibedby" -> "aria-describedby" * Bug fix: "chararacter" -> "character" * Fix typos in comments * Fix typos in documentation * Fix typo in test description * Fix typos in sample data: "childen" -> "children" * Fix typos in sample data: "pargraphs" -> "paragraphs" * Fixes to test data: "Additinoal", "Informaion" -> "Additional", "Information" * Fix typo in test data: "dolhpin" -> "dolphin" * Fix typo in local variable: "attachement" -> "attachment" * Fix typo in method name: "detachOrganizastion" -> "detachOrganization" * Fix typo in method name: "getNewlyAddedDomians" -> "getNewlyAddedDomains" * Fix typo: "EncyptedMessageResponse" -> "EncryptedMessageResponse" * Fix typo: "miliseconds" -> "milliseconds" * Fix typo: "authResponsePushNotifiction" -> "authResponsePushNotification" * Fix typo: "getPushNotifcationObs" -> "getPushNotificationObs" * Fix typo: "ExpriationDate" -> "ExpirationDate" * Fix typo: "OrganizationUserResetPasswordDetailsReponse" -> "OrganizationUserResetPasswordDetailsResponse" * Fix typo: "DISPLAY_TITLE_ATTRIBUE" -> "DISPLAY_TITLE_ATTRIBUTE" * Fix typo: "credentialretreivalCommandHandler" -> "credentialRetrievalCommandHandler" * Fix typo: "buildLoginCredntials" -> "buildLoginCredentials" * Fix typo: "_mappedCredentialsColums" -> "_mappedCredentialsColumns" * Fix typo: "_mappedPersonalInfoAsIdentiyColumns" -> "_mappedPersonalInfoAsIdentityColumns" * Fix typo in input name: "StroageGbAdjustment" -> "StorageGbAdjustment" * Fix typo in const: "encryptionAlogrithm" -> "encryptionAlgorithm" --------- Co-authored-by: Daniel James Smith <djsmith@web.de>
92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
import { Directive, Input, OnDestroy, OnInit, Optional } from "@angular/core";
|
|
import { FormGroupDirective } from "@angular/forms";
|
|
import { BehaviorSubject, catchError, filter, of, Subject, switchMap, takeUntil } from "rxjs";
|
|
|
|
import { LogService } from "@bitwarden/common/abstractions/log.service";
|
|
import { ValidationService } from "@bitwarden/common/abstractions/validation.service";
|
|
|
|
import { FunctionReturningAwaitable, functionToObservable } from "../utils/function-to-observable";
|
|
|
|
/**
|
|
* Allow a form to perform async actions on submit, disabling the form while the action is processing.
|
|
*/
|
|
@Directive({
|
|
selector: "[formGroup][bitSubmit]",
|
|
})
|
|
export class BitSubmitDirective implements OnInit, OnDestroy {
|
|
private destroy$ = new Subject<void>();
|
|
private _loading$ = new BehaviorSubject<boolean>(false);
|
|
private _disabled$ = new BehaviorSubject<boolean>(false);
|
|
|
|
@Input("bitSubmit") protected handler: FunctionReturningAwaitable;
|
|
|
|
@Input() allowDisabledFormSubmit?: boolean = false;
|
|
|
|
readonly loading$ = this._loading$.asObservable();
|
|
readonly disabled$ = this._disabled$.asObservable();
|
|
|
|
constructor(
|
|
private formGroupDirective: FormGroupDirective,
|
|
@Optional() validationService?: ValidationService,
|
|
@Optional() logService?: LogService
|
|
) {
|
|
formGroupDirective.ngSubmit
|
|
.pipe(
|
|
filter(() => !this.disabled),
|
|
switchMap(() => {
|
|
// Calling functionToObservable executes the sync part of the handler
|
|
// allowing the function to check form validity before it gets disabled.
|
|
const awaitable = functionToObservable(this.handler);
|
|
|
|
// Disable form
|
|
this.loading = true;
|
|
|
|
return awaitable.pipe(
|
|
catchError((err: unknown) => {
|
|
logService?.error(`Async submit exception: ${err}`);
|
|
validationService?.showError(err);
|
|
return of(undefined);
|
|
})
|
|
);
|
|
}),
|
|
takeUntil(this.destroy$)
|
|
)
|
|
.subscribe({
|
|
next: () => (this.loading = false),
|
|
complete: () => (this.loading = false),
|
|
});
|
|
}
|
|
|
|
ngOnInit(): void {
|
|
this.formGroupDirective.statusChanges.pipe(takeUntil(this.destroy$)).subscribe((c) => {
|
|
if (this.allowDisabledFormSubmit) {
|
|
this._disabled$.next(false);
|
|
} else {
|
|
this._disabled$.next(c === "DISABLED");
|
|
}
|
|
});
|
|
}
|
|
|
|
get disabled() {
|
|
return this._disabled$.value;
|
|
}
|
|
|
|
set disabled(value: boolean) {
|
|
this._disabled$.next(value);
|
|
}
|
|
|
|
get loading() {
|
|
return this._loading$.value;
|
|
}
|
|
|
|
set loading(value: boolean) {
|
|
this.disabled = value;
|
|
this._loading$.next(value);
|
|
}
|
|
|
|
ngOnDestroy(): void {
|
|
this.destroy$.next();
|
|
this.destroy$.complete();
|
|
}
|
|
}
|