2018-04-04 14:22:55 +02:00
|
|
|
import {
|
|
|
|
Directive,
|
|
|
|
ElementRef,
|
|
|
|
Input,
|
|
|
|
OnChanges,
|
|
|
|
} from '@angular/core';
|
2021-07-22 19:28:45 +02:00
|
|
|
import { LogService } from 'jslib-common/abstractions/log.service';
|
|
|
|
|
|
|
|
import { ErrorResponse } from 'jslib-common/models/response';
|
2018-04-04 14:22:55 +02:00
|
|
|
|
|
|
|
import { ValidationService } from '../services/validation.service';
|
|
|
|
|
|
|
|
@Directive({
|
|
|
|
selector: '[appApiAction]',
|
|
|
|
})
|
|
|
|
export class ApiActionDirective implements OnChanges {
|
|
|
|
@Input() appApiAction: Promise<any>;
|
|
|
|
|
2021-07-22 19:28:45 +02:00
|
|
|
constructor(private el: ElementRef, private validationService: ValidationService,
|
|
|
|
private logService: LogService) { }
|
2018-04-04 14:22:55 +02:00
|
|
|
|
|
|
|
ngOnChanges(changes: any) {
|
|
|
|
if (this.appApiAction == null || this.appApiAction.then == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.el.nativeElement.loading = true;
|
|
|
|
|
|
|
|
this.appApiAction.then((response: any) => {
|
|
|
|
this.el.nativeElement.loading = false;
|
|
|
|
}, (e: any) => {
|
|
|
|
this.el.nativeElement.loading = false;
|
2021-07-22 19:28:45 +02:00
|
|
|
|
2021-07-23 18:47:06 +02:00
|
|
|
if ((e instanceof ErrorResponse || e.constructor.name === 'ErrorResponse') && (e as ErrorResponse).captchaRequired) {
|
2021-07-22 19:28:45 +02:00
|
|
|
this.logService.error('Captcha required error response: ' + e.getSingleMessage());
|
|
|
|
return;
|
|
|
|
}
|
2018-04-04 14:22:55 +02:00
|
|
|
this.validationService.showError(e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|