aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/dashboard/dashboard.component.ts
blob: 2ab4db26b42065bed512aa6b412dffd4491a58b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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<number | undefined>(undefined)
    protected totalDonations = new BehaviorSubject<number | undefined>(undefined)
    protected totalNeeds = new BehaviorSubject<number | undefined>(undefined)
    protected fulfilledNeeds = new BehaviorSubject<Need[] | undefined>(undefined)
    protected mostFulfilledNeeds = new BehaviorSubject<Need[] | undefined>(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;
}