import {Component, Input, OnInit, ViewChild} from '@angular/core'; import { CupboardService } from '../../services/cupboard.service'; 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'; @Component({ selector: 'app-cupboard', standalone: false, templateUrl: './cupboard.component.html', styleUrl: './cupboard.component.css' }) export class CupboardComponent implements OnInit { protected statusText = new BehaviorSubject("") selectedForm = "create"; needs: any; @ViewChild("needList") needList?: NeedListComponent; constructor(private cupboardService: CupboardService, private usersService: UsersService) { } ngOnInit(): void { this.cupboardService.getNeeds().subscribe(n => this.needs = n); if (this.isManager()) { console.log("Admin view of Cupboard"); } else { console.log("Limited helper view of Cupboard"); } } selectedNeed: any = { name: '', location:'', id: null, maxGoal: null, type: '', urgent: false }; selectedNeedId: number | null = null; searchResults: any[] = []; 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) }); } } } 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 }; } isManager() { 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, location: form.location, id: this.selectedNeed.id, //system will control this maxGoal: form.maxGoal, type: GoalType[form.type as keyof typeof GoalType], urgent: form.urgent, filterAttributes: [], current: 0 }; 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, location: form.location, id: 0, maxGoal: form.maxGoal, type: form.type, urgent: form.urgent ? true : false, 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 } = { 200: 'OK', 201: 'Created', 202: 'Accepted', 203: 'Non-Authoritative Information', 204: 'No Content', 205: 'Reset Content', 206: 'Partial Content', 300: 'Multiple Choices', 301: 'Moved Permanently', 302: 'Found', 303: 'See Other', 304: 'Not Modified', 305: 'Use Proxy', 306: 'Unused', 307: 'Temporary Redirect', 400: 'Bad Request', 401: 'Unauthorized', 402: 'Payment Required', 403: 'Forbidden', 404: 'Not Found', 405: 'Method Not Allowed', 406: 'Not Acceptable', 407: 'Proxy Authentication Required', 408: 'Request Timeout', 409: 'Conflict', 410: 'Gone', 411: 'Length Required', 412: 'Precondition Required', 413: 'Request Entry Too Large', 414: 'Request-URI Too Long', 415: 'Unsupported Media Type', 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', 502: 'Bad Gateway', 503: 'Service Unavailable', 504: 'Gateway Timeout', 505: 'HTTP Version Not Supported', };