aboutsummaryrefslogtreecommitdiff
path: root/ufund-ui/src/app/components/need-list/need-list.component.ts
blob: 4409b637f3ff077b3d162da1d2333c9d53837cc0 (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
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
  ) {}

  ngOnInit(): void {
    this.cupboardService.getNeeds().subscribe(n => this.needs = n)
    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'));
  }

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

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