aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/need-edit/need-edit.component.ts
blob: 24625343d4c92c0044127e13966331ca60dc6ed7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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");
                }
            }

        );
  }
}