MOBILE-4081 database: Fix wrong entry displayed after sort
This commit is contained in:
		
							parent
							
								
									6b0fbbeaa5
								
							
						
					
					
						commit
						413b69832b
					
				| @ -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; | ||||
|  | ||||
| @ -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; | ||||
|  | ||||
| @ -56,6 +56,8 @@ export class AddonModDataEntryPage implements OnInit, OnDestroy { | ||||
|     protected fields: Record<number, AddonModDataField> = {}; | ||||
|     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<string>('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, | ||||
|         }); | ||||
|  | ||||
| @ -200,9 +200,9 @@ export class AddonModDataHelperProvider { | ||||
|         template: string, | ||||
|         fields: AddonModDataField[], | ||||
|         entry: AddonModDataEntry, | ||||
|         offset = 0, | ||||
|         mode: AddonModDataTemplateMode, | ||||
|         actions: Record<AddonModDataAction, boolean>, | ||||
|         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 = '<addon-mod-data-action action="' + action + '" [entry]="entries[' + entry.id + ']" mode="' + mode + | ||||
|                     '" [database]="database" [title]="title" [offset]="' + offset + '" [group]="group" ></addon-mod-data-action>'; | ||||
|                     render = `<addon-mod-data-action action="${action}" [entry]="entries[${entry.id}]" mode="${mode}" ` + | ||||
|                         '[database]="database" [title]="title" ' + | ||||
|                         (options.offset !== undefined ? `[offset]="${options.offset}" ` : '') + | ||||
|                         (options.sortBy !== undefined ? `[sortBy]="${options.sortBy}" ` : '') + | ||||
|                         (options.sortDirection !== undefined ? `sortDirection="${options.sortDirection}" ` : '') + | ||||
|                         '[group]="group"></addon-mod-data-action>'; | ||||
|                 } | ||||
|                 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; | ||||
| }; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user