bitwarden-estensione-browser/angular/src/directives/api-action.directive.ts

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

47 lines
1.3 KiB
TypeScript
Raw Normal View History

import { Directive, ElementRef, Input, OnChanges } from "@angular/core";
import { LogService } from "jslib-common/abstractions/log.service";
import { ErrorResponse } from "jslib-common/models/response/errorResponse";
import { ValidationService } from "../services/validation.service";
@Directive({
selector: "[appApiAction]",
})
export class ApiActionDirective implements OnChanges {
@Input() appApiAction: Promise<any>;
constructor(
private el: ElementRef,
private validationService: ValidationService,
private logService: LogService
) {}
ngOnChanges(changes: any) {
if (this.appApiAction == null || this.appApiAction.then == null) {
return;
}
2021-12-16 13:36:21 +01:00
this.el.nativeElement.loading = true;
2021-12-16 13:36:21 +01:00
this.appApiAction.then(
(response: any) => {
this.el.nativeElement.loading = false;
2021-12-16 13:36:21 +01:00
},
(e: any) => {
this.el.nativeElement.loading = false;
2021-12-16 13:36:21 +01:00
if (
(e instanceof ErrorResponse || e.constructor.name === "ErrorResponse") &&
(e as ErrorResponse).captchaRequired
2021-12-16 13:36:21 +01:00
) {
this.logService.error("Captcha required error response: " + e.getSingleMessage());
2021-12-16 13:36:21 +01:00
return;
}
this.logService?.error(`Received API exception: ${e}`);
this.validationService.showError(e);
2021-12-16 13:36:21 +01:00
}
);
}
}