aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/need-list/need-list.component.ts
blob: e1c0de134bd467c3602ba2081333e113c8bcf67d (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
124
125
126
127
128
129
import { Component } from '@angular/core';
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)
    }

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

  private showElement(element: any) {
    if (element){
      element.style.visibility = 'visible';
      element.style.position = 'relative';
    }
  }

  private hideElement(element: any) {
    if (element){
      element.style.visibility = 'hidden';
      element.style.position = 'absolute';
    }
  }

  private updateSearchStatus(text: string) {
    let element = document.getElementById('search-status');
    if (element) {
      element.innerHTML = text;
    }
  }

  open() {
    this.hideElement(document.getElementById('search-button'));
    this.showElement(document.getElementById('search-form'));
  }

  close() {
    this.hideElement(document.getElementById('search-form'));
    this.showElement(document.getElementById('search-button'));
    this.hideElement(document.getElementById('search-status'));
  }

  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);
    }

    this.searchDelay = setTimeout(() => {
      const currentSearchValue = form.search; //latest value of the search
      this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => {
        this.searchResults = n;
        console.log(currentSearchValue, this.searchResults);
        this.showElement(document.getElementById('search-results'));
        this.showElement(document.getElementById('search-status'));
        if (this.searchResults.length === this.needs.length) {
          this.updateSearchStatus("Please refine your search");
          this.searchResults = [];
        } else if (this.searchResults.length === 0) {
          this.updateSearchStatus("No results found");
        } else {
          this.updateSearchStatus("Search results:");
        }
      });
    }, 250);
  }

  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: number) {
    const currentUser = this.usersService.getCurrentUser();
    if (currentUser) {
      this.usersService.updateUser(currentUser.username, currentUser).subscribe(() => {
        const currentUser = this.usersService.getCurrentUser();
        if (currentUser && currentUser.basket) {
          currentUser.basket.push(need);
          console.log("added to basket");
        }
        error: (err: any) => {
          console.error(err);
        }
      });

    }

  }

  back() {
    this.searchResults = [];
  }
}