aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/need-list/need-list.component.ts
blob: 3f77df4e9147e5e565fbba217a39a992f9005f05 (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
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
import {Need} from '../../models/Need';
import {CupboardService} from '../../services/cupboard.service';
import { UsersService } from '../../services/users.service';
import { userType } from '../../models/User';
@Component({
  selector: 'app-need-list',
  standalone: false,
  templateUrl: './need-list.component.html',
  styleUrl: './need-list.component.css'
})
export class NeedListComponent {
  needs: Need[] = [];
  searchResults: Need[] = [];

  constructor(
    private cupboardService: CupboardService,
    private usersService: UsersService
  ) {}

    refresh() {
        this.cupboardService.getNeeds().subscribe(n => {
          this.needs = n;
          this.searchResults = this.needs;
        });
        console.log(this.searchResults);
    }

  ngOnInit(): void {
    this.refresh()
  }

  private searchDelay: any;

  async search(form: any) {
    //wait .25 seconds before searching but cancel if another search is made during the wait to prevent too many api calls

    //remove previous search if it exists
    if (this.searchDelay) {
      clearTimeout(this.searchDelay);
    }
    if (form) {
      this.searchDelay = setTimeout(() => {
        
        if (form) {
          const currentSearchValue = form.search; //latest value of the search
          this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => {
            this.searchResults = n;
            console.log(currentSearchValue, this.searchResults);
            });
          }
        }, 250);
      } else {
        //user has cleared the search bar, we can skip the timeout for a 1/4 second faster response
        //clear timeout to stop pending search
        clearTimeout(this.searchDelay);
        this.searchResults = this.needs;
      }
      

     
  }

  delete(id : number) {
    this.cupboardService.deleteNeed(id).subscribe(() => {
      this.needs = this.needs.filter(n => n.id !== id)
    })
  }

  isManager() {
    const type = this.usersService.getCurrentUser()?.type;
    return type === ("MANAGER" as unknown as userType);
  }

  isHelper() {
    const type = this.usersService.getCurrentUser()?.type;
    return type === ("HELPER" as unknown as userType);
  }

  add(need: Need) {
    const currentUser = this.usersService.getCurrentUser();
    //console.log("get current user in angular:", currentUser)
    if (currentUser) {
      if (!currentUser.basket.includes(need.id)) {
        currentUser.basket.push(need.id);
        this.usersService.updateUser(currentUser).subscribe(() => {
          this.usersService.refreshBasket();
          error: (err: any) => {
            console.error(err);
          }
        });
      } else {
        window.alert("This need is already in your basket!")
      }
      

    }

  }

  back() {
    this.searchResults = this.needs;
  }
}