diff options
Diffstat (limited to '')
-rw-r--r-- | ufund-ui/src/app/components/need-edit/need-edit.component.ts | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/ufund-ui/src/app/components/need-edit/need-edit.component.ts b/ufund-ui/src/app/components/need-edit/need-edit.component.ts new file mode 100644 index 0000000..2462534 --- /dev/null +++ b/ufund-ui/src/app/components/need-edit/need-edit.component.ts @@ -0,0 +1,61 @@ +import { Component, Input, Output, EventEmitter } 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() refreshNeedList = new EventEmitter<void>(); + + 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.refreshNeedList.emit(); + } else { + console.log("need update failed"); + } + } + + ); + } +}
\ No newline at end of file |