AltitudeWeb/frontend/src/app/bans/bans.component.ts

163 lines
4.1 KiB
TypeScript
Raw Normal View History

import {Component, OnInit} from '@angular/core';
2025-04-11 19:20:02 +00:00
import {HeaderComponent} from "../header/header.component";
import {HistoryComponent} from './history/history.component';
import {HistoryCount, HistoryService} from '../../api';
import {NgClass, NgForOf, NgIf} from '@angular/common';
import {FormsModule} from '@angular/forms';
2025-04-11 19:20:02 +00:00
@Component({
selector: 'app-bans',
imports: [
HeaderComponent,
HistoryComponent,
NgIf,
FormsModule,
NgForOf,
NgClass
2025-04-11 19:20:02 +00:00
],
templateUrl: './bans.component.html',
styleUrl: './bans.component.scss'
})
export class BansComponent implements OnInit {
private PAGE_SIZE: number = 10;
public getCurrentButtonId(options: 'all' | 'ban' | 'mute' | 'kick' | 'warn') {
if (options == this.punishmentType) {
return 'currentButton'
}
return null;
}
constructor(public historyApi: HistoryService) {
}
2025-04-11 19:20:02 +00:00
public active: string = '';
2025-04-11 19:20:02 +00:00
public userType: 'player' | 'staff' = "player";
public punishmentType: 'all' | 'ban' | 'mute' | 'kick' | 'warn' = "all";
public page: number = 0;
public names: string[] = [];
public finalSearchTerm: string = '';
public searchTerm: string = '';
public filteredNames: string[] = [];
public pageSize = 0;
public historyCount: HistoryCount = {
bans: 0,
mutes: 0,
kicks: 0,
warnings: 0
}
ngOnInit() {
this.historyApi.getUserNames(this.userType, this.punishmentType).subscribe(names => {
this.names = names;
});
this.historyApi.getTotalPunishments().subscribe(totalPunishments => {
this.historyCount = totalPunishments;
})
this.pushState();
window.addEventListener('popstate', (event) => {
if (event.state && event.state.searchTerm !== undefined) {
this.searchTerm = event.state.searchTerm;
this.finalSearchTerm = event.state.searchTerm;
this.page = event.state.page;
this.userType = event.state.userType;
this.punishmentType = event.state.punishmentType;
}
});
}
public filterNames() {
if (!this.searchTerm) {
this.filteredNames = [];
return;
}
this.filteredNames = this.names.filter(name =>
name.toLowerCase().startsWith(this.searchTerm.toLowerCase())
).slice(0, 10);
}
2025-04-11 19:20:02 +00:00
public selectName(name: string) {
this.searchTerm = name;
this.filteredNames = [];
}
public search() {
this.pushState()
this.finalSearchTerm = this.searchTerm;
this.page = 0;
}
public changeHistoryType(type: 'player' | 'staff') {
this.pushState();
this.userType = type;
this.page = 0;
}
public changeHistoryPunishment(type: 'all' | 'ban' | 'mute' | 'kick' | 'warn') {
this.pushState();
this.punishmentType = type;
this.page = 0;
}
public nextPage() {
if (this.page === this.getMaxPage()) {
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 = {
searchTerm: this.searchTerm,
page: this.page,
userType: this.userType,
punishmentType: this.punishmentType
};
const title = 'Altitude Bans';
const url = window.location.href;
window.history.pushState(state, title, url);
}
public getMaxPage() {
const all = this.historyCount.bans + this.historyCount.mutes + this.historyCount.warnings;
switch (this.punishmentType) {
case 'all':
return Math.floor(all / this.PAGE_SIZE) - 1;
case 'ban':
return Math.floor(this.historyCount.bans / this.PAGE_SIZE) - 1;
case 'mute':
return Math.floor(this.historyCount.mutes / this.PAGE_SIZE) - 1;
case 'kick':
return Math.floor(this.historyCount.kicks / this.PAGE_SIZE) - 1;
case 'warn':
return Math.floor(this.historyCount.warnings / this.PAGE_SIZE) - 1;
default:
return 0;
}
}
public buttonActive(compare: number): boolean {
if (compare !== 0 && this.pageSize < this.PAGE_SIZE) {
return false;
}
return this.page !== compare;
}
2025-04-11 19:20:02 +00:00
}