aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
blob: 8d23d7b356e65f6e5c2aca99043d93b1ba4de7e4 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {UsersService} from '../../services/users.service';
import {Router} from '@angular/router';
import {CupboardService} from '../../services/cupboard.service';
import {BehaviorSubject, firstValueFrom, of} from 'rxjs';
import {AuthService} from '../../services/auth.service';
import {ToastsService, ToastType} from '../../services/toasts.service';
import {userType} from '../../models/User';
import {GoalType} from '../../models/Need';

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

    constructor(
        private router: Router,
        protected cupboardService: CupboardService,
        protected usersService: UsersService,
        protected authService: AuthService,
        private toastService: ToastsService
    ) {}

    protected runningTotal = new BehaviorSubject(0)
    protected physicalTotal: string[] = []
    @ViewChild("contribution") contribution?: Input;

    ngOnInit(): void {

        this.usersService.refreshBasket();
    }

    async checkout() {
        let order: { needID: number, quantity: number }[] = []
        let isNotValid = false

        this.runningTotal.next(0);
        this.physicalTotal = []

        for (let contribution of document.querySelectorAll<HTMLInputElement>('.contribution')!) {
            if (contribution.value == '' || contribution.valueAsNumber <= 0) {
                isNotValid = true
                contribution.setAttribute("style", "border-color: #ff0000");
            }
            order.push({needID: +contribution.id, quantity: contribution.valueAsNumber});
        }

        if (isNotValid) {
            this.toastService.sendToast(ToastType.ERROR, "Invalid input in funding basket!")
            return;
        }

        try {
            await firstValueFrom(this.cupboardService.checkoutNeed(order))
        } catch (ex:any) {
            this.toastService.sendToast(ToastType.ERROR, ex.error);
            return
        }

        order.forEach(contribution => this.usersService.removeNeed(contribution.needID))
        this.toastService.sendToast(ToastType.INFO, "Checkout successful");
    }

    onInput(ev: any) {
        let total = 0
        this.runningTotal.next(total);
        this.physicalTotal = []
        for (let contribution of document.querySelectorAll<HTMLInputElement>('.contribution')!) {
            this.cupboardService.getNeed(+contribution.id).subscribe(need => {
                if (contribution.value != '' && need.type != GoalType.PHYSICAL) {
                    total += contribution.valueAsNumber
                } else if (contribution.value != '' && need.type == GoalType.PHYSICAL) {
                    this.physicalTotal.push(need.name + ": " + contribution.value)
                }
                this.runningTotal.next(total);

            })
        }

        this.physicalTotal.sort((a, b) => a < b ? -1 : 1);

        (ev.target as HTMLInputElement).setAttribute("style", "border-color: unset")
    }

    protected readonly of = of;
    protected readonly userType = userType;
}