21364: added comment for lastQuery var Arvados-DCO-1.1-Signed-off-by: Lisa Knox...
[arvados.git] / services / workbench2 / src / store / search-results-panel / search-results-middleware-service.ts
index 2f64d76fb5ed5d0776d94c0f48352c2cb24e6ab1..866e148bcd73de296fd262231080c32bd34a78e1 100644 (file)
@@ -18,7 +18,6 @@ import {
     getSearchSessions,
     queryToFilters,
     getAdvancedDataFromQuery,
-    searchBarActions
 } from 'store/search-bar/search-bar-actions';
 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
 import { FilterBuilder, joinFilters } from 'services/api/filter-builder';
@@ -27,9 +26,11 @@ import { serializeResourceTypeFilters } from 'store//resource-type-filters/resou
 import { ProjectPanelColumnNames } from 'views/project-panel/project-panel';
 import { ResourceKind } from 'models/resource';
 import { ContainerRequestResource } from 'models/container-request';
+import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
+import { dataExplorerActions } from 'store/data-explorer/data-explorer-action';
 
 export class SearchResultsMiddlewareService extends DataExplorerMiddlewareService {
-    constructor(private services: ServiceRepository, id: string) {
+    constructor(private services: ServiceRepository, id: string, private responseMap: Record<string, number> = {}) {
         super(id);
     }
 
@@ -39,6 +40,11 @@ export class SearchResultsMiddlewareService extends DataExplorerMiddlewareServic
         const searchValue = state.searchBar.searchValue;
         const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
         const sessions = getSearchSessions(clusterId, state.auth.sessions);
+        const recentQueries = this.services.searchService.getRecentQueries();
+        //the last query is compared to the current query to check if the value has changed
+        //once the search button is clicked, the value is pushed to the recentQueries array
+        //therefore, the last query is the second to last element in the array 
+        const lastQuery = recentQueries[recentQueries.length - 2];
 
         if (searchValue.trim() === '') {
             return;
@@ -58,18 +64,31 @@ export class SearchResultsMiddlewareService extends DataExplorerMiddlewareServic
 
         const numberOfSessions = sessions.length;
         let numberOfResolvedResponses = 0;
-        api.dispatch(searchBarActions.SET_IS_SEARCHING(true));
+        let totalNumItemsAvailable = 0;
+        api.dispatch(progressIndicatorActions.START_WORKING(this.id))
+        api.dispatch(dataExplorerActions.SET_IS_NOT_FOUND({ id: this.id, isNotFound: false }));
+
+        //In SearchResultsPanel, if we don't reset the items available, the items available will
+        //will be added to the previous value every time the 'load more' button is clicked.
+        api.dispatch(resetItemsAvailable());
 
         sessions.forEach(session => {
             const params = getParams(dataExplorer, searchValue, session.apiRevision);
             this.services.groupsService.contents('', params, session)
                 .then((response) => {
+                    //if items were added or deleted, we ignore them for "load more" button purposes
+                    if (lastQuery === searchValue && this.responseMap[session.clusterId] && this.responseMap[session.clusterId] !== response.itemsAvailable) {
+                        response.itemsAvailable = this.responseMap[session.clusterId];
+                    }
+                    this.responseMap[session.clusterId] = response.itemsAvailable;
                     api.dispatch(updateResources(response.items));
                     api.dispatch(appendItems(response));
                     numberOfResolvedResponses++;
-                        if (numberOfResolvedResponses === numberOfSessions) {
-                            api.dispatch(searchBarActions.SET_IS_SEARCHING(false));
-                        }
+                    totalNumItemsAvailable += response.itemsAvailable;
+                    if (numberOfResolvedResponses === numberOfSessions) {
+                        api.dispatch(progressIndicatorActions.STOP_WORKING(this.id))
+                        if(totalNumItemsAvailable === 0) api.dispatch(dataExplorerActions.SET_IS_NOT_FOUND({ id: this.id, isNotFound: true }))
+                    }
                     // Request all containers for process status to be available
                     const containerRequests = response.items.filter((item) => item.kind === ResourceKind.CONTAINER_REQUEST) as ContainerRequestResource[];
                     const containerUuids = containerRequests.map(container => container.containerUuid).filter(uuid => uuid !== null) as string[];
@@ -84,7 +103,7 @@ export class SearchResultsMiddlewareService extends DataExplorerMiddlewareServic
                         });
                     }).catch(() => {
                         api.dispatch(couldNotFetchSearchResults(session.clusterId));
-                        api.dispatch(searchBarActions.SET_IS_SEARCHING(false));
+                        api.dispatch(progressIndicatorActions.STOP_WORKING(this.id))
                     });
             }
         );
@@ -130,6 +149,9 @@ export const setItems = (listResults: ListResults<GroupContentsResource>) =>
         items: listResults.items.map(resource => resource.uuid),
     });
 
+export const resetItemsAvailable = () =>
+    searchResultsPanelActions.RESET_ITEMS_AVAILABLE();
+
 export const appendItems = (listResults: ListResults<GroupContentsResource>) =>
     searchResultsPanelActions.APPEND_ITEMS({
         ...listResultsToDataExplorerItemsMeta(listResults),