Skip to content

Commit

Permalink
Add pagination to leaderboards view. (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviofernandes004 authored Jul 14, 2023
1 parent bb1a253 commit b81a722
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
17 changes: 15 additions & 2 deletions console/ui/src/app/leaderboards/leaderboards.component.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
<h2 class="pb-1">Leaderboards</h2>
<h6 class="pb-4">{{leaderboards.length}} leaderboards found.</h6>

<div class="row no-gutters">
<div class="col d-flex justify-content-between no-gutters align-items-center">
<div class="col-md-9">
<h2 class="pb-1">Leaderboards</h2>
<h6 class="pb-4">{{leaderboardsCount}} leaderboards found.</h6>
</div>
<div class="col-md-3 justify-content-end text-right">
<div class="btn-group page-btns" role="group" aria-label="Basic example">
<button type="button" class="btn btn-outline-secondary" (click)="search(0)" [disabled]="leaderboards.length === 0"><img src="/static/svg/page-first.svg" alt="" width="20" height=""></button>
<button type="button" class="btn btn-outline-secondary" (click)="search(1)" [disabled]="nextCursor === ''"><img src="/static/svg/page-next.svg" alt="" width="20" height=""></button>
</div>
</div>
</div>
</div>

<ngb-alert [dismissible]="false" type="danger" *ngIf="error">
<img src="/static/svg/red-triangle.svg" alt="" width="16" height="" class="mr-2">
Expand Down
49 changes: 43 additions & 6 deletions console/ui/src/app/leaderboards/leaderboards.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ import {AuthenticationService} from '../authentication.service';
export class LeaderboardsComponent implements OnInit {
public error = '';
public leaderboards: Array<Leaderboard> = [];

public nextCursor: string = "";
public leaderboardsCount: number = 0;
public orderString = {
0: 'Ascending',
1: 'Descending',
Expand All @@ -44,12 +47,14 @@ export class LeaderboardsComponent implements OnInit {
) {}

ngOnInit(): void {
this.route.data.subscribe(d => {
this.leaderboards.length = 0;
this.leaderboards.push(...d[0].leaderboards);
}, err => {
this.error = err;
});
const qp = this.route.snapshot.queryParamMap;
this.nextCursor = qp.get('cursor');

if (this.nextCursor && this.nextCursor !== '') {
this.search(1);
} else {
this.search(0);
}
}

deleteAllowed(): boolean {
Expand All @@ -64,6 +69,7 @@ export class LeaderboardsComponent implements OnInit {
this.consoleService.deleteLeaderboard('', l.id).subscribe(() => {
this.error = '';
this.leaderboards.splice(i, 1);
this.leaderboardsCount--;
}, err => {
this.error = err;
});
Expand All @@ -72,6 +78,37 @@ export class LeaderboardsComponent implements OnInit {
viewLeaderboardEntries(l: Leaderboard): void {
this.router.navigate(['/leaderboards', l.id], {relativeTo: this.route});
}

search(state: number): void {
let cursor = '';
switch (state) {
case 0:
cursor = '';
break;
case 1:
cursor = this.nextCursor;
break;
}

this.consoleService.listLeaderboards('', cursor).subscribe(d => {
this.error = '';

this.leaderboards.length = 0;
this.leaderboards.push(...d.leaderboards);
this.leaderboardsCount = d.total;
this.nextCursor = d.cursor;

this.router.navigate([], {
relativeTo: this.route,
queryParams: {
cursor
},
queryParamsHandling: 'merge',
});
}, err => {
this.error = err;
});
}
}

@Injectable({providedIn: 'root'})
Expand Down

0 comments on commit b81a722

Please sign in to comment.