From 65590e6dfbed90e4acd342a72feb8d7b5120d70c Mon Sep 17 00:00:00 2001 From: benal01 Date: Thu, 27 Mar 2025 09:49:41 -0400 Subject: refactor sorting to use acending descending toggle --- .../components/need-list/need-list.component.html | 5 ++- .../components/need-list/need-list.component.ts | 46 +++++++++++++--------- 2 files changed, 31 insertions(+), 20 deletions(-) (limited to 'ufund-ui/src') 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 a653527..5be915c 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 @@ -3,9 +3,12 @@ +

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 2fdd993..97be0cb 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 @@ -17,11 +17,7 @@ const sortByName: sortAlgo = (a: Need, b: Need): number => { return 1; } -const sortByNameReverse: sortAlgo = (a: Need, b: Need): number => { - return sortByName(a,b)*-1; -} - -const sortByMaxGoal: sortAlgo = (a: Need, b: Need): number => { +const sortByGoal: sortAlgo = (a: Need, b: Need): number => { if(a.maxGoal == b.maxGoal) { return sortByName(a,b); } @@ -31,10 +27,6 @@ const sortByMaxGoal: sortAlgo = (a: Need, b: Need): number => { return 1; } -const sortByMinGoal: sortAlgo = (a: Need, b: Need): number => { - return sortByMaxGoal(a,b)*-1; -} - @Component({ selector: 'app-need-list', standalone: false, @@ -45,14 +37,13 @@ export class NeedListComponent { selectedNeed: Need | null = null; needs: Need[] = []; searchResults: Need[] = []; + sortMode = 'Ascending' sortSelection: string = ''; - currentSortAlgo: sortAlgo = sortByMaxGoal; + currentSortAlgo: sortAlgo = sortByGoal; - SortingAlgoArrays: {func:sortAlgo,name:string,display:string}[] = [ - {func:sortByMaxGoal,name:"sortByMaxGoal", display:"Max Goal"}, - {func:sortByName,name:"sortByName", display:"Name"}, - {func:sortByNameReverse,name:"sortByNameReverse", display:"Name (reverse)"}, - {func:sortByMinGoal,name:"sortByMinGoal", display:"Min Goal"}, + SortingAlgoArrays: {func:sortAlgo,name:string, display:string[]}[] = [ + {func:sortByGoal,name:"sortByMaxGoal", display:["Max Goal", "Min Goal"]}, + {func:sortByName,name:"sortByName", display:["Name", "Name (Descending)"]}, ]; @Output() currentNeed = new EventEmitter(); @@ -64,22 +55,34 @@ export class NeedListComponent { refresh() { this.cupboardService.getNeeds().subscribe(n => { - this.needs = n.sort(this.currentSortAlgo); + if (this.sortMode == 'Ascending') { + this.needs = n.sort(this.currentSortAlgo); + } else { + this.needs = n.sort(this.currentSortAlgo).reverse(); + } this.searchResults = this.needs; }); - console.log(this.searchResults); } ngOnInit(): void { this.refresh() } + changeSortMode(form : any) { + if (this.sortMode == 'Ascending'){ + this.sortMode = 'Descending' + } else { + this.sortMode = 'Ascending' + } + this.search(form) + } + 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) + console.log("changed sorting algorithm to: ", algo.name + this.sortMode) return } }); @@ -102,7 +105,12 @@ export class NeedListComponent { if (form) { const currentSearchValue = form.search; //latest value of the search this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => { - this.searchResults = n.sort(this.currentSortAlgo); + if (this.sortMode == 'Ascending') { + this.searchResults = n.sort(this.currentSortAlgo); + } else { + this.searchResults = n.sort(this.currentSortAlgo).reverse(); + } + console.log(currentSearchValue, this.searchResults); }); } -- cgit v1.2.3