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 , private validationService , private logService ) { } ngOnChanges ( changes ) { if ( this . appApiAction == null || this . appApiAction . then == null ) { return ; } this . el . nativeElement . loading = true ; this . appApiAction . then ( ( response ) => { this . el . nativeElement . loading = false ; } , ( e ) => { this . el . nativeElement . loading = false ; if ( ( e instanceof ErrorResponse || e . constructor . name === "ErrorResponse" ) && ( e as ErrorResponse ) . captchaRequired ) { this . logService . error ( "Captcha required error response: " + e . getSingleMessage ( ) ) ; return ; } this . logService ?. error ( `Received API exception: ${ e }` ) ; this . validationService . showError ( e ) ; } ) ; } }