This repository has been archived on 2025-06-18. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
casino/frontend/src/app/shared/components/navbar/navbar.component.ts
2025-05-07 14:27:46 +02:00

51 lines
1.4 KiB
TypeScript

import {ChangeDetectionStrategy, Component, inject, OnDestroy, OnInit, signal,} from '@angular/core';
import {RouterModule} from '@angular/router';
import {AuthService} from '../../../service/auth.service';
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, AnimatedNumberComponent],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class NavbarComponent implements OnInit, OnDestroy {
isMenuOpen = false;
private authService: AuthService = inject(AuthService);
isLoggedIn = this.authService.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 {
this.authService.login();
} catch (error) {
console.error('Login failed:', error);
}
}
logout() {
this.authService.logout();
}
toggleMenu() {
this.isMenuOpen = !this.isMenuOpen;
}
}