From 47f633c1b284619dca594a3dbeeefce679df2c8f Mon Sep 17 00:00:00 2001 From: ACX <8075870+acx10@users.noreply.github.com> Date: Tue, 17 Feb 2026 21:17:03 -0700 Subject: [PATCH] Fix audit logs pagination bug and add URL query param sync (#2787) --- .../audit-logs/audit-logs.component.html | 1 + .../audit-logs/audit-logs.component.ts | 43 ++++++++++++++++++- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.html b/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.html index e7d419bfe..970620f1e 100644 --- a/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.html +++ b/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.html @@ -65,6 +65,7 @@ [lazy]="true" [paginator]="true" [rows]="rows" + [first]="currentPage * rows" [totalRecords]="totalRecords" [loading]="loading" [showCurrentPageReport]="true" diff --git a/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.ts b/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.ts index 0abd22310..b109cedd1 100644 --- a/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.ts +++ b/booklore-ui/src/app/features/settings/audit-logs/audit-logs.component.ts @@ -1,5 +1,6 @@ import {Component, inject, OnInit, OnDestroy} from '@angular/core'; import {CommonModule} from '@angular/common'; +import {ActivatedRoute, Router} from '@angular/router'; import {TableLazyLoadEvent, TableModule} from 'primeng/table'; import {Select} from 'primeng/select'; import {DatePicker} from 'primeng/datepicker'; @@ -27,6 +28,8 @@ interface UsernameOption { }) export class AuditLogsComponent implements OnInit, OnDestroy { private readonly auditLogService = inject(AuditLogService); + private readonly route = inject(ActivatedRoute); + private readonly router = inject(Router); logs: AuditLog[] = []; totalRecords = 0; @@ -76,9 +79,10 @@ export class AuditLogsComponent implements OnInit, OnDestroy { {label: 'Naming Pattern Changed', value: 'NAMING_PATTERN_CHANGED'}, ]; - private currentPage = 0; + currentPage = 0; ngOnInit(): void { + this.restoreFromQueryParams(); this.loadUsernames(); this.loadLogs(); } @@ -119,12 +123,15 @@ export class AuditLogsComponent implements OnInit, OnDestroy { } onLazyLoad(event: TableLazyLoadEvent): void { - this.currentPage = (event.first ?? 0) / (event.rows ?? this.rows); + this.rows = event.rows ?? this.rows; + this.currentPage = (event.first ?? 0) / this.rows; + this.updateQueryParams(); this.loadLogs(); } onFilterChange(): void { this.currentPage = 0; + this.updateQueryParams(); this.loadLogs(); } @@ -148,6 +155,7 @@ export class AuditLogsComponent implements OnInit, OnDestroy { this.selectedUsername = null; this.dateRange = null; this.currentPage = 0; + this.updateQueryParams(); this.loadLogs(); } @@ -198,6 +206,37 @@ export class AuditLogsComponent implements OnInit, OnDestroy { return 'action-info'; } + private restoreFromQueryParams(): void { + const params = this.route.snapshot.queryParams; + if (params['page']) this.currentPage = +params['page']; + if (params['size']) this.rows = +params['size']; + if (params['action']) this.selectedAction = params['action']; + if (params['username']) this.selectedUsername = params['username']; + if (params['from'] || params['to']) { + const from = params['from'] ? new Date(params['from'] + 'T00:00:00') : null; + const to = params['to'] ? new Date(params['to'] + 'T00:00:00') : null; + if (from) this.dateRange = [from, to!]; + } + } + + private updateQueryParams(): void { + const queryParams: Record = { + page: this.currentPage > 0 ? String(this.currentPage) : null, + size: this.rows !== 25 ? String(this.rows) : null, + action: this.selectedAction || null, + username: this.selectedUsername || null, + from: this.dateRange?.[0] ? this.formatDate(this.dateRange[0]) : null, + to: this.dateRange?.[1] ? this.formatDate(this.dateRange[1]) : null, + }; + this.router.navigate([], {queryParams, queryParamsHandling: 'merge', replaceUrl: true}); + } + + private formatDate(date: Date): string { + return date.getFullYear() + '-' + + String(date.getMonth() + 1).padStart(2, '0') + '-' + + String(date.getDate()).padStart(2, '0'); + } + private formatDateTime(date: Date, endOfDay = false): string { const d = new Date(date); if (endOfDay) {