aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/dashboard/dashboard.component.ts
blob: c94b5c6ab5f7957f36868fcc9e7a6998f4f69f33 (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
import {Component, OnInit} from '@angular/core';
import {AuthService} from '../../services/auth.service';
import {Router} from '@angular/router';
import {Need} from '../../models/Need';
import {CupboardService} from '../../services/cupboard.service';
import {firstValueFrom} from 'rxjs';
import {UsersService} from '../../services/users.service';

@Component({
    selector: 'app-dashboard',
    standalone: false,
    templateUrl: './dashboard.component.html',
    styleUrl: './dashboard.component.css'
})
export class DashboardComponent implements OnInit{

    topNeeds?: Need[]
    almostThere?: Need[]
    inBasket?: Need[]

    constructor(
        protected authService: AuthService,
        protected router: Router,
        protected cupboardService: CupboardService,
        protected userService: UsersService
    ) {}

    ngOnInit() {
        let user = this.authService.getCurrentUser()
        if(!localStorage.getItem("credential") && !user) {
            this.router.navigate(['/login'])
            return
        }

        firstValueFrom(this.cupboardService.getNeeds()).then(r => {
            this.topNeeds = r.sort((a, b) => b.current - a.current)
            this.almostThere = r.sort((a, b) => a.current/a.maxGoal - b.current/b.maxGoal)
        })

        this.userService.getBasket().subscribe(r => {
            this.inBasket = r;
        })
    }

}