Add pagination reseting to set filters action handler
[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.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
37                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
38             }),
39             REQUEST_ITEMS: handleProjectPanelAction(() => {
40                 const state = store.getState() as RootState;
41                 const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
42                 const typeColumn = columns.find(c => c.name === "Type");
43                 const typeFilters = getSelectedTypeFilters(dataExplorer.columns as DataColumns<ProjectPanelItem, ProjectPanelFilter>);
44                 if (typeFilters.length > 0) {
45                     groupsService
46                         .contents(state.projects.currentItemId, {
47                             limit: dataExplorer.rowsPerPage,
48                             offset: dataExplorer.page * dataExplorer.rowsPerPage,
49                             filters: FilterBuilder.create().addIsA("uuid", typeFilters.map(f => f.type))
50                         })
51                         .then(response => {
52                             store.dispatch(actions.SET_ITEMS({
53                                 id: PROJECT_PANEL_ID,
54                                 items: response.items.map(resourceToDataItem),
55                                 itemsAvailable: response.itemsAvailable,
56                                 page: Math.floor(response.offset / response.limit),
57                                 rowsPerPage: response.limit
58                             }));
59                         });
60                 } else {
61                     store.dispatch(actions.SET_ITEMS({
62                         id: PROJECT_PANEL_ID,
63                         items: [],
64                         itemsAvailable: 0,
65                         page: 0,
66                         rowsPerPage: dataExplorer.rowsPerPage
67                     }));
68                 }
69             }),
70             default: () => next(action)
71         });
72     };
73 };
74
75 const getSelectedTypeFilters = (columns: DataColumns<ProjectPanelItem, ProjectPanelFilter>) => {
76     const typeColumn = columns.find(c => c.name === "Type");
77     return typeColumn && typeColumn.filters ? typeColumn.filters.filter(f => f.selected) : [];
78 };
79