diff options
author | benal01 <bja4245@rit.edu> | 2025-03-17 18:25:19 -0400 |
---|---|---|
committer | benal01 <bja4245@rit.edu> | 2025-03-17 18:25:19 -0400 |
commit | 88f6a4174d7fcc53028b78d0d9b3d91b6d17d2c6 (patch) | |
tree | 363e124e6770f35c211752cb473c0318d7810f0a | |
parent | 59119fb799ea27d4bffcf793f768538ad202cd85 (diff) | |
download | JellySolutions-88f6a4174d7fcc53028b78d0d9b3d91b6d17d2c6.tar.gz JellySolutions-88f6a4174d7fcc53028b78d0d9b3d91b6d17d2c6.tar.bz2 JellySolutions-88f6a4174d7fcc53028b78d0d9b3d91b6d17d2c6.zip |
search needs with timeout to throttle api calls
4 files changed, 95 insertions, 4 deletions
diff --git a/ufund-ui/src/app/components/cupboard/cupboard.component.ts b/ufund-ui/src/app/components/cupboard/cupboard.component.ts index e3f33ac..adc38b0 100644 --- a/ufund-ui/src/app/components/cupboard/cupboard.component.ts +++ b/ufund-ui/src/app/components/cupboard/cupboard.component.ts @@ -19,7 +19,7 @@ needs: any; this.close(); this.openmenu(); } - + selectedNeed: any = { name: '', id: null, diff --git a/ufund-ui/src/app/components/need-list/need-list.component.css b/ufund-ui/src/app/components/need-list/need-list.component.css index 1bcaed9..bbc3f2c 100644 --- a/ufund-ui/src/app/components/need-list/need-list.component.css +++ b/ufund-ui/src/app/components/need-list/need-list.component.css @@ -7,10 +7,18 @@ } -li { +li, div { border: 2px solid #000; border-radius: 5px; padding: 5px; margin: 5px; -}
\ No newline at end of file +} + +#search-form { + background-color: #d9d9d9; + padding: 10px 20px 20px 20px; + border: 2px solid #000; + border-radius: 5px; + visibility: visible; + }
\ No newline at end of file diff --git a/ufund-ui/src/app/components/need-list/need-list.component.html b/ufund-ui/src/app/components/need-list/need-list.component.html index b8774f1..6dd6511 100644 --- a/ufund-ui/src/app/components/need-list/need-list.component.html +++ b/ufund-ui/src/app/components/need-list/need-list.component.html @@ -1,4 +1,23 @@ <h1>Needs List</h1> +<input id="search-button" type="button" value="Search" (click)="open()"> +<div id="search-form"> + <form #searchForm="ngForm"> + <label>Search:</label><br> + <input type="text" name="search" (input)="search(searchForm.value)" ngModel> + <input type="button" value="Clear" (click)="searchForm.reset()"> <br> + </form> + <button (click)="close()">Close</button> + <div> + <h2 id="search-status">Search Results:</h2> + <div *ngFor="let need of searchResults"> + <a routerLink="/need/{{need.id}}"> + {{need.name}} + </a> + <button (click)="delete(need.id)">Delete</button> + </div> + </div> +</div> + <li *ngFor="let need of needs"> <a routerLink="/need/{{need.id}}"> {{need.name}} diff --git a/ufund-ui/src/app/components/need-list/need-list.component.ts b/ufund-ui/src/app/components/need-list/need-list.component.ts index 579565c..8451d5b 100644 --- a/ufund-ui/src/app/components/need-list/need-list.component.ts +++ b/ufund-ui/src/app/components/need-list/need-list.component.ts @@ -10,13 +10,73 @@ import {CupboardService} from '../../services/cupboard.service'; }) export class NeedListComponent { needs: Need[] = []; - + searchResults: Need[] = []; + constructor( private cupboardService: CupboardService ) {} 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) { @@ -24,4 +84,8 @@ export class NeedListComponent { this.needs = this.needs.filter(n => n.id !== id) }) } + + back() { + this.searchResults = []; + } } |