aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/cupboard/cupboard.component.ts
diff options
context:
space:
mode:
Diffstat (limited to 'ufund-ui/src/app/components/cupboard/cupboard.component.ts')
-rw-r--r--ufund-ui/src/app/components/cupboard/cupboard.component.ts121
1 files changed, 38 insertions, 83 deletions
diff --git a/ufund-ui/src/app/components/cupboard/cupboard.component.ts b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
index a812baf..2230cd3 100644
--- a/ufund-ui/src/app/components/cupboard/cupboard.component.ts
+++ b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
@@ -1,10 +1,11 @@
-import { Component, OnInit, ViewChild } from '@angular/core';
+import {Component, OnInit, ViewChild} from '@angular/core';
import { CupboardService } from '../../services/cupboard.service';
import { Need, GoalType } from '../../models/Need';
import { userType } from '../../models/User';
-import { BehaviorSubject, catchError, of } from 'rxjs';
+import { catchError, of } from 'rxjs';
import { NeedListComponent } from '../need-list/need-list.component';
import {AuthService} from '../../services/auth.service';
+import {ToastsService, ToastType} from '../../services/toasts.service';
@Component({
selector: 'app-cupboard',
@@ -15,21 +16,18 @@ import {AuthService} from '../../services/auth.service';
export class CupboardComponent implements OnInit {
- protected statusText = new BehaviorSubject("")
-
+ selectedForm?: string = undefined;
needs: any;
@ViewChild("needList") needList?: NeedListComponent
constructor(
private cupboardService: CupboardService,
- private authService: AuthService
+ private authService: AuthService,
+ private toastService: ToastsService
) {}
ngOnInit(): void {
this.cupboardService.getNeeds().subscribe(n => this.needs = n);
- this.close();
- this.openmenu();
-
if (this.isManager()) {
console.log("Admin view of Cupboard");
} else {
@@ -39,54 +37,43 @@ export class CupboardComponent implements OnInit {
selectedNeed: any = {
name: '',
+ location:'',
id: null,
maxGoal: null,
- type: ''
+ type: '',
+ urgent: false
};
selectedNeedId: number | null = null;
+ searchResults: any[] = [];
- private hideElement(element: any) {
- if (element) {
- element.style.visibility = 'hidden';
- element.style.position = 'absolute';
+ selectForm(name: string) {
+ //get search results from the need list
+ if (this.needList) {
+ this.searchResults = this.needList.searchResults;
}
- }
+ console.log(this.searchResults)
+ this.selectedForm = name;
+ if (name == 'update') {
+ if (this.searchResults) {
+ this.searchResults.forEach((element: any) => {
+ console.log(element)
+ });
+ }
- private showElement(element: any) {
- if (element) {
- element.style.visibility = 'visible';
- element.style.position = 'relative';
}
}
- openmenu() {
- const menuElement = document.getElementById('menu');
- this.showElement(menuElement);
- }
-
- opencreate() {
- this.close();
- this.showElement(document.getElementById('create-form'));
- }
-
- openupdate() {
- this.close();
- this.showElement(document.getElementById('update-form'));
- }
-
- back() {
- this.close();
- this.openmenu();
- }
-
- close() {
- this.hideElement(document.getElementById('create-form'));
- this.hideElement(document.getElementById('destroy-form'));
- this.hideElement(document.getElementById('menu'));
- this.hideElement(document.getElementById('update-form'));
+ async updateSearchResults() {
+ if (this.needList) {
+ while (this.selectedForm == 'update') {
+ this.searchResults = this.needList.searchResults
+ await new Promise(resolve => setTimeout(resolve, 100));
+ }
+ }
}
populateForm(need: any): void {
+ this.selectForm('update');
this.selectedNeed = { ...need };
}
@@ -95,61 +82,29 @@ export class CupboardComponent implements OnInit {
return type === ("MANAGER" as unknown as userType);
}
- update(form: any) {
- console.log(form);
- const need: Need = {
- name: form.name,
- id: form.id, //system will control this
- maxGoal: form.maxGoal,
- type: GoalType[form.type as keyof typeof GoalType],
- filterAttributes: [],
- current: 0
- };
- console.log("need:", need);
- console.log(need.id, need, "need updated");
- this.cupboardService.updateNeed(need.id, need)
- .pipe(catchError((ex, _) => {
- 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 of()
- }))
- .subscribe(
- (result) => {
- if (result) {
- console.log("need updated successfully");
- this.needList?.refresh()
- } else {
- console.log("need update failed");
- }
- }
-
- );
- }
-
submit(form: any) {
const need: Need = {
name: form.name,
+ image: form.image,
+ location: form.location,
id: 0,
maxGoal: form.maxGoal,
type: form.type,
+ urgent: form.urgent ? true : false,
filterAttributes: [],
- current: 0
+ current: 0,
+ description: form.description
};
console.log("need:", need);
console.log("form submitted. creating need: ", need);
this.cupboardService.createNeed(need)
.pipe(catchError((ex, _) => {
if (ex.status == 500) {
- this.statusText.next("Fields cannot be blank");
+ this.toastService.sendToast(ToastType.ERROR, "Fields cannot be blank");
} else if (ex.status == 400) {
- this.statusText.next("Goal must be greater than 0");
+ this.toastService.sendToast(ToastType.ERROR, ex.error);
} else {
- this.statusText.next("Error on creating need");
+ this.toastService.sendToast(ToastType.ERROR, "Error on creating need");
}
return of()
}))