Implement filtering by status
[arvados-workbench2.git] / src / views / project-panel / project-panel-middleware.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Middleware } from "redux";
6 import actions from "../../store/data-explorer/data-explorer-action";
7 import { PROJECT_PANEL_ID, columns, ProjectPanelFilter } from "./project-panel";
8 import { groupsService } from "../../services/services";
9 import { RootState } from "../../store/store";
10 import { getDataExplorer, DataExplorerState } from "../../store/data-explorer/data-explorer-reducer";
11 import { resourceToDataItem, ProjectPanelItem } from "./project-panel-item";
12 import FilterBuilder from "../../common/api/filter-builder";
13 import { DataColumns } from "../../components/data-table/data-table";
14 import { ProcessResource } from "../../models/process";
15
16 export const projectPanelMiddleware: Middleware = store => next => {
17     next(actions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
18
19     return action => {
20
21         const handleProjectPanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
22             (data: T) => {
23                 next(action);
24                 if (data.id === PROJECT_PANEL_ID) {
25                     handler(data);
26                 }
27             };
28
29         actions.match(action, {
30             SET_PAGE: handleProjectPanelAction(() => {
31                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
32             }),
33             SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
34                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
35             }),
36             SET_FILTERS: handleProjectPanelAction(() => {
37                 store.dispatch(actions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
38                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
39             }),
40             REQUEST_ITEMS: handleProjectPanelAction(() => {
41                 const state = store.getState() as RootState;
42                 const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
43                 const columns = dataExplorer.columns as DataColumns<ProjectPanelItem, ProjectPanelFilter>;
44                 const typeFilters = getColumnFilters(columns, "Type");
45                 const statusFilters = getColumnFilters(columns, "Status");
46                 if (typeFilters.length > 0) {
47                     groupsService
48                         .contents(state.projects.currentItemId, {
49                             limit: dataExplorer.rowsPerPage,
50                             offset: dataExplorer.page * dataExplorer.rowsPerPage,
51                             filters: FilterBuilder
52                                 .create()
53                                 .addIsA("uuid", typeFilters.map(f => f.type))
54                                 .concat(FilterBuilder
55                                     .create<ProcessResource>("containerRequests")
56                                     .addIn("state", statusFilters.map(f => f.type))
57                                 )
58                         })
59                         .then(response => {
60                             store.dispatch(actions.SET_ITEMS({
61                                 id: PROJECT_PANEL_ID,
62                                 items: response.items.map(resourceToDataItem),
63                                 itemsAvailable: response.itemsAvailable,
64                                 page: Math.floor(response.offset / response.limit),
65                                 rowsPerPage: response.limit
66                             }));
67                         });
68                 } else {
69                     store.dispatch(actions.SET_ITEMS({
70                         id: PROJECT_PANEL_ID,
71                         items: [],
72                         itemsAvailable: 0,
73                         page: 0,
74                         rowsPerPage: dataExplorer.rowsPerPage
75                     }));
76                 }
77             }),
78             default: () => next(action)
79         });
80     };
81 };
82
83 const getColumnFilters = (columns: DataColumns<ProjectPanelItem, ProjectPanelFilter>, columnName: string) => {
84     const column = columns.find(c => c.name === columnName);
85     return column && column.filters ? column.filters.filter(f => f.selected) : [];
86 };
87