diff --git a/src/core/siteplugins/classes/call-ws-click-directive.ts b/src/core/siteplugins/classes/call-ws-click-directive.ts index 7f535eaad..c6c598b53 100644 --- a/src/core/siteplugins/classes/call-ws-click-directive.ts +++ b/src/core/siteplugins/classes/call-ws-click-directive.ts @@ -15,6 +15,7 @@ import { Input, OnInit, ElementRef } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreSitePluginsProvider } from '../providers/siteplugins'; import { CoreSitePluginsPluginContentComponent } from '../components/plugin-content/plugin-content'; import { CoreSitePluginsCallWSBaseDirective } from './call-ws-directive'; @@ -28,10 +29,11 @@ import { CoreSitePluginsCallWSBaseDirective } from './call-ws-directive'; */ export class CoreSitePluginsCallWSOnClickBaseDirective extends CoreSitePluginsCallWSBaseDirective implements OnInit { @Input() confirmMessage: string; // Message to confirm the action. If not supplied, no confirmation. If empty, default message. + @Input() showError: boolean | string; // Whether to show an error message if the WS call fails. Defaults to true. constructor(element: ElementRef, protected translate: TranslateService, protected domUtils: CoreDomUtilsProvider, protected sitePluginsProvider: CoreSitePluginsProvider, - protected parentContent: CoreSitePluginsPluginContentComponent) { + protected parentContent: CoreSitePluginsPluginContentComponent, protected utils: CoreUtilsProvider) { super(element, translate, domUtils, sitePluginsProvider, parentContent); } @@ -66,7 +68,11 @@ export class CoreSitePluginsCallWSOnClickBaseDirective extends CoreSitePluginsCa protected callWS(): Promise { const modal = this.domUtils.showModalLoading(); - return super.callWS().finally(() => { + return super.callWS().catch((error) => { + if (typeof this.showError == 'undefined' || this.utils.isTrueOrOne(this.showError)) { + this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true); + } + }).finally(() => { modal.dismiss(); }); } diff --git a/src/core/siteplugins/classes/call-ws-directive.ts b/src/core/siteplugins/classes/call-ws-directive.ts index 8d7dedc4b..8716afdd0 100644 --- a/src/core/siteplugins/classes/call-ws-directive.ts +++ b/src/core/siteplugins/classes/call-ws-directive.ts @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { Input, OnInit, OnDestroy, ElementRef } from '@angular/core'; +import { Input, OnInit, OnDestroy, ElementRef, Output, EventEmitter } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreSitePluginsProvider } from '../providers/siteplugins'; @@ -30,6 +30,9 @@ export class CoreSitePluginsCallWSBaseDirective implements OnInit, OnDestroy { // @see CoreSitePluginsProvider.loadOtherDataInArgs. @Input() form: string; // ID or name to identify a form. The form will be obtained from document.forms. // If supplied and form is found, the form data will be retrieved and sent to the WS. + @Output() onSuccess: EventEmitter = new EventEmitter(); // Sends the result when the WS call succeeds. + @Output() onError: EventEmitter = new EventEmitter(); // Sends the error when the WS call fails. + @Output() onDone: EventEmitter = new EventEmitter(); // Notifies when the WS call is done (either success or fail). protected element: HTMLElement; protected invalidateObserver: Subscription; @@ -60,9 +63,15 @@ export class CoreSitePluginsCallWSBaseDirective implements OnInit, OnDestroy { const params = this.getParamsForWS(); return this.sitePluginsProvider.callWS(this.name, params, this.preSets).then((result) => { + this.onSuccess.emit(result); + return this.wsCallSuccess(result); }).catch((error) => { - this.domUtils.showErrorModalDefault(error, 'core.serverconnection', true); + this.onError.emit(error); + + return Promise.reject(error); + }).finally(() => { + this.onDone.emit(); }); } diff --git a/src/core/siteplugins/directives/call-ws-new-content.ts b/src/core/siteplugins/directives/call-ws-new-content.ts index 9d67ccba0..9117b831e 100644 --- a/src/core/siteplugins/directives/call-ws-new-content.ts +++ b/src/core/siteplugins/directives/call-ws-new-content.ts @@ -62,8 +62,8 @@ export class CoreSitePluginsCallWSNewContentDirective extends CoreSitePluginsCal constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider, sitePluginsProvider: CoreSitePluginsProvider, @Optional() parentContent: CoreSitePluginsPluginContentComponent, - protected utils: CoreUtilsProvider, @Optional() protected navCtrl: NavController) { - super(element, translate, domUtils, sitePluginsProvider, parentContent); + utils: CoreUtilsProvider, @Optional() protected navCtrl: NavController) { + super(element, translate, domUtils, sitePluginsProvider, parentContent, utils); } /** diff --git a/src/core/siteplugins/directives/call-ws.ts b/src/core/siteplugins/directives/call-ws.ts index 7880ad64a..9a0742ed2 100644 --- a/src/core/siteplugins/directives/call-ws.ts +++ b/src/core/siteplugins/directives/call-ws.ts @@ -56,8 +56,8 @@ export class CoreSitePluginsCallWSDirective extends CoreSitePluginsCallWSOnClick constructor(element: ElementRef, translate: TranslateService, domUtils: CoreDomUtilsProvider, sitePluginsProvider: CoreSitePluginsProvider, @Optional() parentContent: CoreSitePluginsPluginContentComponent, - protected utils: CoreUtilsProvider, protected navCtrl: NavController) { - super(element, translate, domUtils, sitePluginsProvider, parentContent); + utils: CoreUtilsProvider, protected navCtrl: NavController) { + super(element, translate, domUtils, sitePluginsProvider, parentContent, utils); } /**