aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/cupboard/cupboard.component.ts
blob: a4706b3dd42db91e4443bf522467a8b95c88e528 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import {Component, OnInit, ViewChild} from '@angular/core';
import { CupboardService } from '../../services/cupboard.service';
import { Need } from '../../models/Need';
import { userType } from '../../models/User';
import { catchError, of } from 'rxjs';
import { NeedListComponent } from '../need-list/need-list.component';
import {AuthService} from '../../services/auth.service';
import {ToastsService, ToastType} from '../../services/toasts.service';

@Component({
    selector: 'app-cupboard',
    standalone: false,
    templateUrl: './cupboard.component.html',
    styleUrl: './cupboard.component.css'
})

export class CupboardComponent implements OnInit {

    selectedForm?: string = undefined;
    needs: any;
    @ViewChild("needList") needList?: NeedListComponent

    constructor(
        private cupboardService: CupboardService,
        private authService: AuthService,
        private toastService: ToastsService
    ) {}

    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.authService.getCurrentUser()?.type;
        return type === ("MANAGER" as unknown as userType);
    }

    submit(form: any) {
        const need: Need = {
            name: form.name,
            image: form.image,
            location: form.location,
            id: 0,
            maxGoal: form.maxGoal,
            type: form.type,
            urgent: !!form.urgent,
            filterAttributes: [],
            current: 0,
            description: form.description
        };
        console.log("need:", need);
        console.log("form submitted. creating need: ", need);
        this.cupboardService.createNeed(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 created successfully");
                        this.needList?.refresh()
                    } else {
                        console.log("need creation failed");
                    }
                }

            );
    }
}