60 lines
1.6 KiB
TypeScript
60 lines
1.6 KiB
TypeScript
import {
|
|
ChangeDetectionStrategy,
|
|
Component,
|
|
inject,
|
|
OnInit,
|
|
OnDestroy,
|
|
signal,
|
|
} from '@angular/core';
|
|
import { RouterModule } from '@angular/router';
|
|
import { KeycloakService } from 'keycloak-angular';
|
|
import { CurrencyPipe } from '@angular/common';
|
|
import { UserService } from '@service/user.service';
|
|
import { Subscription } from 'rxjs';
|
|
import { AnimatedNumberComponent } from '@blackjack/components/animated-number/animated-number.component';
|
|
|
|
@Component({
|
|
selector: 'app-navbar',
|
|
templateUrl: './navbar.component.html',
|
|
standalone: true,
|
|
imports: [RouterModule, CurrencyPipe, AnimatedNumberComponent],
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
})
|
|
export class NavbarComponent implements OnInit, OnDestroy {
|
|
isMenuOpen = false;
|
|
private keycloakService: KeycloakService = inject(KeycloakService);
|
|
isLoggedIn = this.keycloakService.isLoggedIn();
|
|
|
|
private userService = inject(UserService);
|
|
private userSubscription: Subscription | undefined;
|
|
public balance = signal(0);
|
|
|
|
ngOnInit() {
|
|
this.userSubscription = this.userService.currentUser$.subscribe((user) => {
|
|
this.balance.set(user?.balance ?? 0);
|
|
});
|
|
}
|
|
|
|
ngOnDestroy() {
|
|
if (this.userSubscription) {
|
|
this.userSubscription.unsubscribe();
|
|
}
|
|
}
|
|
|
|
login() {
|
|
try {
|
|
const baseUrl = window.location.origin;
|
|
this.keycloakService.login({ redirectUri: `${baseUrl}/login/success` });
|
|
} catch (error) {
|
|
console.error('Login failed:', error);
|
|
}
|
|
}
|
|
|
|
logout() {
|
|
this.keycloakService.logout();
|
|
}
|
|
|
|
toggleMenu() {
|
|
this.isMenuOpen = !this.isMenuOpen;
|
|
}
|
|
}
|