diff --git a/src/addon/mod/data/pages/edit/edit.ts b/src/addon/mod/data/pages/edit/edit.ts index b3542eebf..72e56a5ac 100644 --- a/src/addon/mod/data/pages/edit/edit.ts +++ b/src/addon/mod/data/pages/edit/edit.ts @@ -52,6 +52,8 @@ export class AddonModDataEditPage { protected siteId: string; protected offline: boolean; protected forceLeave = false; // To allow leaving the page without checking for changes. + protected initialSelectedGroup = null; + protected isEditing = false; title = ''; component = AddonModDataProvider.COMPONENT; @@ -75,7 +77,10 @@ export class AddonModDataEditPage { this.module = params.get('module') || {}; this.entryId = params.get('entryId') || null; this.courseId = params.get('courseId'); - this.selectedGroup = params.get('group') || 0; + this.selectedGroup = this.entryId ? null : (params.get('group') || 0); + + // If entryId is lower than 0 or null, it is a new entry or an offline entry. + this.isEditing = this.entryId && this.entryId > 0; this.siteId = sitesProvider.getCurrentSiteId(); @@ -103,7 +108,8 @@ export class AddonModDataEditPage { const inputData = this.editForm.value; - const changed = await this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents); + let changed = await this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents); + changed = changed || (!this.isEditing && this.initialSelectedGroup != this.selectedGroup); if (changed) { // Show confirmation if some data has been modified. @@ -169,6 +175,7 @@ export class AddonModDataEditPage { return this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents).then((changed) => { + changed = changed || (!this.isEditing && this.initialSelectedGroup != this.selectedGroup); if (!changed) { if (this.entryId) { return this.returnToEntryList(); @@ -196,7 +203,7 @@ export class AddonModDataEditPage { return Promise.reject(e); }).then((editData) => { if (editData.length > 0) { - if (this.entryId) { + if (this.isEditing) { return this.dataProvider.editEntry(this.data.id, this.entryId, this.courseId, editData, this.fields, undefined, this.offline); } @@ -213,20 +220,20 @@ export class AddonModDataEditPage { } // This is done if entry is updated when editing or creating if not. - if ((this.entryId && result.updated) || (!this.entryId && result.newentryid)) { + if ((this.isEditing && result.updated) || (!this.isEditing && result.newentryid)) { this.domUtils.triggerFormSubmittedEvent(this.formElement, result.sent, this.siteId); - if (result.sent) { - this.eventsProvider.trigger(CoreEventsProvider.ACTIVITY_DATA_SENT, { module: 'data' }); - } - const promises = []; - this.entryId = this.entryId || result.newentryid; + if (result.sent) { + this.eventsProvider.trigger(CoreEventsProvider.ACTIVITY_DATA_SENT, { module: 'data' }); - promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId, this.siteId)); - promises.push(this.dataProvider.invalidateEntriesData(this.data.id, this.siteId)); + if (this.isEditing) { + promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId, this.siteId)); + } + promises.push(this.dataProvider.invalidateEntriesData(this.data.id, this.siteId)); + } return Promise.all(promises).then(() => { this.eventsProvider.trigger(AddonModDataProvider.ENTRY_CHANGED, @@ -264,7 +271,7 @@ export class AddonModDataEditPage { * @param groupId Group identifier to set. * @return Resolved when done. */ - setGroup(groupId: number): Promise { + setGroup(groupId: number): Promise { this.selectedGroup = groupId; this.loaded = false; @@ -322,7 +329,7 @@ export class AddonModDataEditPage { * * @return Resolved when done. */ - protected returnToEntryList(): Promise { + protected returnToEntryList(): Promise { const inputData = this.editForm.value; return this.dataHelper.getEditTmpFiles(inputData, this.fieldsArray, this.data.id, diff --git a/src/addon/mod/data/providers/data.ts b/src/addon/mod/data/providers/data.ts index cde86d294..6fd85d2db 100644 --- a/src/addon/mod/data/providers/data.ts +++ b/src/addon/mod/data/providers/data.ts @@ -133,6 +133,7 @@ export class AddonModDataProvider { }); }; + // Checks to store offline. if (!this.appProvider.isOnline() || forceOffline) { const notifications = this.checkFields(fields, contents); if (notifications) { @@ -140,22 +141,40 @@ export class AddonModDataProvider { fieldnotifications: notifications }); } - - return storeOffline(); } - return this.addEntryOnline(dataId, contents, groupId, siteId).then((result) => { - result.sent = true; + // Get other not synced actions. + return this.dataOffline.getEntryActions(dataId, entryId, siteId).then((entries) => { + if (entries && entries.length) { + // Found. Delete add and edit actions first. + const proms = []; + entries.forEach((entry) => { + if (entry.action == 'add') { + proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId)); + } + }); - return result; - }).catch((error) => { - if (this.utils.isWebServiceError(error)) { - // The WebService has thrown an error, this means that responses cannot be submitted. - return Promise.reject(error); + return Promise.all(proms); + } + }).then(() => { + // App is offline, store the action. + if (!this.appProvider.isOnline() || forceOffline) { + return storeOffline(); } - // Couldn't connect to server, store in offline. - return storeOffline(); + return this.addEntryOnline(dataId, contents, groupId, siteId).then((result) => { + result.sent = true; + + return result; + }).catch((error) => { + if (this.utils.isWebServiceError(error)) { + // The WebService has thrown an error, this means that responses cannot be submitted. + return Promise.reject(error); + } + + // Couldn't connect to server, store in offline. + return storeOffline(); + }); }); } @@ -398,9 +417,6 @@ export class AddonModDataProvider { }); }; - let justAdded = false, - groupId; - if (!this.appProvider.isOnline() || forceOffline) { const notifications = this.checkFields(fields, contents); if (notifications) { @@ -416,11 +432,7 @@ export class AddonModDataProvider { // Found. Delete add and edit actions first. const proms = []; entries.forEach((entry) => { - if (entry.action == 'add') { - justAdded = true; - groupId = entry.groupid; - proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId)); - } else if (entry.action == 'edit') { + if (entry.action == 'edit') { proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId)); } }); @@ -428,17 +440,6 @@ export class AddonModDataProvider { return Promise.all(proms); } }).then(() => { - if (justAdded) { - // The field was added offline, add again and stop. - return this.addEntry(dataId, entryId, courseId, contents, groupId, fields, siteId, forceOffline) - .then((result) => { - result.updated = true; - result.sent = true; - - return result; - }); - } - if (!this.appProvider.isOnline() || forceOffline) { // App is offline, store the action. return storeOffline();