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.ts205
1 files changed, 109 insertions, 96 deletions
diff --git a/ufund-ui/src/app/components/cupboard/cupboard.component.ts b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
index 9574de3..24b3e2d 100644
--- a/ufund-ui/src/app/components/cupboard/cupboard.component.ts
+++ b/ufund-ui/src/app/components/cupboard/cupboard.component.ts
@@ -4,13 +4,13 @@ import { UsersService } from '../../services/users.service';
import { Need, GoalType } from '../../models/Need';
import { userType } from '../../models/User';
import { BehaviorSubject, catchError, of } from 'rxjs';
-import {NeedListComponent} from '../need-list/need-list.component';
+import { NeedListComponent } from '../need-list/need-list.component';
@Component({
- selector: 'app-cupboard',
- standalone: false,
- templateUrl: './cupboard.component.html',
- styleUrl: './cupboard.component.css'
+ selector: 'app-cupboard',
+ standalone: false,
+ templateUrl: './cupboard.component.html',
+ styleUrl: './cupboard.component.css'
})
export class CupboardComponent implements OnInit {
@@ -23,140 +23,152 @@ export class CupboardComponent implements OnInit {
constructor(private cupboardService: CupboardService, private usersService: UsersService) { }
ngOnInit(): void {
- this.cupboardService.getNeeds().subscribe(n => this.needs = n);
- this.close();
- this.openmenu();
-
- if (this.isManager()) {
- console.log("Admin view of Cupboard");
- } else {
- console.log("Limited helper view of Cupboard");
- }
+ this.cupboardService.getNeeds().subscribe(n => this.needs = n);
+ this.close();
+ this.openmenu();
+
+ if (this.isManager()) {
+ console.log("Admin view of Cupboard");
+ } else {
+ console.log("Limited helper view of Cupboard");
+ }
}
selectedNeed: any = {
- name: '',
- id: null,
- maxGoal: null,
- type: ''
+ name: '',
+ id: null,
+ maxGoal: null,
+ type: ''
};
selectedNeedId: number | null = null;
private hideElement(element: any) {
- if (element){
- element.style.visibility = 'hidden';
- element.style.position = 'absolute';
- }
+ if (element) {
+ element.style.visibility = 'hidden';
+ element.style.position = 'absolute';
+ }
}
private showElement(element: any) {
- if (element){
- element.style.visibility = 'visible';
- element.style.position = 'relative';
- }
+ if (element) {
+ element.style.visibility = 'visible';
+ element.style.position = 'relative';
+ }
}
openmenu() {
- const menuElement = document.getElementById('menu');
- this.showElement(menuElement);
+ const menuElement = document.getElementById('menu');
+ this.showElement(menuElement);
}
opencreate() {
- this.close();
- this.showElement(document.getElementById('create-form'));
+ this.close();
+ this.showElement(document.getElementById('create-form'));
}
openupdate() {
- this.close();
- this.showElement(document.getElementById('update-form'));
+ this.close();
+ this.showElement(document.getElementById('update-form'));
}
back() {
- this.close();
- this.openmenu();
+ 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'));
+ this.hideElement(document.getElementById('create-form'));
+ this.hideElement(document.getElementById('destroy-form'));
+ this.hideElement(document.getElementById('menu'));
+ this.hideElement(document.getElementById('update-form'));
}
populateForm(need: any): void {
- this.selectedNeed = { ...need };
+ this.selectedNeed = { ...need };
}
isManager() {
- const type = this.usersService.getCurrentUser()?.type;
- return type === ("MANAGER" as unknown as userType);
+ const type = this.usersService.getCurrentUser()?.type;
+ 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, r) => {
- this.statusText.next("Max goal must be greater than 0 " + friendlyHttpStatus[ex.status])
- return of()
- }))
- .subscribe(
- (result) => {
- if (result) {
- console.log("need updated successfully");
- this.needList?.refresh()
- } else {
- console.log("need update failed");
- }
- }
-
- );
+ 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, r) => {
+ 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,
- id: 0,
- maxGoal: form.maxGoal,
- type: form.type,
- filterAttributes: [],
- current: 0
- };
- console.log("need:", need);
- console.log("form submitted. creating need: ", need);
- this.cupboardService.createNeed(need)
- .pipe(catchError((ex, r) => {
- this.statusText.next("Max goal must be greater than 0 " + friendlyHttpStatus[ex.status])
- return of()
- }))
- .subscribe(
- (result) => {
- if (result) {
- console.log("need created successfully");
- this.needList?.refresh()
- } else {
- console.log("need creation failed");
- }
- }
-
- );
+ const need: Need = {
+ name: form.name,
+ id: 0,
+ maxGoal: form.maxGoal,
+ type: form.type,
+ filterAttributes: [],
+ current: 0
+ };
+ console.log("need:", need);
+ console.log("form submitted. creating need: ", need);
+ this.cupboardService.createNeed(need)
+ .pipe(catchError((ex, r) => {
+ 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 created successfully");
+ this.needList?.refresh()
+ } else {
+ console.log("need creation failed");
+ }
+ }
+
+ );
}
destroy() {
}
- }
+}
-let friendlyHttpStatus: {[key: number]: string} = {
+let friendlyHttpStatus: { [key: number]: string } = {
200: 'OK',
201: 'Created',
202: 'Accepted',
@@ -191,6 +203,7 @@ let friendlyHttpStatus: {[key: number]: string} = {
416: 'Requested Range Not Satisfiable',
417: 'Expectation Failed',
418: 'I\'m a teapot',
+ 422: 'Unprocessable Entity',
429: 'Too Many Requests',
500: 'Internal Server Error',
501: 'Not Implemented',