1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
15 FavoritePanelColumnNames,
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";
26 export const favoritePanelMiddleware: Middleware = store => next => {
27 next(dataExplorerActions.SET_COLUMNS({ id: FAVORITE_PANEL_ID, columns }));
31 const handlePanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
34 if (data.id === FAVORITE_PANEL_ID) {
39 dataExplorerActions.match(action, {
40 SET_PAGE: handlePanelAction(() => {
41 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
43 SET_ROWS_PER_PAGE: handlePanelAction(() => {
44 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
46 SET_FILTERS: handlePanelAction(() => {
47 store.dispatch(dataExplorerActions.RESET_PAGINATION({ id: FAVORITE_PANEL_ID }));
48 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
50 TOGGLE_SORT: handlePanelAction(() => {
51 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: FAVORITE_PANEL_ID }));
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 }));
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) {
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")
74 filters: FilterBuilder
75 .create<LinkResource>()
76 .addIsA("headUuid", typeFilters.map(filter => filter.type))
77 .addILike("name", dataExplorer.searchValue)
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
87 store.dispatch<any>(checkPresenceInFavorites(response.items.map(item => item.uuid)));
90 store.dispatch(dataExplorerActions.SET_ITEMS({
91 id: FAVORITE_PANEL_ID,
95 rowsPerPage: dataExplorer.rowsPerPage
99 default: () => next(action)
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");
112 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
113 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
114 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)
116 acc.concat(addRule(b, direction)), addRule(OrderBuilder.create(), direction));
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) : [];