feat(blackjack): refresh user balance on game state change

This commit is contained in:
Jan-Marlon Leibl 2025-04-02 10:25:43 +02:00
parent 08b12d238e
commit e983b21e07
Signed by: jleibl
GPG key ID: 300B2F906DC6F1D5
7 changed files with 48 additions and 19 deletions

View file

@ -1,7 +1,7 @@
import { inject, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { KeycloakProfile } from 'keycloak-js';
import { catchError, EMPTY, Observable } from 'rxjs';
import { BehaviorSubject, catchError, EMPTY, Observable, tap } from 'rxjs';
import { User } from '../model/User';
@Injectable({
@ -9,20 +9,39 @@ import { User } from '../model/User';
})
export class UserService {
private http: HttpClient = inject(HttpClient);
private currentUserSubject = new BehaviorSubject<User | null>(null);
public currentUser$ = this.currentUserSubject.asObservable();
constructor() {
// Initialize with current user data
this.getCurrentUser().subscribe();
}
public getUser(id: string): Observable<User | null> {
return this.http.get<User | null>(`/backend/user/${id}`).pipe(catchError(() => EMPTY));
return this.http.get<User | null>(`/backend/user/${id}`).pipe(
catchError(() => EMPTY),
tap(user => this.currentUserSubject.next(user))
);
}
public getCurrentUser(): Observable<User | null> {
return this.http.get<User | null>('/backend/user').pipe(catchError(() => EMPTY));
return this.http.get<User | null>('/backend/user').pipe(
catchError(() => EMPTY),
tap(user => this.currentUserSubject.next(user))
);
}
public refreshCurrentUser(): void {
this.getCurrentUser().subscribe();
}
public createUser(id: string, username: string): Observable<User> {
return this.http.post<User>('/backend/user', {
keycloakId: id,
username: username,
});
}).pipe(
tap(user => this.currentUserSubject.next(user))
);
}
public async getOrCreateUser(userProfile: KeycloakProfile) {