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 {firstValueFrom, of} from 'rxjs'; import {AuthService} from '../../services/auth.service'; import {ToastsService, ToastType} from '../../services/toasts.service'; import {userType} from '../../models/User'; @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 ) {} @ViewChild("contribution") contribution?: Input; ngOnInit(): void { this.usersService.refreshBasket(); } async checkout() { let order: { needID: number, quantity: number }[] = [] let isNotValid = false for (let contribution of document.querySelectorAll('.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"); } resetColor(ev: any) { // for (let contribution of document.querySelectorAll('.contribution')!) {} (ev.target as HTMLInputElement).setAttribute("style", "") } protected readonly of = of; protected readonly userType = userType; }