From 77f935cd27801ce140addefa13986a8c06259279 Mon Sep 17 00:00:00 2001
From: Daniel Nemergut <dnemergu@nrao.edu>
Date: Thu, 20 Jul 2023 11:51:16 -0400
Subject: [PATCH] Made use of the activeRequestsObserver to poll the
 capability_request service in only one location

---
 .../active-capability-requests.component.ts   | 83 ++++++++-----------
 1 file changed, 35 insertions(+), 48 deletions(-)

diff --git a/apps/web/src/app/workspaces/components/active-capability-requests/active-capability-requests.component.ts b/apps/web/src/app/workspaces/components/active-capability-requests/active-capability-requests.component.ts
index 217855150..58091a418 100644
--- a/apps/web/src/app/workspaces/components/active-capability-requests/active-capability-requests.component.ts
+++ b/apps/web/src/app/workspaces/components/active-capability-requests/active-capability-requests.component.ts
@@ -127,11 +127,22 @@ export class ActiveCapabilityRequestsComponent implements OnInit, OnDestroy {
   // Observers
   private activeRequestsObserver = {
     next: (request) => {
+      // Set active requests and perform sorting
       this.activeCapabilityRequests = request.active_requests;
+      this.sortedActiveRequests$ = this.sortDirection$.pipe(
+        map((sort) => {
+          this.activeCapabilityRequests = this.filterFrontendFilters(this.activeCapabilityRequests);
+          this.pageAllVal = this.activeCapabilityRequests.length;
+          return !sort.col
+            ? this.activeCapabilityRequests
+            : this.sortByColumn(this.activeCapabilityRequests, sort.col, sort.dir);
+        }),
+      );
     },
     error: (error) =>
       console.error("Error when retrieving list of active capability requests:", error),
   };
+
   public capabilityObserver = {
     next: (capability) => {
       this.capability = capability;
@@ -236,6 +247,20 @@ export class ActiveCapabilityRequestsComponent implements OnInit, OnDestroy {
       )
       .subscribe(this.capabilityObserver);
 
+    // Set up the observer to receive active requests, this triggers the initial sorting
+    this.pollingDataUpdaterService
+      .getDataPoller$(
+        this.capabilityRequestService.getActiveCapabilityRequestsUrl(
+          this.capabilityName,
+          this.filters,
+        ),
+      )
+      .pipe(
+        takeUntil(this.ngUnsubscribe),
+        repeatWhen(() => this.ngUnsubscribe),
+      )
+      .subscribe(this.activeRequestsObserver);
+
     const qaStaffObserver = {
       next: (req) => {
         // this will distinguish Stage 1 and Stage 2 reviewers in this.qaStaff
@@ -259,21 +284,6 @@ export class ActiveCapabilityRequestsComponent implements OnInit, OnDestroy {
     this.setupAvailableFilters();
     this.checkFilters();
 
-    // Initial sorting
-    this.sortedActiveRequests$ = combineLatest([
-      this.getActiveCapabilityRequests$(),
-      this.sortDirection$,
-    ]).pipe(
-      map(([list, sort]) => {
-        list.active_requests = this.filterFrontendFilters(list.active_requests);
-        // Set max page size while we already have the count
-        this.pageAllVal = list.active_requests.length;
-        return !sort.col
-          ? list.active_requests
-          : this.sortByColumn(list.active_requests, sort.col, sort.dir);
-      }),
-    );
-
     // Get autocomplete suggestion list
     this.filteredStage2QaStaff = this.stage2QaControl.valueChanges.pipe(
       startWith(""),
@@ -455,21 +465,6 @@ export class ActiveCapabilityRequestsComponent implements OnInit, OnDestroy {
     // set capabilityName from dropdown
     this.capabilityName = capabilityName;
 
-    // Sorting
-    this.sortedActiveRequests$ = combineLatest([
-      this.getActiveCapabilityRequests$(),
-      this.sortDirection$,
-    ]).pipe(
-      map(([list, sort]) => {
-        list.active_requests = this.filterFrontendFilters(list.active_requests);
-        // Set max page size while we already have the count
-        this.pageAllVal = list.active_requests.length;
-        return !sort.col
-          ? list.active_requests
-          : this.sortByColumn(list.active_requests, sort.col, sort.dir);
-      }),
-    );
-
     // Store the current capability selection so the header can access it
     this.storageService.saveSession(WsHeaderComponent.previousCapabilityKey, this.capabilityName);
 
@@ -604,26 +599,18 @@ export class ActiveCapabilityRequestsComponent implements OnInit, OnDestroy {
     this.handleFilterSets(e, workingSet);
     this.handleNewParams(e, existingQueryParams);
 
-    // Filtering and Sorting
+    // Set filters that will be applied when this component refreshes
     this.filters = this.newParams;
-    this.sortedActiveRequests$ = combineLatest([
-      this.getActiveCapabilityRequests$(),
-      this.sortDirection$,
-    ]).pipe(
-      map(([list, sort]) => {
-        list.active_requests = this.filterFrontendFilters(list.active_requests);
-        // Set max page size while we already have the count
-        this.pageAllVal = list.active_requests.length;
-        return !sort.col
-          ? list.active_requests
-          : this.sortByColumn(list.active_requests, sort.col, sort.dir);
-      }),
-    );
 
-    this.router.navigate(["."], {
-      relativeTo: this.route,
-      queryParams: this.newParams,
-    });
+    this.router
+      .navigate(["."], {
+        relativeTo: this.route,
+        queryParams: this.newParams,
+      })
+      .then(() => {
+        // TODO: There's probably a better way to refresh this component without a reload
+        window.location.reload();
+      });
   }
 
   handleNewParams(filter: Filter, existingQueryParams: any) {
-- 
GitLab