Merge pull request 'Add current user balance display in navbar (CAS-26)' (!49) from task/CAS-26/add-balance-to-nav into main
Reviewed-on: https://git.simonis.lol/projects/casino/pulls/49 Reviewed-by: Klan Jattenhoff <jan@kjan.email> Reviewed-by: lziemke <lea.z4@schule.bremen.de>
This commit is contained in:
commit
c651337d30
3 changed files with 25 additions and 3 deletions
|
@ -14,6 +14,10 @@ export class UserService {
|
||||||
return this.http.get<User | null>(`/backend/user/${id}`).pipe(catchError(() => EMPTY));
|
return this.http.get<User | null>(`/backend/user/${id}`).pipe(catchError(() => EMPTY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getCurrentUser(): Observable<User | null> {
|
||||||
|
return this.http.get<User | null>('/backend/user').pipe(catchError(() => EMPTY));
|
||||||
|
}
|
||||||
|
|
||||||
public createUser(id: string, username: string): Observable<User> {
|
public createUser(id: string, username: string): Observable<User> {
|
||||||
return this.http.post<User>('/backend/user', {
|
return this.http.post<User>('/backend/user', {
|
||||||
keycloakId: id,
|
keycloakId: id,
|
||||||
|
|
|
@ -11,6 +11,11 @@
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="hidden md:flex items-center space-x-4">
|
<div class="hidden md:flex items-center space-x-4">
|
||||||
|
<div
|
||||||
|
class="text-white font-bold bg-deep-blue-contrast rounded-full px-4 py-2 text-sm hover:bg-deep-blue-contrast/80 hover:cursor-pointer hover:scale-105 transition-all active:scale-95 select-none duration-300"
|
||||||
|
>
|
||||||
|
<span>Balance: {{ balance() | currency: 'EUR' : 'symbol' : '1.2-2' }}</span>
|
||||||
|
</div>
|
||||||
@if (!isLoggedIn) {
|
@if (!isLoggedIn) {
|
||||||
<button (click)="login()" class="button-primary px-4 py-1.5">Anmelden</button>
|
<button (click)="login()" class="button-primary px-4 py-1.5">Anmelden</button>
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +1,32 @@
|
||||||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
|
import { ChangeDetectionStrategy, Component, inject, OnInit, signal } from '@angular/core';
|
||||||
import { RouterModule } from '@angular/router';
|
import { RouterModule } from '@angular/router';
|
||||||
import { KeycloakService } from 'keycloak-angular';
|
import { KeycloakService } from 'keycloak-angular';
|
||||||
|
import { UserService } from '../../../service/user.service';
|
||||||
|
|
||||||
|
import { CurrencyPipe } from '@angular/common';
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-navbar',
|
selector: 'app-navbar',
|
||||||
templateUrl: './navbar.component.html',
|
templateUrl: './navbar.component.html',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [RouterModule],
|
imports: [RouterModule, CurrencyPipe],
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class NavbarComponent {
|
export class NavbarComponent implements OnInit {
|
||||||
isMenuOpen = false;
|
isMenuOpen = false;
|
||||||
private keycloakService: KeycloakService = inject(KeycloakService);
|
private keycloakService: KeycloakService = inject(KeycloakService);
|
||||||
isLoggedIn = this.keycloakService.isLoggedIn();
|
isLoggedIn = this.keycloakService.isLoggedIn();
|
||||||
|
|
||||||
|
private userService = inject(UserService);
|
||||||
|
private user = this.userService.getCurrentUser();
|
||||||
|
|
||||||
|
public balance = signal(0);
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.user.subscribe((user) => {
|
||||||
|
this.balance.set(user?.balance ?? 0);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
login() {
|
login() {
|
||||||
try {
|
try {
|
||||||
const baseUrl = window.location.origin;
|
const baseUrl = window.location.origin;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue