Merge branch 'master' into 13764-icons-colors-unification-refactoring
[arvados-workbench2.git] / src / store / 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 "../data-explorer/data-explorer-action";
7 import { PROJECT_PANEL_ID, columns, ProjectPanelFilter, ProjectPanelColumnNames } from "../../views/project-panel/project-panel";
8 import { groupsService } from "../../services/services";
9 import { RootState } from "../store";
10 import { getDataExplorer, DataExplorerState } from "../data-explorer/data-explorer-reducer";
11 import { resourceToDataItem, ProjectPanelItem } from "../../views/project-panel/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 import { CollectionResource } from "../../models/collection";
16 import OrderBuilder from "../../common/api/order-builder";
17 import { GroupContentsResource, GroupContentsResourcePrefix } from "../../services/groups-service/groups-service";
18 import { SortDirection } from "../../components/data-table/data-column";
19
20 export const projectPanelMiddleware: Middleware = store => next => {
21     next(actions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
22
23     return action => {
24
25         const handleProjectPanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
26             (data: T) => {
27                 next(action);
28                 if (data.id === PROJECT_PANEL_ID) {
29                     handler(data);
30                 }
31             };
32
33         actions.match(action, {
34             SET_PAGE: handleProjectPanelAction(() => {
35                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
36             }),
37             SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
38                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
39             }),
40             SET_FILTERS: handleProjectPanelAction(() => {
41                 store.dispatch(actions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
42                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
43             }),
44             TOGGLE_SORT: handleProjectPanelAction(() => {
45                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
46             }),
47             SET_SEARCH_VALUE: handleProjectPanelAction(() => {
48                 store.dispatch(actions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
49                 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
50             }),
51             REQUEST_ITEMS: handleProjectPanelAction(() => {
52                 const state = store.getState() as RootState;
53                 const dataExplorer = getDataExplorer(state.dataExplorer, PROJECT_PANEL_ID);
54                 const columns = dataExplorer.columns as DataColumns<ProjectPanelItem, ProjectPanelFilter>;
55                 const typeFilters = getColumnFilters(columns, ProjectPanelColumnNames.Type);
56                 const statusFilters = getColumnFilters(columns, ProjectPanelColumnNames.Status);
57                 const sortColumn = dataExplorer.columns.find(({ sortDirection }) => Boolean(sortDirection && sortDirection !== "none"));
58                 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.Asc ? SortDirection.Asc : SortDirection.Desc;
59                 if (typeFilters.length > 0) {
60                     groupsService
61                         .contents(state.projects.currentItemId, {
62                             limit: dataExplorer.rowsPerPage,
63                             offset: dataExplorer.page * dataExplorer.rowsPerPage,
64                             order: sortColumn
65                                 ? sortColumn.name === ProjectPanelColumnNames.Name
66                                     ? getOrder("name", sortDirection)
67                                     : getOrder("createdAt", sortDirection)
68                                 : OrderBuilder.create(),
69                             filters: FilterBuilder
70                                 .create()
71                                 .concat(FilterBuilder
72                                     .create()
73                                     .addIsA("uuid", typeFilters.map(f => f.type)))
74                                 .concat(FilterBuilder
75                                     .create<ProcessResource>(GroupContentsResourcePrefix.Process)
76                                     .addIn("state", statusFilters.map(f => f.type)))
77                                 .concat(getSearchFilter(dataExplorer.searchValue))
78                         })
79                         .then(response => {
80                             store.dispatch(actions.SET_ITEMS({
81                                 id: PROJECT_PANEL_ID,
82                                 items: response.items.map(resourceToDataItem),
83                                 itemsAvailable: response.itemsAvailable,
84                                 page: Math.floor(response.offset / response.limit),
85                                 rowsPerPage: response.limit
86                             }));
87                         });
88                 } else {
89                     store.dispatch(actions.SET_ITEMS({
90                         id: PROJECT_PANEL_ID,
91                         items: [],
92                         itemsAvailable: 0,
93                         page: 0,
94                         rowsPerPage: dataExplorer.rowsPerPage
95                     }));
96                 }
97             }),
98             default: () => next(action)
99         });
100     };
101 };
102
103 const getColumnFilters = (columns: DataColumns<ProjectPanelItem, ProjectPanelFilter>, columnName: string) => {
104     const column = columns.find(c => c.name === columnName);
105     return column && column.filters ? column.filters.filter(f => f.selected) : [];
106 };
107
108 const getOrder = (attribute: "name" | "createdAt", direction: SortDirection) =>
109     [
110         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
111         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
112         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)
113     ].reduce((acc, b) =>
114         acc.concat(direction === SortDirection.Asc
115             ? b.addAsc(attribute)
116             : b.addDesc(attribute)), OrderBuilder.create());
117
118 const getSearchFilter = (searchValue: string) =>
119     searchValue
120         ? [
121             FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
122             FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
123             FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)]
124             .reduce((acc, b) =>
125                 acc.concat(b.addILike("name", searchValue)), FilterBuilder.create())
126         : FilterBuilder.create();
127
128