Skip to content

Commit

Permalink
#19 Dashboard Overview (#2)
Browse files Browse the repository at this point in the history
* Overview ready

* Limit to top five only

* BE adjustments

* PR fix
  • Loading branch information
ManuelSzecsenyi authored Sep 29, 2023
1 parent 212c3f0 commit 5d2da16
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 9 deletions.
19 changes: 12 additions & 7 deletions src/app/components/dashboard/dashboard.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
<div class="grid grid-cols-2">
<app-panel>
<div class="text-light-pink">Chärtli</div>
<div class="text-4xl">1/420</div>
<div class="text-4xl">
{{ (this.dashboardData$ | async)?.myUniqueCardsCount }} /
{{ (this.dashboardData$ | async)?.allCardsCount }}
</div>
</app-panel>
<app-panel>
<div class="text-light-pink">Rang</div>
<div class="text-4xl">69</div>
<div class="text-4xl">{{ this.myRank }}</div>
</app-panel>
</div>
<app-panel>
<div class="text-light-pink">Top 5</div>
<ol class="list-decimal list-inside">
<li>Big P</li>
<li>Stefan Hüsemann</li>
<li>Larissa Zollinger</li>
<li>Christoph Weber</li>
<li>Kevin Duss</li>
<li
*ngFor="
let ranking of (this.dashboardData$ | async)?.rankingList | slice: 0 : 5
"
>
{{ ranking.displayName }}
</li>
</ol>
</app-panel>
36 changes: 34 additions & 2 deletions src/app/components/dashboard/dashboard.component.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { Observable, zip } from 'rxjs';
import { Dashboard } from 'src/app/models/dashboard.model';
import { DashboardService } from 'src/app/services/dashboard.service';
import { selectProfile } from 'src/app/state/profile/profile.selectors';

@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
})
export class DashboardComponent {}
export class DashboardComponent implements OnInit {
dashboardData$?: Observable<Dashboard>;
myRank?: number;

constructor(
private readonly dashboardService: DashboardService,
private readonly store: Store,
) {}

ngOnInit(): void {
this.dashboardData$ = this.dashboardService.getDashboardData(
this.store.select(selectProfile),
);

zip([this.dashboardData$, this.store.select(selectProfile)]).subscribe(
([dashboardData, user]) => {
if (!user) {
this.myRank = undefined;
} else {
this.myRank = this.dashboardService.getRankOf(
user.userPrincipalName,
dashboardData.rankingList,
);
}
},
);
}
}
14 changes: 14 additions & 0 deletions src/app/models/dashboard.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface Dashboard {
myCardsCount: number;
myUniqueCardsCount: number;
allCardsCount: number;
duplicateCardsCount: number;
rankingList: Ranking[];
}

export interface Ranking {
rank: number;
cardsCount: number;
displayName: string;
userEmail: string;
}
38 changes: 38 additions & 0 deletions src/app/services/dashboard.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Dashboard, Ranking } from '../models/dashboard.model';
import { environment } from 'src/environments/environment';
import { Profile } from '../models/profile.model';
import { Observable, of, switchMap, defaultIfEmpty } from 'rxjs';

@Injectable({
providedIn: 'root',
})
export class DashboardService {
constructor(private readonly http: HttpClient) {}

getDashboardData(user: Observable<Profile | null>): Observable<Dashboard> {
return user.pipe(
switchMap((user) => {
// Return empty dashboard if no user is found
if (user === null) {
return of<Dashboard>({
myCardsCount: 0,
myUniqueCardsCount: 0,
allCardsCount: 0,
duplicateCardsCount: 0,
rankingList: [],
});
}

return this.http.get<Dashboard>(`${environment.backendUri}/overview`);
}),
);
}

getRankOf(userEmail: string, rankingList: Ranking[]): number {
return (
rankingList.find((ranking) => ranking.userEmail === userEmail)?.rank ?? 0
);
}
}

0 comments on commit 5d2da16

Please sign in to comment.