Handle filtering in projectPanelMiddleware
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 4 Jul 2018 13:55:02 +0000 (15:55 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Wed, 4 Jul 2018 13:55:02 +0000 (15:55 +0200)
Feature #13703

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/views/project-panel/project-panel-middleware.ts

index 9be59811fff0e3055e2df95f468647d2e2957c06..8c1e8cc8d006085bbdd8ee13b75e2c6acfd9f3cc 100644 (file)
@@ -4,11 +4,13 @@
 
 import { Middleware } from "redux";
 import actions from "../../store/data-explorer/data-explorer-action";
 
 import { Middleware } from "redux";
 import actions from "../../store/data-explorer/data-explorer-action";
-import { PROJECT_PANEL_ID, columns } from "./project-panel";
+import { PROJECT_PANEL_ID, columns, ProjectPanelFilter } from "./project-panel";
 import { groupsService } from "../../services/services";
 import { RootState } from "../../store/store";
 import { groupsService } from "../../services/services";
 import { RootState } from "../../store/store";
-import { getDataExplorer } from "../../store/data-explorer/data-explorer-reducer";
-import { resourceToDataItem } from "./project-panel-item";
+import { getDataExplorer, DataExplorerState } from "../../store/data-explorer/data-explorer-reducer";
+import { resourceToDataItem, ProjectPanelItem } from "./project-panel-item";
+import FilterBuilder from "../../common/api/filter-builder";
+import { DataColumns } from "../../components/data-table/data-table";
 
 export const projectPanelMiddleware: Middleware = store => next => {
     next(actions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
 
 export const projectPanelMiddleware: Middleware = store => next => {
     next(actions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
@@ -30,26 +32,47 @@ export const projectPanelMiddleware: Middleware = store => next => {
             SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
             }),
             SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
             }),
+            SET_FILTERS: handleProjectPanelAction(() => {
+                store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
+            }),
             REQUEST_ITEMS: handleProjectPanelAction(() => {
                 const state = store.getState() as RootState;
                 const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
             REQUEST_ITEMS: handleProjectPanelAction(() => {
                 const state = store.getState() as RootState;
                 const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
-                groupsService
-                    .contents(state.projects.currentItemId, {
-                        limit: dataExplorer.rowsPerPage,
-                        offset: dataExplorer.page * dataExplorer.rowsPerPage,
-                    })
-                    .then(response => {
-                        store.dispatch(actions.SET_ITEMS({
-                            id: PROJECT_PANEL_ID,
-                            items: response.items.map(resourceToDataItem),
-                            itemsAvailable: response.itemsAvailable,
-                            page: Math.floor(response.offset / response.limit),
-                            rowsPerPage: response.limit
-                        }));
-                    });
-
+                const typeColumn = columns.find(c => c.name === "Type");
+                const typeFilters = getSelectedTypeFilters(dataExplorer.columns as DataColumns<ProjectPanelItem, ProjectPanelFilter>);
+                if (typeFilters.length > 0) {
+                    groupsService
+                        .contents(state.projects.currentItemId, {
+                            limit: dataExplorer.rowsPerPage,
+                            offset: dataExplorer.page * dataExplorer.rowsPerPage,
+                            filters: FilterBuilder.create().addIsA("uuid", typeFilters.map(f => f.type))
+                        })
+                        .then(response => {
+                            store.dispatch(actions.SET_ITEMS({
+                                id: PROJECT_PANEL_ID,
+                                items: response.items.map(resourceToDataItem),
+                                itemsAvailable: response.itemsAvailable,
+                                page: Math.floor(response.offset / response.limit),
+                                rowsPerPage: response.limit
+                            }));
+                        });
+                } else {
+                    store.dispatch(actions.SET_ITEMS({
+                        id: PROJECT_PANEL_ID,
+                        items: [],
+                        itemsAvailable: 0,
+                        page: 0,
+                        rowsPerPage: dataExplorer.rowsPerPage
+                    }));
+                }
             }),
             default: () => next(action)
         });
     };
 };
             }),
             default: () => next(action)
         });
     };
 };
+
+const getSelectedTypeFilters = (columns: DataColumns<ProjectPanelItem, ProjectPanelFilter>) => {
+    const typeColumn = columns.find(c => c.name === "Type");
+    return typeColumn && typeColumn.filters ? typeColumn.filters.filter(f => f.selected) : [];
+};
+