import { Component, Input, Output } from '@angular/core'; import { Need, GoalType } from '../../models/Need'; import { CupboardService } from '../../services/cupboard.service'; import { catchError, of } from 'rxjs'; import { ToastsService, ToastType } from '../../services/toasts.service'; @Component({ selector: 'app-need-edit', standalone: false, templateUrl: './need-edit.component.html', styleUrl: './need-edit.component.css' }) export class NeedEditComponent { constructor( private cupboardService: CupboardService, private toastService: ToastsService ) {} @Input() selectedNeed!: Need; @Output() selectedNeedID: number | null = null; update(form: any) { console.log(form); const need: Need = { name: form.name, image: form.image, 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, description: form.description }; this.cupboardService.updateNeed(need.id, need) .pipe(catchError((ex, _) => { if (ex.status == 500) { this.toastService.sendToast(ToastType.ERROR, 'Fields cannot be blank'); } else if (ex.status == 400) { this.toastService.sendToast(ToastType.ERROR, ex.error); } else { this.toastService.sendToast(ToastType.ERROR, "Error on creating need"); } return of() })) .subscribe( (result) => { if (result) { console.log("need updated successfully"); // this.needList?.refresh() } else { console.log("need update failed"); } } ); } }