From 786a6db3f5a22f7bb2cc249731ef654b675c3c86 Mon Sep 17 00:00:00 2001 From: benal01 Date: Mon, 31 Mar 2025 22:15:37 -0400 Subject: moving functionality to new component --- .../components/need-edit/need-edit.component.ts | 61 ++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 ufund-ui/src/app/components/need-edit/need-edit.component.ts (limited to 'ufund-ui/src/app/components/need-edit/need-edit.component.ts') 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..02ffb71 --- /dev/null +++ b/ufund-ui/src/app/components/need-edit/need-edit.component.ts @@ -0,0 +1,61 @@ +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"); + } + } + + ); + } +} \ No newline at end of file -- cgit v1.2.3 From 4186983da13740ec0a4731770ac238922631be11 Mon Sep 17 00:00:00 2001 From: benal01 Date: Mon, 31 Mar 2025 22:52:49 -0400 Subject: auto refresh with emitter --- ufund-ui/src/app/components/need-edit/need-edit.component.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ufund-ui/src/app/components/need-edit/need-edit.component.ts') 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 index 02ffb71..2462534 100644 --- a/ufund-ui/src/app/components/need-edit/need-edit.component.ts +++ b/ufund-ui/src/app/components/need-edit/need-edit.component.ts @@ -1,4 +1,4 @@ -import { Component, Input, Output } from '@angular/core'; +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'; @@ -18,7 +18,7 @@ export class NeedEditComponent { ) {} @Input() selectedNeed!: Need; - @Output() selectedNeedID: number | null = null; + @Output() refreshNeedList = new EventEmitter(); update(form: any) { console.log(form); @@ -50,7 +50,7 @@ export class NeedEditComponent { (result) => { if (result) { console.log("need updated successfully"); - // this.needList?.refresh() + this.refreshNeedList.emit(); } else { console.log("need update failed"); } -- cgit v1.2.3