aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAkash Keshav <112591754+domesticchores@users.noreply.github.com>2025-03-18 12:42:37 -0400
committerAkash Keshav <112591754+domesticchores@users.noreply.github.com>2025-03-18 12:42:37 -0400
commit98431cd75078fd671270039ad9fa4d42759dc502 (patch)
tree24316d810e3f34b676b298220a0c2c7af36edc80
parent0b41684e57c79374aee3a564307cd4fdef603e7c (diff)
downloadJellySolutions-98431cd75078fd671270039ad9fa4d42759dc502.tar.gz
JellySolutions-98431cd75078fd671270039ad9fa4d42759dc502.tar.bz2
JellySolutions-98431cd75078fd671270039ad9fa4d42759dc502.zip
reworked funding basket to accept need-list component. -ak
-rw-r--r--ufund-ui/src/app/components/funding-basket/funding-basket.component.ts71
-rw-r--r--ufund-ui/src/app/components/need-list/need-list.component.ts6
-rw-r--r--ufund-ui/src/app/services/users.service.ts20
3 files changed, 31 insertions, 66 deletions
diff --git a/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts b/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
index dd68c0c..bc7f087 100644
--- a/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
+++ b/ufund-ui/src/app/components/funding-basket/funding-basket.component.ts
@@ -5,6 +5,7 @@ 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, firstValueFrom } from 'rxjs';
@Component({
selector: 'app-funding-basket',
@@ -12,80 +13,26 @@ import { CupboardService } from '../../services/cupboard.service';
templateUrl: './funding-basket.component.html',
styleUrl: './funding-basket.component.css'
})
-export class FundingBasketComponent implements
- OnInit {
- user!: User;
- needs: Need[] = [];
- basket: Need[] = [];
- needCount = 0;
- need_quantity: {[key: number]: number} = {};
+export class FundingBasketComponent implements OnInit {
+ basket = new BehaviorSubject<Need[]>([]);
constructor(
private router: Router,
- private cupboardService: CupboardService, private usersService: UsersService
+ private cupboardService: CupboardService,
+ private usersService: UsersService
) {}
+ // this is for login rerouting
ngOnInit(): void {
if (!this.usersService.getCurrentUser()) {
this.router.navigate(['/login'], {queryParams: {redir: this.router.url}});
return;
}
- this.cupboardService.getNeeds().subscribe(n => this.needs = n)
- const currentUser = this.usersService.getCurrentUser();
- if (currentUser) {
- this.user = currentUser;
- }
- this.getBasketNeeds();
- }
-
- getBasketNeeds(): void {
- if (this.user && this.user.basket) {
- this.user.basket.forEach(need => {
- // if (this.isInBasket(need)) {
- // this.basket.push(need);
- // }
- });
- }
- }
- isInBasket(need: Need): boolean {
- return this.basket.includes(need)
- }
-
- addNeed(need: Need, quantity: number = 1): void {
- if (this.user && !this.isInBasket(need)) {
- this.basket.push(need);
- this.need_quantity[need.id] = quantity;
- }
- if (this.isInBasket(need)) {
- this.need_quantity[need.id] += quantity;
- }
- this.needCount++;
- }
-
- removeNeed(need: Need, quantity: number = 1): void {
- if (this.user && this.isInBasket(need)) {
- this.need_quantity[need.id] -= quantity;
- if (this.need_quantity[need.id] === 0) {
- this.basket = this.basket.filter(n => need.id !== n.id);
- }
- this.needCount--;
- }
- }
+ this.usersService.refreshBasket();
+ // this.usersService.removeNeed(); <- call this to remove
+ }
- removeAllNeeds(): void {
- this.basket.forEach(need => {
- this.need_quantity = [];
- });
- this.basket = [];
- this.needCount = 0;
- }
- isBasketEmpty(): boolean {
- return this.needCount === 0;
- }
- checkout(): void {
- this.removeAllNeeds();
- }
}
diff --git a/ufund-ui/src/app/components/need-list/need-list.component.ts b/ufund-ui/src/app/components/need-list/need-list.component.ts
index 46e09f0..2764c7e 100644
--- a/ufund-ui/src/app/components/need-list/need-list.component.ts
+++ b/ufund-ui/src/app/components/need-list/need-list.component.ts
@@ -107,12 +107,12 @@ export class NeedListComponent {
add(need: Need) {
const currentUser = this.usersService.getCurrentUser();
- console.log("get current user in angular:", currentUser)
+ //console.log("get current user in angular:", currentUser)
if (currentUser) {
currentUser.basket.push(need.id);
- console.log("pushed to basket: " + currentUser.basket)
+ //console.log("pushed to basket: " + currentUser.basket)
this.usersService.updateUser(currentUser).subscribe(() => {
- console.log("rah");
+ this.usersService.refreshBasket();
error: (err: any) => {
console.error(err);
}
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts
index 2a95202..a0d316c 100644
--- a/ufund-ui/src/app/services/users.service.ts
+++ b/ufund-ui/src/app/services/users.service.ts
@@ -2,6 +2,8 @@ import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {BehaviorSubject, firstValueFrom, Observable} from 'rxjs';
import {User} from '../models/User';
+import { Need } from '../models/Need';
+import { CupboardService } from './cupboard.service';
@Injectable({
providedIn: 'root'
@@ -10,6 +12,7 @@ export class UsersService {
private currentUser : BehaviorSubject<User | null> = new BehaviorSubject<User | null>(null);
private apiKey: string = "";
+ private basket = new BehaviorSubject<Need[]>([]);
private url = "http://localhost:8080/users"
private authUrl = "http://localhost:8080/auth"
@@ -29,7 +32,8 @@ export class UsersService {
};
constructor(
- private http: HttpClient
+ private http: HttpClient,
+ private cupboardService: CupboardService,
) {}
async createUser(username:string, password:string) {
@@ -73,4 +77,18 @@ export class UsersService {
this.currentUser.next(currentU);
// this.currentUser.subscribe(r => console.log("currentUser: "+r.username))
}
+
+ refreshBasket() {
+ let promiseArr = this.getCurrentUser()!.basket.map(async needID => {
+ return await firstValueFrom(this.cupboardService.getNeed(needID));
+ })
+ Promise.all(promiseArr).then(r => this.basket.next(r));
+ }
+
+ removeNeed(id: number) {
+ let newArr = this.basket.getValue().filter(v => v.id != id);
+ this.basket.next(newArr);
+ this.getCurrentUser()!.basket = newArr.map(need => need.id);
+ this.updateUser(this.getCurrentUser()!);
+ }
}