Skip to content

Commit b0b8062

Browse files
committed
MOBILE-3200 database: Improve add/edit offline action handling
1 parent 6d7a96d commit b0b8062

File tree

2 files changed

+51
-43
lines changed

2 files changed

+51
-43
lines changed

src/addon/mod/data/pages/edit/edit.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export class AddonModDataEditPage {
5252
protected siteId: string;
5353
protected offline: boolean;
5454
protected forceLeave = false; // To allow leaving the page without checking for changes.
55+
protected initialSelectedGroup = null;
56+
protected isEditing = false;
5557

5658
title = '';
5759
component = AddonModDataProvider.COMPONENT;
@@ -75,7 +77,10 @@ export class AddonModDataEditPage {
7577
this.module = params.get('module') || {};
7678
this.entryId = params.get('entryId') || null;
7779
this.courseId = params.get('courseId');
78-
this.selectedGroup = params.get('group') || 0;
80+
this.selectedGroup = this.entryId ? null : (params.get('group') || 0);
81+
82+
// If entryId is lower than 0 or null, it is a new entry or an offline entry.
83+
this.isEditing = this.entryId && this.entryId > 0;
7984

8085
this.siteId = sitesProvider.getCurrentSiteId();
8186

@@ -103,7 +108,8 @@ export class AddonModDataEditPage {
103108

104109
const inputData = this.editForm.value;
105110

106-
const changed = await this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents);
111+
let changed = await this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id, this.entry.contents);
112+
changed = changed || (!this.isEditing && this.initialSelectedGroup != this.selectedGroup);
107113

108114
if (changed) {
109115
// Show confirmation if some data has been modified.
@@ -169,6 +175,7 @@ export class AddonModDataEditPage {
169175
return this.dataHelper.hasEditDataChanged(inputData, this.fieldsArray, this.data.id,
170176
this.entry.contents).then((changed) => {
171177

178+
changed = changed || (!this.isEditing && this.initialSelectedGroup != this.selectedGroup);
172179
if (!changed) {
173180
if (this.entryId) {
174181
return this.returnToEntryList();
@@ -196,7 +203,7 @@ export class AddonModDataEditPage {
196203
return Promise.reject(e);
197204
}).then((editData) => {
198205
if (editData.length > 0) {
199-
if (this.entryId) {
206+
if (this.isEditing) {
200207
return this.dataProvider.editEntry(this.data.id, this.entryId, this.courseId, editData, this.fields,
201208
undefined, this.offline);
202209
}
@@ -213,20 +220,20 @@ export class AddonModDataEditPage {
213220
}
214221

215222
// This is done if entry is updated when editing or creating if not.
216-
if ((this.entryId && result.updated) || (!this.entryId && result.newentryid)) {
223+
if ((this.isEditing && result.updated) || (!this.isEditing && result.newentryid)) {
217224

218225
this.domUtils.triggerFormSubmittedEvent(this.formElement, result.sent, this.siteId);
219226

220-
if (result.sent) {
221-
this.eventsProvider.trigger(CoreEventsProvider.ACTIVITY_DATA_SENT, { module: 'data' });
222-
}
223-
224227
const promises = [];
225228

226-
this.entryId = this.entryId || result.newentryid;
229+
if (result.sent) {
230+
this.eventsProvider.trigger(CoreEventsProvider.ACTIVITY_DATA_SENT, { module: 'data' });
227231

228-
promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId, this.siteId));
229-
promises.push(this.dataProvider.invalidateEntriesData(this.data.id, this.siteId));
232+
if (this.isEditing) {
233+
promises.push(this.dataProvider.invalidateEntryData(this.data.id, this.entryId, this.siteId));
234+
}
235+
promises.push(this.dataProvider.invalidateEntriesData(this.data.id, this.siteId));
236+
}
230237

231238
return Promise.all(promises).then(() => {
232239
this.eventsProvider.trigger(AddonModDataProvider.ENTRY_CHANGED,
@@ -264,7 +271,7 @@ export class AddonModDataEditPage {
264271
* @param groupId Group identifier to set.
265272
* @return Resolved when done.
266273
*/
267-
setGroup(groupId: number): Promise<any> {
274+
setGroup(groupId: number): Promise<void> {
268275
this.selectedGroup = groupId;
269276
this.loaded = false;
270277

@@ -322,7 +329,7 @@ export class AddonModDataEditPage {
322329
*
323330
* @return Resolved when done.
324331
*/
325-
protected returnToEntryList(): Promise<any> {
332+
protected returnToEntryList(): Promise<void> {
326333
const inputData = this.editForm.value;
327334

328335
return this.dataHelper.getEditTmpFiles(inputData, this.fieldsArray, this.data.id,

src/addon/mod/data/providers/data.ts

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,29 +133,48 @@ export class AddonModDataProvider {
133133
});
134134
};
135135

136+
// Checks to store offline.
136137
if (!this.appProvider.isOnline() || forceOffline) {
137138
const notifications = this.checkFields(fields, contents);
138139
if (notifications) {
139140
return Promise.resolve({
140141
fieldnotifications: notifications
141142
});
142143
}
143-
144-
return storeOffline();
145144
}
146145

147-
return this.addEntryOnline(dataId, contents, groupId, siteId).then((result) => {
148-
result.sent = true;
146+
// Get other not synced actions.
147+
return this.dataOffline.getEntryActions(dataId, entryId, siteId).then((entries) => {
148+
if (entries && entries.length) {
149+
// Found. Delete add and edit actions first.
150+
const proms = [];
151+
entries.forEach((entry) => {
152+
if (entry.action == 'add') {
153+
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
154+
}
155+
});
149156

150-
return result;
151-
}).catch((error) => {
152-
if (this.utils.isWebServiceError(error)) {
153-
// The WebService has thrown an error, this means that responses cannot be submitted.
154-
return Promise.reject(error);
157+
return Promise.all(proms);
155158
}
159+
}).then(() => {
160+
// App is offline, store the action.
161+
if (!this.appProvider.isOnline() || forceOffline) {
162+
return storeOffline();
163+
}
164+
165+
return this.addEntryOnline(dataId, contents, groupId, siteId).then((result) => {
166+
result.sent = true;
167+
168+
return result;
169+
}).catch((error) => {
170+
if (this.utils.isWebServiceError(error)) {
171+
// The WebService has thrown an error, this means that responses cannot be submitted.
172+
return Promise.reject(error);
173+
}
156174

157-
// Couldn't connect to server, store in offline.
158-
return storeOffline();
175+
// Couldn't connect to server, store in offline.
176+
return storeOffline();
177+
});
159178
});
160179
}
161180

@@ -398,9 +417,6 @@ export class AddonModDataProvider {
398417
});
399418
};
400419

401-
let justAdded = false,
402-
groupId;
403-
404420
if (!this.appProvider.isOnline() || forceOffline) {
405421
const notifications = this.checkFields(fields, contents);
406422
if (notifications) {
@@ -416,29 +432,14 @@ export class AddonModDataProvider {
416432
// Found. Delete add and edit actions first.
417433
const proms = [];
418434
entries.forEach((entry) => {
419-
if (entry.action == 'add') {
420-
justAdded = true;
421-
groupId = entry.groupid;
422-
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
423-
} else if (entry.action == 'edit') {
435+
if (entry.action == 'edit') {
424436
proms.push(this.dataOffline.deleteEntry(dataId, entryId, entry.action, siteId));
425437
}
426438
});
427439

428440
return Promise.all(proms);
429441
}
430442
}).then(() => {
431-
if (justAdded) {
432-
// The field was added offline, add again and stop.
433-
return this.addEntry(dataId, entryId, courseId, contents, groupId, fields, siteId, forceOffline)
434-
.then((result) => {
435-
result.updated = true;
436-
result.sent = true;
437-
438-
return result;
439-
});
440-
}
441-
442443
if (!this.appProvider.isOnline() || forceOffline) {
443444
// App is offline, store the action.
444445
return storeOffline();

0 commit comments

Comments
 (0)