Add dynamic pagination and state management for history

Introduced dynamic pagination based on punishment types and counts, retrieved via a new API call. Refactored state management with a centralized `pushState` method and updated button logic in the template to reflect the changes. This improves navigation consistency and user experience.
This commit is contained in:
Teriuihi 2025-04-12 01:45:59 +02:00
parent 66d641b825
commit 2b27fb6955
2 changed files with 68 additions and 15 deletions

View File

@ -11,10 +11,10 @@
<div class="container"> <div class="container">
<div class="columnSection"> <div class="columnSection">
<div class="historyTypeContainer"> <div class="historyTypeContainer">
<button class="button-outer" (click)="punishmentType = 'all'">All</button> <button class="button-outer" (click)="changeHistoryPunishment('all')">All</button>
<button class="button-outer" (click)="punishmentType = 'ban'">Bans</button> <button class="button-outer" (click)="changeHistoryPunishment('ban')">Bans</button>
<button class="button-outer" (click)="punishmentType = 'mute'">Mutes</button> <button class="button-outer" (click)="changeHistoryPunishment('mute')">Mutes</button>
<button class="button-outer" (click)="punishmentType = 'warn'">Warnings</button> <button class="button-outer" (click)="changeHistoryPunishment('warn')">Warnings</button>
</div> </div>
<div class="historySearchContainer"> <div class="historySearchContainer">
<input class="historySearch" <input class="historySearch"

View File

@ -1,7 +1,7 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {HeaderComponent} from "../header/header.component"; import {HeaderComponent} from "../header/header.component";
import {HistoryComponent} from './history/history.component'; import {HistoryComponent} from './history/history.component';
import {HistoryService} from '../../api'; import {HistoryCount, HistoryService} from '../../api';
import {NgForOf, NgIf} from '@angular/common'; import {NgForOf, NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms'; import {FormsModule} from '@angular/forms';
@ -29,21 +29,23 @@ export class BansComponent implements OnInit {
public finalSearchTerm: string = ''; public finalSearchTerm: string = '';
public searchTerm: string = ''; public searchTerm: string = '';
public filteredNames: string[] = []; public filteredNames: string[] = [];
public historyCount: HistoryCount = {
bans: 0,
mutes: 0,
kicks: 0,
warnings: 0
}
ngOnInit() { ngOnInit() {
this.historyApi.getUserNames(this.userType, this.punishmentType).subscribe(names => { this.historyApi.getUserNames(this.userType, this.punishmentType).subscribe(names => {
this.names = names; this.names = names;
}); });
const state = { this.historyApi.getTotalPunishments().subscribe(totalPunishments => {
searchTerm: this.searchTerm, this.historyCount = totalPunishments;
page: this.page, })
userType: this.userType,
punishmentType: this.punishmentType this.pushState();
};
const title = 'Altitude Bans';
const url = window.location.href;
window.history.pushState(state, title, url)
window.addEventListener('popstate', (event) => { window.addEventListener('popstate', (event) => {
if (event.state && event.state.searchTerm !== undefined) { if (event.state && event.state.searchTerm !== undefined) {
@ -73,6 +75,40 @@ export class BansComponent implements OnInit {
} }
public search() { public search() {
this.pushState()
this.finalSearchTerm = this.searchTerm;
}
public changeHistoryType(type: 'player' | 'staff') {
this.pushState();
this.userType = type;
}
public changeHistoryPunishment(type: 'all' | 'ban' | 'mute' | 'kick' | 'warn') {
this.pushState();
this.punishmentType = type;
}
public nextPage() {
if (this.page === this.getMaxPage() - 1) {
return;
}
this.setPage(this.page++)
}
public previousPage() {
if (this.page === 0) {
return;
}
this.setPage(this.page--)
}
public setPage(page: number) {
this.pushState();
this.page = page
}
private pushState() {
const state = { const state = {
searchTerm: this.searchTerm, searchTerm: this.searchTerm,
page: this.page, page: this.page,
@ -82,6 +118,23 @@ export class BansComponent implements OnInit {
const title = 'Altitude Bans'; const title = 'Altitude Bans';
const url = window.location.href; const url = window.location.href;
window.history.pushState(state, title, url); window.history.pushState(state, title, url);
this.finalSearchTerm = this.searchTerm; }
public getMaxPage() {
const all = this.historyCount.bans + this.historyCount.mutes + this.historyCount.warnings;
switch (this.punishmentType) {
case 'all':
return Math.ceil(all / 10);
case 'ban':
return Math.ceil(this.historyCount.bans / 10);
case 'mute':
return Math.ceil(this.historyCount.mutes / 10);
case 'kick':
return Math.ceil(this.historyCount.kicks / 10);
case 'warn':
return Math.ceil(this.historyCount.warnings / 10);
default:
return 0;
}
} }
} }