feat(navbar): update balance display and use signal for state
This commit is contained in:
		
					parent
					
						
							
								564601f7bc
							
						
					
				
			
			
				commit
				
					
						454e99f812
					
				
			
		
					 2 changed files with 13 additions and 12 deletions
				
			
		| 
						 | 
				
			
			@ -10,10 +10,11 @@
 | 
			
		|||
        </div>
 | 
			
		||||
      </div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
      <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">
 | 
			
		||||
          {{ balance | currency : 'EUR' : 'symbol' : '1.2-2' }}
 | 
			
		||||
        <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) {
 | 
			
		||||
          <button (click)="login()" class="button-base px-4 py-1.5">Anmelden</button>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,10 +1,9 @@
 | 
			
		|||
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
 | 
			
		||||
import { ChangeDetectionStrategy, Component, inject, OnInit, signal } from '@angular/core';
 | 
			
		||||
import { RouterModule } from '@angular/router';
 | 
			
		||||
import { KeycloakService } from 'keycloak-angular';
 | 
			
		||||
import { UserService } from '../../../service/user.service';
 | 
			
		||||
import { firstValueFrom } from 'rxjs';
 | 
			
		||||
import { CurrencyPipe } from '@angular/common';
 | 
			
		||||
 | 
			
		||||
import { CurrencyPipe } from '@angular/common';
 | 
			
		||||
@Component({
 | 
			
		||||
  selector: 'app-navbar',
 | 
			
		||||
  templateUrl: './navbar.component.html',
 | 
			
		||||
| 
						 | 
				
			
			@ -12,21 +11,22 @@ import { CurrencyPipe } from '@angular/common';
 | 
			
		|||
  imports: [RouterModule, CurrencyPipe],
 | 
			
		||||
  changeDetection: ChangeDetectionStrategy.OnPush,
 | 
			
		||||
})
 | 
			
		||||
export class NavbarComponent {
 | 
			
		||||
export class NavbarComponent implements OnInit {
 | 
			
		||||
  isMenuOpen = false;
 | 
			
		||||
  private keycloakService: KeycloakService = inject(KeycloakService);
 | 
			
		||||
  isLoggedIn = this.keycloakService.isLoggedIn();
 | 
			
		||||
 | 
			
		||||
  private userService = inject(UserService);
 | 
			
		||||
  private user = this.userService.getCurrentUser();
 | 
			
		||||
  balance = 0;
 | 
			
		||||
 | 
			
		||||
  public balance = signal(0);
 | 
			
		||||
 | 
			
		||||
  async ngOnInit() {
 | 
			
		||||
    const user = await firstValueFrom(this.user);
 | 
			
		||||
    this.balance = user?.balance ?? 0;
 | 
			
		||||
  ngOnInit() {
 | 
			
		||||
    this.user.subscribe((user) => {
 | 
			
		||||
      this.balance.set(user?.balance ?? 0);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  login() {
 | 
			
		||||
    try {
 | 
			
		||||
      const baseUrl = window.location.origin;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Reference in a new issue