From 6d7a96d8dfb3006bd0e102b01ac4f88f00380768 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pau=20Ferrer=20Oca=C3=B1a?= Date: Tue, 29 Sep 2020 14:40:51 +0200 Subject: [PATCH] MOBILE-3200 database: Do not need courseId to delete an offline entry --- src/addon/mod/data/providers/helper.ts | 73 ++++++++++++++++---------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/addon/mod/data/providers/helper.ts b/src/addon/mod/data/providers/helper.ts index ea686e422..eaf9a2b5d 100644 --- a/src/addon/mod/data/providers/helper.ts +++ b/src/addon/mod/data/providers/helper.ts @@ -18,7 +18,6 @@ import { CoreEventsProvider } from '@providers/events'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; import { CoreTextUtilsProvider } from '@providers/utils/text'; -import { CoreUtilsProvider } from '@providers/utils/utils'; import { CoreCourseProvider } from '@core/course/providers/course'; import { CoreFileUploaderProvider } from '@core/fileuploader/providers/fileuploader'; import { AddonModDataFieldsDelegate } from './fields-delegate'; @@ -35,12 +34,19 @@ import { CoreRatingOfflineProvider } from '@core/rating/providers/offline'; @Injectable() export class AddonModDataHelperProvider { - constructor(private sitesProvider: CoreSitesProvider, protected dataProvider: AddonModDataProvider, - private translate: TranslateService, private fieldsDelegate: AddonModDataFieldsDelegate, - private dataOffline: AddonModDataOfflineProvider, private fileUploaderProvider: CoreFileUploaderProvider, - private textUtils: CoreTextUtilsProvider, private eventsProvider: CoreEventsProvider, private utils: CoreUtilsProvider, - private domUtils: CoreDomUtilsProvider, private courseProvider: CoreCourseProvider, - private ratingOffline: CoreRatingOfflineProvider) {} + constructor( + protected sitesProvider: CoreSitesProvider, + protected dataProvider: AddonModDataProvider, + protected translate: TranslateService, + protected fieldsDelegate: AddonModDataFieldsDelegate, + protected dataOffline: AddonModDataOfflineProvider, + protected fileUploaderProvider: CoreFileUploaderProvider, + protected textUtils: CoreTextUtilsProvider, + protected eventsProvider: CoreEventsProvider, + protected domUtils: CoreDomUtilsProvider, + protected courseProvider: CoreCourseProvider, + protected ratingOffline: CoreRatingOfflineProvider + ) {} /** * Returns the record with the offline actions applied. @@ -632,35 +638,44 @@ export class AddonModDataHelperProvider { * @param courseId Course ID. It not defined, it will be fetched. * @param siteId Site ID. If not defined, current site. */ - showDeleteEntryModal(dataId: number, entryId: number, courseId?: number, siteId?: string): void { + async showDeleteEntryModal(dataId: number, entryId: number, courseId?: number, siteId?: string): Promise { siteId = siteId || this.sitesProvider.getCurrentSiteId(); - this.domUtils.showDeleteConfirm('addon.mod_data.confirmdeleterecord').then(() => { - const modal = this.domUtils.showModalLoading(); + let modal; + try { + await this.domUtils.showDeleteConfirm('addon.mod_data.confirmdeleterecord'); - return this.getActivityCourseIdIfNotSet(dataId, courseId, siteId).then((courseId) => { - return this.dataProvider.deleteEntry(dataId, entryId, courseId, siteId); - }).catch((message) => { + modal = this.domUtils.showModalLoading(); + + try { + if (entryId > 0) { + courseId = await this.getActivityCourseIdIfNotSet(dataId, courseId, siteId); + } + + this.dataProvider.deleteEntry(dataId, entryId, courseId, siteId); + } catch (message) { this.domUtils.showErrorModalDefault(message, 'addon.mod_data.errordeleting', true); - return Promise.reject(null); - }).then(() => { - return this.utils.allPromises([ - this.dataProvider.invalidateEntryData(dataId, entryId, siteId), - this.dataProvider.invalidateEntriesData(dataId, siteId) - ]).catch(() => { - // Ignore errors. - }); - }).then(() => { - this.eventsProvider.trigger(AddonModDataProvider.ENTRY_CHANGED, {dataId, entryId, deleted: true}, siteId); + modal && modal.dismiss(); - this.domUtils.showToast('addon.mod_data.recorddeleted', true, 3000); - }).finally(() => { - modal.dismiss(); - }); - }).catch(() => { + return; + } + + try { + await this.dataProvider.invalidateEntryData(dataId, entryId, siteId); + await this.dataProvider.invalidateEntriesData(dataId, siteId); + } catch (error) { + // Ignore errors. + } + + this.eventsProvider.trigger(AddonModDataProvider.ENTRY_CHANGED, {dataId, entryId, deleted: true}, siteId); + + this.domUtils.showToast('addon.mod_data.recorddeleted', true, 3000); + } catch (error) { // Ignore error, it was already displayed. - }); + } + + modal && modal.dismiss(); } /**