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 { 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";
20 export const projectPanelMiddleware: Middleware = store => next => {
21 next(dataExplorerActions.SET_COLUMNS({ id: PROJECT_PANEL_ID, columns }));
25 const handleProjectPanelAction = <T extends { id: string }>(handler: (data: T) => void) =>
28 if (data.id === PROJECT_PANEL_ID) {
33 dataExplorerActions.match(action, {
34 SET_PAGE: handleProjectPanelAction(() => {
35 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
37 SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
38 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
40 SET_FILTERS: handleProjectPanelAction(() => {
41 store.dispatch(dataExplorerActions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
42 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
44 TOGGLE_SORT: handleProjectPanelAction(() => {
45 store.dispatch(dataExplorerActions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
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 }));
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) {
61 .contents(state.projects.currentItemId, {
62 limit: dataExplorer.rowsPerPage,
63 offset: dataExplorer.page * dataExplorer.rowsPerPage,
65 ? sortColumn.name === ProjectPanelColumnNames.NAME
66 ? getOrder("name", sortDirection)
67 : getOrder("createdAt", sortDirection)
68 : OrderBuilder.create(),
69 filters: FilterBuilder
73 .addIsA("uuid", typeFilters.map(f => f.type)))
75 .create<ProcessResource>(GroupContentsResourcePrefix.Process)
76 .addIn("state", statusFilters.map(f => f.type)))
77 .concat(getSearchFilter(dataExplorer.searchValue))
80 store.dispatch(dataExplorerActions.SET_ITEMS({
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({
95 rowsPerPage: dataExplorer.rowsPerPage
99 default: () => next(action)
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) : [];
109 const getOrder = (attribute: "name" | "createdAt", direction: SortDirection) =>
111 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
112 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
113 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)
115 acc.concat(direction === SortDirection.Asc
116 ? b.addAsc(attribute)
117 : b.addDesc(attribute)), OrderBuilder.create());
119 const getSearchFilter = (searchValue: string) =>
122 FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
123 FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
124 FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)]
126 acc.concat(b.addILike("name", searchValue)), FilterBuilder.create())
127 : FilterBuilder.create();