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
|
import {Component, Input, OnInit, ViewChild} from '@angular/core';
import {User} from '../../models/User';
import { UsersService } from '../../services/users.service';
import { Need } from '../../models/Need';
import { NeedListComponent } from '../need-list/need-list.component';
import { Router } from '@angular/router';
import { CupboardService } from '../../services/cupboard.service';
import { BehaviorSubject, catchError, firstValueFrom, Observable } from 'rxjs';
@Component({
selector: 'app-funding-basket',
standalone: false,
templateUrl: './funding-basket.component.html',
styleUrl: './funding-basket.component.css'
})
export class FundingBasketComponent implements OnInit {
statusText: any;
constructor(
private router: Router,
protected cupboardService: CupboardService,
protected usersService: UsersService
) {}
@ViewChild("contribution") contribution?: Input;
@Input() isValid: boolean = true;
// this is for login rerouting
ngOnInit(): void {
if (!this.usersService.getCurrentUser()) {
this.router.navigate(['/login'], {queryParams: {redir: this.router.url}});
return;
}
this.usersService.refreshBasket();
// this.usersService.removeNeed(); <- call this to remove
}
async checkout() {
this.isValid = true;
for (let c of document.getElementById("funding-basket")?.querySelectorAll('.contribution')!) {
let contribution = c as HTMLInputElement;
console.log(contribution.value, contribution.id);
contribution.setAttribute("style","");
if ( contribution.value == '' || contribution.valueAsNumber < 0) {
this.isValid = false;
contribution.setAttribute("style","color: #ff0000");
}
}
if (this.isValid) {
for (let c of document.getElementById("funding-basket")?.querySelectorAll('.contribution')!) {
let contribution = c as HTMLInputElement;
let need = await firstValueFrom(this.cupboardService.getNeed(+contribution.id));
need.current +=+ contribution.value;
console.log(need);
this.usersService.removeNeed(need.id);
this.cupboardService.updateNeed(need.id, need)
.pipe(catchError((ex, r) => {
console.log(ex.status);
if (ex.status == 500) {
this.statusText.next("Fields cannot be blank");
} else if (ex.status == 400) {
this.statusText.next("Goal must be greater than 0");
} else {
this.statusText.next("Error on creating need");
}
return new Observable<string>();
}))
.subscribe(
(result) => {
if (result) {
console.log("need updated successfully");
//this.needList?.refresh()
} else {
console.log("need update failed");
}
}
);
}
}
}
}
|