Skip to content

Commit 413b698

Browse files
committed
MOBILE-4081 database: Fix wrong entry displayed after sort
1 parent 6b0fbbe commit 413b698

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

src/addons/mod/data/components/action/action.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ export class AddonModDataActionComponent implements OnInit {
4646
@Input() title = ''; // Name of the module.
4747
@Input() group = 0; // Module group.
4848
@Input() offset?: number; // Offset of the entry.
49+
@Input() sortBy?: string | number; // Sort by used to calculate the offset.
50+
@Input() sortDirection?: string; // Sort direction used to calculate the offset.
4951

5052
siteId: string;
5153
userPicture?: string;
@@ -110,6 +112,8 @@ export class AddonModDataActionComponent implements OnInit {
110112
title: this.title,
111113
group: this.group,
112114
offset: this.offset,
115+
sortBy: this.sortBy,
116+
sortDirection: this.sortDirection,
113117
};
114118

115119
const basePath = AddonModDataModuleHandlerService.PAGE_NAME;

src/addons/mod/data/components/index/index.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import {
3939
AddonModDataData,
4040
AddonModDataSearchEntriesAdvancedField,
4141
} from '../../services/data';
42-
import { AddonModDataHelper } from '../../services/data-helper';
42+
import { AddonModDataHelper, AddonModDatDisplayFieldsOptions } from '../../services/data-helper';
4343
import { AddonModDataAutoSyncData, AddonModDataSyncProvider, AddonModDataSyncResult } from '../../services/data-sync';
4444
import { AddonModDataModuleHandlerService } from '../../services/handlers/module';
4545
import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch';
@@ -349,17 +349,20 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
349349
entriesById[entry.id] = entry;
350350

351351
const actions = AddonModDataHelper.getActions(this.database!, this.access!, entry);
352-
const offset = this.search.searching
353-
? 0
354-
: this.search.page * AddonModDataProvider.PER_PAGE + index - numOfflineEntries;
352+
const options: AddonModDatDisplayFieldsOptions = {};
353+
if (!this.search.searching) {
354+
options.offset = this.search.page * AddonModDataProvider.PER_PAGE + index - numOfflineEntries;
355+
options.sortBy = this.search.sortBy;
356+
options.sortDirection = this.search.sortDirection;
357+
}
355358

356359
entriesHTML += AddonModDataHelper.displayShowFields(
357360
template,
358361
this.fieldsArray,
359362
entry,
360-
offset,
361363
AddonModDataTemplateMode.LIST,
362364
actions,
365+
options,
363366
);
364367
});
365368

@@ -504,6 +507,8 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
504507
const pageXOffset = this.entries.findIndex((entry) => entry.id == entryId);
505508
if (pageXOffset >= 0) {
506509
params.offset = this.search.page * AddonModDataProvider.PER_PAGE + pageXOffset;
510+
params.sortBy = this.search.sortBy;
511+
params.sortDirection = this.search.sortDirection;
507512
}
508513
}
509514

@@ -556,7 +561,7 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp
556561
}
557562

558563
export type AddonModDataSearchDataParams = {
559-
sortBy: string;
564+
sortBy: string | number;
560565
sortDirection: string;
561566
page: number;
562567
text: string;

src/addons/mod/data/pages/entry/entry.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy {
5656
protected fields: Record<number, AddonModDataField> = {};
5757
protected fieldsArray: AddonModDataField[] = [];
5858
protected logAfterFetch = true;
59+
protected sortBy = 0;
60+
protected sortDirection = 'DESC';
5961

6062
moduleId = 0;
6163
courseId!: number;
@@ -139,6 +141,9 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy {
139141
this.title = CoreNavigator.getRouteParam<string>('title') || '';
140142
this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0;
141143
this.offset = CoreNavigator.getRouteNumberParam('offset');
144+
this.sortDirection = CoreNavigator.getRouteParam('sortDirection') ?? this.sortDirection;
145+
const sortBy = Number(CoreNavigator.getRouteParam('sortBy'));
146+
this.sortBy = isNaN(sortBy) ? this.sortBy : sortBy;
142147
} catch (error) {
143148
CoreDomUtils.showErrorModal(error);
144149

@@ -189,9 +194,13 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy {
189194
template,
190195
this.fieldsArray,
191196
this.entry!,
192-
this.offset,
193197
AddonModDataTemplateMode.SHOW,
194198
actions,
199+
{
200+
offset: this.offset,
201+
sortBy: this.sortBy,
202+
sortDirection: this.sortDirection,
203+
},
195204
);
196205

197206
this.showComments = actions.comments;
@@ -330,8 +339,8 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy {
330339

331340
const entries = await AddonModDataHelper.fetchEntries(this.database!, this.fieldsArray, {
332341
groupId: this.selectedGroup,
333-
sort: 0,
334-
order: 'DESC',
342+
sort: this.sortBy,
343+
order: this.sortDirection,
335344
page,
336345
perPage,
337346
});

src/addons/mod/data/services/data-helper.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ export class AddonModDataHelperProvider {
200200
template: string,
201201
fields: AddonModDataField[],
202202
entry: AddonModDataEntry,
203-
offset = 0,
204203
mode: AddonModDataTemplateMode,
205204
actions: Record<AddonModDataAction, boolean>,
205+
options: AddonModDatDisplayFieldsOptions = {},
206206
): string {
207207

208208
if (!template) {
@@ -233,8 +233,12 @@ export class AddonModDataHelperProvider {
233233
} else if (action == 'approvalstatus') {
234234
render = Translate.instant('addon.mod_data.' + (entry.approved ? 'approved' : 'notapproved'));
235235
} else {
236-
render = '<addon-mod-data-action action="' + action + '" [entry]="entries[' + entry.id + ']" mode="' + mode +
237-
'" [database]="database" [title]="title" [offset]="' + offset + '" [group]="group" ></addon-mod-data-action>';
236+
render = `<addon-mod-data-action action="${action}" [entry]="entries[${entry.id}]" mode="${mode}" ` +
237+
'[database]="database" [title]="title" ' +
238+
(options.offset !== undefined ? `[offset]="${options.offset}" ` : '') +
239+
(options.sortBy !== undefined ? `[sortBy]="${options.sortBy}" ` : '') +
240+
(options.sortDirection !== undefined ? `sortDirection="${options.sortDirection}" ` : '') +
241+
'[group]="group"></addon-mod-data-action>';
238242
}
239243
template = template.replace(replaceRegex, render);
240244
} else {
@@ -822,3 +826,9 @@ export class AddonModDataHelperProvider {
822826

823827
}
824828
export const AddonModDataHelper = makeSingleton(AddonModDataHelperProvider);
829+
830+
export type AddonModDatDisplayFieldsOptions = {
831+
sortBy?: string | number;
832+
sortDirection?: string;
833+
offset?: number;
834+
};

0 commit comments

Comments
 (0)