Merge branch '13819-fix-dynamic-url-configuration'
[arvados.git] / src / store / favorite-panel / favorite-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 { favoriteService } from "../../services/services";
8 import { RootState } from "../store";
9 import { getDataExplorer } from "../data-explorer/data-explorer-reducer";
10 import { FilterBuilder } from "../../common/api/filter-builder";
11 import { DataColumns } from "../../components/data-table/data-table";
12 import {
13     columns,
14     FAVORITE_PANEL_ID,
15     FavoritePanelColumnNames,
16     FavoritePanelFilter
17 } from "../../views/favorite-panel/favorite-panel";
18 import { FavoritePanelItem, resourceToDataItem } from "../../views/favorite-panel/favorite-panel-item";
19 import { LinkResource } from "../../models/link";
20 import { checkPresenceInFavorites } from "../favorites/favorites-actions";
21 import { OrderBuilder } from "../../common/api/order-builder";
22 import { SortDirection } from "../../components/data-table/data-column";
23 import { GroupContentsResource, GroupContentsResourcePrefix } from "../../services/groups-service/groups-service";
24 import { FavoriteOrderBuilder } from "../../services/favorite-service/favorite-order-builder";
25
26 export const favoritePanelMiddleware: Middleware = store => next => {
27     next(dataExplorerActions.SET_COLUMNS({ id: FAVORITE_PANEL_ID, columns }));
28
29     return action => {
30
31         const handlePanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
32             (data: T) => {
33                 next(action);
34                 if (data.id === FAVORITE_PANEL_ID) {
35                     handler(data);
36                 }
37             };
38
39         dataExplorerActions.match(action, {
40             SET_PAGE: handlePanelAction(() => {
41                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
42             }),
43             SET_ROWS_PER_PAGE: handlePanelAction(() => {
44                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
45             }),
46             SET_FILTERS: handlePanelAction(() => {
47                 store.dispatch(dataExplorerActions.RESET_PAGINATION({ id: FAVORITE_PANEL_ID }));
48                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
49             }),
50             TOGGLE_SORT: handlePanelAction(() => {
51                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
52             }),
53             SET_SEARCH_VALUE: handlePanelAction(() => {
54                 store.dispatch(dataExplorerActions.RESET_PAGINATION({ id: FAVORITE_PANEL_ID }));
55                 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
56             }),
57             REQUEST_ITEMS: handlePanelAction(() => {
58                 const state = store.getState() as RootState;
59                 const dataExplorer = getDataExplorer(state.dataExplorer, FAVORITE_PANEL_ID);
60                 const columns = dataExplorer.columns as DataColumns<FavoritePanelItem, FavoritePanelFilter>;
61                 const sortColumn = dataExplorer.columns.find(({ sortDirection }) => Boolean(sortDirection && sortDirection !== "none"));
62                 const typeFilters = getColumnFilters(columns, FavoritePanelColumnNames.TYPE);
63                 const order = FavoriteOrderBuilder.create();
64                 if (typeFilters.length > 0) {
65                     favoriteService
66                         .list(state.projects.currentItemId, {
67                             limit: dataExplorer.rowsPerPage,
68                             offset: dataExplorer.page * dataExplorer.rowsPerPage,
69                             order: sortColumn!.name === FavoritePanelColumnNames.NAME
70                                 ? sortColumn!.sortDirection === SortDirection.Asc
71                                     ? order.addDesc("name")
72                                     : order.addAsc("name")
73                                 : order,
74                             filters: FilterBuilder
75                                 .create<LinkResource>()
76                                 .addIsA("headUuid", typeFilters.map(filter => filter.type))
77                                 .addILike("name", dataExplorer.searchValue)
78                         })
79                         .then(response => {
80                             store.dispatch(dataExplorerActions.SET_ITEMS({
81                                 id: FAVORITE_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: FAVORITE_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 getOrder = (direction: SortDirection) => {
105     const order = OrderBuilder.create<LinkResource>();
106     const addRule = (builder: OrderBuilder<GroupContentsResource | LinkResource>, direction: SortDirection) =>
107         direction === SortDirection.Asc
108             ? builder.addAsc("name")
109             : builder.addDesc("name");
110
111     return [
112         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
113         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
114         OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)
115     ].reduce((acc, b) =>
116         acc.concat(addRule(b, direction)), addRule(OrderBuilder.create(), direction));
117 };
118
119 const getColumnFilters = (columns: DataColumns<FavoritePanelItem, FavoritePanelFilter>, columnName: string) => {
120     const column = columns.find(c => c.name === columnName);
121     return column && column.filters ? column.filters.filter(f => f.selected) : [];
122 };
123
124
125