Refactor pagination logic and add error handling in history

Introduced `updatePageSize` for better page size management and added checks to prevent rapid page changes. Enhanced error handling in `history.component` with retry logic on failure. Implemented `RemoveTrailingPeriodPipe` for cleaner UI formatting.
This commit is contained in:
2025-04-18 19:32:23 +02:00
parent 53f67a5075
commit 21f2b3e4a5
5 changed files with 60 additions and 8 deletions
@@ -1,15 +1,18 @@
import {Component, EventEmitter, Input, OnChanges, OnInit, Output} from '@angular/core';
import {BASE_PATH, HistoryService, PunishmentHistoryInner} from '../../../api';
import {map, Observable, shareReplay} from 'rxjs';
import {catchError, map, Observable, shareReplay} from 'rxjs';
import {NgForOf, NgIf, NgOptimizedImage} from '@angular/common';
import {CookieService} from 'ngx-cookie-service';
import {RemoveTrailingPeriodPipe} from '../../util/RemoveTrailingPeriodPipe';
import {HttpErrorResponse} from '@angular/common/http';
@Component({
selector: 'app-history',
imports: [
NgIf,
NgForOf,
NgOptimizedImage
NgOptimizedImage,
RemoveTrailingPeriodPipe
],
templateUrl: './history.component.html',
styleUrl: './history.component.scss',
@@ -36,7 +39,6 @@ export class HistoryComponent implements OnInit, OnChanges {
ngOnChanges(): void {
this.reloadHistory();
this.pageChange.emit(this.history.length);
}
ngOnInit(): void {
@@ -57,6 +59,27 @@ export class HistoryComponent implements OnInit, OnChanges {
historyObservable.pipe(
map(history => {
this.history = history;
this.pageChange.emit(this.history.length);
}),
catchError(err => {
this.pageChange.emit(-1);
let retrySeconds = 5;
if (err instanceof HttpErrorResponse) {
const headers = err.headers;
const retryAfterHeader = headers.get('Retry-After');
console.warn(err.error);
if (retryAfterHeader) {
const retryAfter = parseInt(retryAfterHeader || '0', 10);
if (!isNaN(retryAfter) && retryAfter > 0) {
retrySeconds = retryAfter + 1;
}
}
} else {
console.error(err);
}
setTimeout(() => this.reloadHistory(), retrySeconds * 1000);
return this.history;
}),
shareReplay(1)
).subscribe();