Handle filtering in projectPanelMiddleware
[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
15 export const projectPanelMiddleware: Middleware = store => next => {
16     next(actions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
17
18     return action => {
19
20         const handleProjectPanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
21             (data: T) => {
22                 next(action);
23                 if (data.id === PROJECT_PANEL_ID) {
24                     handler(data);
25                 }
26             };
27
28         actions.match(action, {
29             SET_PAGE: handleProjectPanelAction(() => {
30                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
31             }),
32             SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
33                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
34             }),
35             SET_FILTERS: handleProjectPanelAction(() => {
36                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
37             }),
38             REQUEST_ITEMS: handleProjectPanelAction(() => {
39                 const state = store.getState() as RootState;
40                 const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
41                 const typeColumn = columns.find(c => c.name === "Type");
42                 const typeFilters = getSelectedTypeFilters(dataExplorer.columns as DataColumns<ProjectPanelItem, ProjectPanelFilter>);
43                 if (typeFilters.length > 0) {
44                     groupsService
45                         .contents(state.projects.currentItemId, {
46                             limit: dataExplorer.rowsPerPage,
47                             offset: dataExplorer.page * dataExplorer.rowsPerPage,
48                             filters: FilterBuilder.create().addIsA("uuid", typeFilters.map(f => f.type))
49                         })
50                         .then(response => {
51                             store.dispatch(actions.SET_ITEMS({
52                                 id: PROJECT_PANEL_ID,
53                                 items: response.items.map(resourceToDataItem),
54                                 itemsAvailable: response.itemsAvailable,
55                                 page: Math.floor(response.offset / response.limit),
56                                 rowsPerPage: response.limit
57                             }));
58                         });
59                 } else {
60                     store.dispatch(actions.SET_ITEMS({
61                         id: PROJECT_PANEL_ID,
62                         items: [],
63                         itemsAvailable: 0,
64                         page: 0,
65                         rowsPerPage: dataExplorer.rowsPerPage
66                     }));
67                 }
68             }),
69             default: () => next(action)
70         });
71     };
72 };
73
74 const getSelectedTypeFilters = (columns: DataColumns<ProjectPanelItem, ProjectPanelFilter>) => {
75     const typeColumn = columns.find(c => c.name === "Type");
76     return typeColumn && typeColumn.filters ? typeColumn.filters.filter(f => f.selected) : [];
77 };
78