import {Component, OnInit} from '@angular/core'; import {AuthService} from '../../services/auth.service'; import {Router} from '@angular/router'; import {CupboardService} from '../../services/cupboard.service'; import {UsersService} from '../../services/users.service'; import {BehaviorSubject} from 'rxjs'; import {GoalType, Need} from '../../models/Need'; import {userType} from '../../models/User'; @Component({ selector: 'app-dashboard', standalone: false, templateUrl: './dashboard.component.html', styleUrl: './dashboard.component.css' }) export class DashboardComponent implements OnInit{ protected count = new BehaviorSubject(undefined) protected totalDonations = new BehaviorSubject(undefined) protected totalNeeds = new BehaviorSubject(undefined) protected fulfilledNeeds = new BehaviorSubject(undefined) protected mostFulfilledNeeds = new BehaviorSubject(undefined) constructor( protected authService: AuthService, protected router: Router, protected cupboardService: CupboardService, protected userService: UsersService ) {} ngOnInit() { this.userService.getCount().subscribe(count => this.count.next(count)) this.cupboardService.getNeeds().subscribe(needs => { let totalValue = 0 for (let need of needs) { if (need.type === GoalType.MONETARY) { totalValue += need.current this.totalDonations.next(totalValue) } } this.fulfilledNeeds.next(needs.filter(a => ((a.current / a.maxGoal)) >= 1)) needs.sort((a, b) => b.current/b.maxGoal - a.current/a.maxGoal) needs = needs.filter(a => a.current != 0) this.totalNeeds.next(needs.length) this.mostFulfilledNeeds.next(needs.slice(0, 5)) }) } protected readonly userType = userType; }