Merge branch 'master' into 13786-current-token
[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 { dataExplorerActions } 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 } 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 { OrderBuilder } from "../../common/api/order-builder";
16 import { GroupContentsResource, GroupContentsResourcePrefix } from "../../services/groups-service/groups-service";
17 import { SortDirection } from "../../components/data-table/data-column";
18 import { checkPresenceInFavorites } from "../favorites/favorites-actions";
19
20 export const projectPanelMiddleware: Middleware = store => next => {
21     next(dataExplorerActions.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         dataExplorerActions.match(action, {
34             SET_PAGE: handleProjectPanelAction(() => {
35                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
36             }),
37             SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
38                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
39             }),
40             SET_FILTERS: handleProjectPanelAction(() => {
41                 store.dispatch(dataExplorerActions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
42                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
43             }),
44             TOGGLE_SORT: handleProjectPanelAction(() => {
45                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
46             }),
47             SET_SEARCH_VALUE: handleProjectPanelAction(() => {
48                 store.dispatch(dataExplorerActions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
49                 store.dispatch(dataExplorerActions.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(dataExplorerActions.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                             store.dispatch<any>(checkPresenceInFavorites(response.items.map(item => item.uuid)));
88                         });
89                 } else {
90                     store.dispatch(dataExplorerActions.SET_ITEMS({
91                         id: PROJECT_PANEL_ID,
92                         items: [],
93                         itemsAvailable: 0,
94                         page: 0,
95                         rowsPerPage: dataExplorer.rowsPerPage
96                     }));
97                 }
98             }),
99             default: () => next(action)
100         });
101     };
102 };
103
104 const getColumnFilters = (columns: DataColumns<ProjectPanelItem, ProjectPanelFilter>, columnName: string) => {
105     const column = columns.find(c => c.name === columnName);
106     return column && column.filters ? column.filters.filter(f => f.selected) : [];
107 };
108
109 const getOrder = (attribute: "name" | "createdAt", direction: SortDirection) =>
110     [
111         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
112         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
113         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)
114     ].reduce((acc, b) =>
115         acc.concat(direction === SortDirection.Asc
116             ? b.addAsc(attribute)
117             : b.addDesc(attribute)), OrderBuilder.create());
118
119 const getSearchFilter = (searchValue: string) =>
120     searchValue
121         ? [
122             FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
123             FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
124             FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)]
125             .reduce((acc, b) =>
126                 acc.concat(b.addILike("name", searchValue)), FilterBuilder.create())
127         : FilterBuilder.create();
128
129