aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java2
-rw-r--r--ufund-ui/src/app/components/dashboard/dashboard.component.ts37
-rw-r--r--ufund-ui/src/app/services/users.service.ts13
3 files changed, 40 insertions, 12 deletions
diff --git a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
index c6e622c..6953276 100644
--- a/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
+++ b/ufund-api/src/main/java/com/ufund/api/ufundapi/controller/UserController.java
@@ -108,7 +108,7 @@ public class UserController {
try {
authService.keyHasAccessToCupboard(key);
- int count = userService.getUserCount();
+ String count = String.valueOf(userService.getUserCount());
return new ResponseEntity<>(count, HttpStatus.OK);
} catch (IllegalAccessException ex) {
LOG.log(Level.WARNING, ex.getLocalizedMessage());
diff --git a/ufund-ui/src/app/components/dashboard/dashboard.component.ts b/ufund-ui/src/app/components/dashboard/dashboard.component.ts
index c94b5c6..8c397ff 100644
--- a/ufund-ui/src/app/components/dashboard/dashboard.component.ts
+++ b/ufund-ui/src/app/components/dashboard/dashboard.component.ts
@@ -1,10 +1,10 @@
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';
+import {BehaviorSubject} from 'rxjs';
+import {GoalType, Need} from '../../models/Need';
@Component({
selector: 'app-dashboard',
@@ -14,9 +14,10 @@ import {UsersService} from '../../services/users.service';
})
export class DashboardComponent implements OnInit{
- topNeeds?: Need[]
- almostThere?: Need[]
- inBasket?: Need[]
+ protected count = new BehaviorSubject<number | undefined>(undefined)
+ protected totalDonations = new BehaviorSubject<number | undefined>(undefined)
+ protected fulfilledNeeds = new BehaviorSubject<number | undefined>(undefined)
+ protected mostFulfilledNeeds = new BehaviorSubject<Need[] | undefined>(undefined)
constructor(
protected authService: AuthService,
@@ -32,14 +33,28 @@ export class DashboardComponent implements OnInit{
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.getCount().subscribe(count => this.count.next(count))
+ this.cupboardService.getNeeds().subscribe(needs => {
+ let fulfilledNeeds = 0
+ let totalValue = 0
+ for (let need of needs) {
+ let needPercent = need.current / need.maxGoal
+ if (needPercent >= 1) {
+ fulfilledNeeds++
+ this.fulfilledNeeds.next(fulfilledNeeds)
+ }
+ if (need.type === GoalType.MONETARY) {
+ totalValue += need.current
+ this.totalDonations.next(totalValue)
+ }
- this.userService.getBasket().subscribe(r => {
- this.inBasket = r;
+ }
+ needs.sort((a, b) => b.current/b.maxGoal - a.current/a.maxGoal)
+ needs = needs.filter(a => a.current != 0)
+ this.mostFulfilledNeeds.next(needs.slice(0, 5))
})
+
+
}
}
diff --git a/ufund-ui/src/app/services/users.service.ts b/ufund-ui/src/app/services/users.service.ts
index 4080ebf..080c394 100644
--- a/ufund-ui/src/app/services/users.service.ts
+++ b/ufund-ui/src/app/services/users.service.ts
@@ -21,6 +21,15 @@ export class UsersService {
})
});
+ httpOptions2 = () => ({
+ headers: new HttpHeaders({
+ 'Content-Type': 'application/json',
+ "jelly-api-key": this.authService.getApiKey()
+ }),
+ responseType: "text" as "json" // don't ask me how or why this works, bc i have no clue...
+ // see the relevant angular bug report https://github.com/angular/angular/issues/18586
+ });
+
constructor(
private http: HttpClient,
private cupboardService: CupboardService,
@@ -35,6 +44,10 @@ export class UsersService {
return this.http.get<User>(`${this.url}/${id}`, this.httpOptions())
}
+ getCount(): Observable<number> {
+ return this.http.get<number>(`${this.url}/count`, this.httpOptions2())
+ }
+
updateUser(user: User): Observable<User> {
console.log(`${this.url}/${user.username}`, user, this.httpOptions)
return this.http.put<User>(`${this.url}/${user.username}`, user, this.httpOptions())