From ee4154df85a971f3a0f8e43fd6afdfa6a620ea99 Mon Sep 17 00:00:00 2001
From: benal01 <bja4245@rit.edu>
Date: Thu, 27 Mar 2025 10:09:19 -0400
Subject: refactor change sort algo func with [(ngModel)] binding

---
 .../components/need-list/need-list.component.ts    | 25 ++++++++++------------
 1 file changed, 11 insertions(+), 14 deletions(-)

(limited to 'ufund-ui/src/app/components/need-list/need-list.component.ts')

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 97be0cb..177d9f7 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
@@ -38,8 +38,9 @@ export class NeedListComponent {
   needs: Need[] = [];
   searchResults: Need[] = [];
   sortMode = 'Ascending'
-  sortSelection: string = '';
+
   currentSortAlgo: sortAlgo = sortByGoal;
+  sortSelection: string = this.currentSortAlgo.name;
 
   SortingAlgoArrays: {func:sortAlgo,name:string, display:string[]}[] = [
     {func:sortByGoal,name:"sortByMaxGoal", display:["Max Goal", "Min Goal"]},
@@ -77,19 +78,6 @@ export class NeedListComponent {
     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 + this.sortMode)
-        return
-      }
-    });
-    this.refresh()
-    this.search(form);
-  }
-
   private searchDelay: any;
 
   async search(form: any) {
@@ -103,6 +91,15 @@ export class NeedListComponent {
       this.searchDelay = setTimeout(() => {
 
         if (form) {
+          //sorting based on algo selected
+          this.SortingAlgoArrays.forEach(algo => {
+            if(algo.name === this.sortSelection) {
+              this.currentSortAlgo = algo.func;
+              console.log("changed sorting algorithm to: ", algo.name + this.sortMode)
+              return
+            }
+          });
+
           const currentSearchValue = form.search; //latest value of the search
           this.cupboardService.searchNeeds(currentSearchValue).subscribe((n) => {
             if (this.sortMode == 'Ascending') {
-- 
cgit v1.2.3