import { Component, Input } 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'; interface sortAlgo { (a: Need,b: Need): number; } // sort functions const sortByName: sortAlgo = (a: Need, b: Need): number => { if(a.name.toLocaleLowerCase() < b.name.toLocaleLowerCase()) { return -1; } return 1; } const sortByNameReverse: sortAlgo = (a: Need, b: Need): number => { return sortByName(a,b)*-1; } const sortByMaxGoal: sortAlgo = (a: Need, b: Need): number => { if(a.maxGoal == b.maxGoal) { return sortByName(a,b); } else if(a.maxGoal > b.maxGoal) { return -1; } return 1; } const sortByMinGoal: sortAlgo = (a: Need, b: Need): number => { return sortByMaxGoal(a,b)*-1; } @Component({ selector: 'app-need-list', standalone: false, templateUrl: './need-list.component.html', styleUrl: './need-list.component.css' }) export class NeedListComponent { needs: Need[] = []; searchResults: Need[] = []; currentSortAlgo: sortAlgo = sortByMaxGoal; SortingAlgoArrays: {func:sortAlgo,name:string}[] = [ {func:sortByMaxGoal,name:"sortByMaxGoal"}, {func:sortByName,name:"sortByName"}, {func:sortByNameReverse,name:"sortByNameReverse"}, {func:sortByMinGoal,name:"sortByMinGoal"}, ]; constructor( private cupboardService: CupboardService, private usersService: UsersService ) {} refresh() { this.cupboardService.getNeeds().subscribe(n => this.needs = n.sort(this.currentSortAlgo)) } 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')); } changeSortAlgo(algoName: string, form: any) { console.log(algoName); this.SortingAlgoArrays.forEach(algo => { if(algo.name === algoName) { this.currentSortAlgo = algo.func; console.log("changed sorting algorithm to: ", algo.name) return } }); this.refresh() this.search(form); } 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 console.log("current search value: ", currentSearchValue) this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => { this.searchResults = n.sort(this.currentSortAlgo); 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: 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 = []; } }