From 413b69832b74596a1085c50dc3511314f9915b86 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Thu, 15 Dec 2022 13:27:30 +0100 Subject: [PATCH] MOBILE-4081 database: Fix wrong entry displayed after sort --- src/addons/mod/data/components/action/action.ts | 4 ++++ src/addons/mod/data/components/index/index.ts | 17 +++++++++++------ src/addons/mod/data/pages/entry/entry.ts | 15 ++++++++++++--- src/addons/mod/data/services/data-helper.ts | 16 +++++++++++++--- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/addons/mod/data/components/action/action.ts b/src/addons/mod/data/components/action/action.ts index 20125df2c..16653ad46 100644 --- a/src/addons/mod/data/components/action/action.ts +++ b/src/addons/mod/data/components/action/action.ts @@ -46,6 +46,8 @@ export class AddonModDataActionComponent implements OnInit { @Input() title = ''; // Name of the module. @Input() group = 0; // Module group. @Input() offset?: number; // Offset of the entry. + @Input() sortBy?: string | number; // Sort by used to calculate the offset. + @Input() sortDirection?: string; // Sort direction used to calculate the offset. siteId: string; userPicture?: string; @@ -110,6 +112,8 @@ export class AddonModDataActionComponent implements OnInit { title: this.title, group: this.group, offset: this.offset, + sortBy: this.sortBy, + sortDirection: this.sortDirection, }; const basePath = AddonModDataModuleHandlerService.PAGE_NAME; diff --git a/src/addons/mod/data/components/index/index.ts b/src/addons/mod/data/components/index/index.ts index 894ca5296..36948b327 100644 --- a/src/addons/mod/data/components/index/index.ts +++ b/src/addons/mod/data/components/index/index.ts @@ -39,7 +39,7 @@ import { AddonModDataData, AddonModDataSearchEntriesAdvancedField, } from '../../services/data'; -import { AddonModDataHelper } from '../../services/data-helper'; +import { AddonModDataHelper, AddonModDatDisplayFieldsOptions } from '../../services/data-helper'; import { AddonModDataAutoSyncData, AddonModDataSyncProvider, AddonModDataSyncResult } from '../../services/data-sync'; import { AddonModDataModuleHandlerService } from '../../services/handlers/module'; import { AddonModDataPrefetchHandler } from '../../services/handlers/prefetch'; @@ -349,17 +349,20 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp entriesById[entry.id] = entry; const actions = AddonModDataHelper.getActions(this.database!, this.access!, entry); - const offset = this.search.searching - ? 0 - : this.search.page * AddonModDataProvider.PER_PAGE + index - numOfflineEntries; + const options: AddonModDatDisplayFieldsOptions = {}; + if (!this.search.searching) { + options.offset = this.search.page * AddonModDataProvider.PER_PAGE + index - numOfflineEntries; + options.sortBy = this.search.sortBy; + options.sortDirection = this.search.sortDirection; + } entriesHTML += AddonModDataHelper.displayShowFields( template, this.fieldsArray, entry, - offset, AddonModDataTemplateMode.LIST, actions, + options, ); }); @@ -504,6 +507,8 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp const pageXOffset = this.entries.findIndex((entry) => entry.id == entryId); if (pageXOffset >= 0) { params.offset = this.search.page * AddonModDataProvider.PER_PAGE + pageXOffset; + params.sortBy = this.search.sortBy; + params.sortDirection = this.search.sortDirection; } } @@ -556,7 +561,7 @@ export class AddonModDataIndexComponent extends CoreCourseModuleMainActivityComp } export type AddonModDataSearchDataParams = { - sortBy: string; + sortBy: string | number; sortDirection: string; page: number; text: string; diff --git a/src/addons/mod/data/pages/entry/entry.ts b/src/addons/mod/data/pages/entry/entry.ts index 9e5be571d..470f97f7a 100644 --- a/src/addons/mod/data/pages/entry/entry.ts +++ b/src/addons/mod/data/pages/entry/entry.ts @@ -56,6 +56,8 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy { protected fields: Record = {}; protected fieldsArray: AddonModDataField[] = []; protected logAfterFetch = true; + protected sortBy = 0; + protected sortDirection = 'DESC'; moduleId = 0; courseId!: number; @@ -139,6 +141,9 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy { this.title = CoreNavigator.getRouteParam('title') || ''; this.selectedGroup = CoreNavigator.getRouteNumberParam('group') || 0; this.offset = CoreNavigator.getRouteNumberParam('offset'); + this.sortDirection = CoreNavigator.getRouteParam('sortDirection') ?? this.sortDirection; + const sortBy = Number(CoreNavigator.getRouteParam('sortBy')); + this.sortBy = isNaN(sortBy) ? this.sortBy : sortBy; } catch (error) { CoreDomUtils.showErrorModal(error); @@ -189,9 +194,13 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy { template, this.fieldsArray, this.entry!, - this.offset, AddonModDataTemplateMode.SHOW, actions, + { + offset: this.offset, + sortBy: this.sortBy, + sortDirection: this.sortDirection, + }, ); this.showComments = actions.comments; @@ -330,8 +339,8 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy { const entries = await AddonModDataHelper.fetchEntries(this.database!, this.fieldsArray, { groupId: this.selectedGroup, - sort: 0, - order: 'DESC', + sort: this.sortBy, + order: this.sortDirection, page, perPage, }); diff --git a/src/addons/mod/data/services/data-helper.ts b/src/addons/mod/data/services/data-helper.ts index e97e8db3a..c83abf47a 100644 --- a/src/addons/mod/data/services/data-helper.ts +++ b/src/addons/mod/data/services/data-helper.ts @@ -200,9 +200,9 @@ export class AddonModDataHelperProvider { template: string, fields: AddonModDataField[], entry: AddonModDataEntry, - offset = 0, mode: AddonModDataTemplateMode, actions: Record, + options: AddonModDatDisplayFieldsOptions = {}, ): string { if (!template) { @@ -233,8 +233,12 @@ export class AddonModDataHelperProvider { } else if (action == 'approvalstatus') { render = Translate.instant('addon.mod_data.' + (entry.approved ? 'approved' : 'notapproved')); } else { - render = ''; + render = `'; } template = template.replace(replaceRegex, render); } else { @@ -822,3 +826,9 @@ export class AddonModDataHelperProvider { } export const AddonModDataHelper = makeSingleton(AddonModDataHelperProvider); + +export type AddonModDatDisplayFieldsOptions = { + sortBy?: string | number; + sortDirection?: string; + offset?: number; +};