1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Middleware } from "redux";
6 import actions from "../../store/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/store";
10 import { getDataExplorer, DataExplorerState } from "../../store/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";
20 export const projectPanelMiddleware: Middleware = store => next => {
21 next(actions.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 actions.match(action, {
34 SET_PAGE: handleProjectPanelAction(() => {
35 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
37 SET_ROWS_PER_PAGE: handleProjectPanelAction(() => {
38 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
40 SET_FILTERS: handleProjectPanelAction(() => {
41 store.dispatch(actions.RESET_PAGINATION({ id: PROJECT_PANEL_ID }));
42 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
44 TOGGLE_SORT: handleProjectPanelAction(() => {
45 store.dispatch(actions.REQUEST_ITEMS({ id: PROJECT_PANEL_ID }));
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 }));
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(actions.SET_ITEMS({
82 items: response.items.map(resourceToDataItem),
83 itemsAvailable: response.itemsAvailable,
84 page: Math.floor(response.offset / response.limit),
85 rowsPerPage: response.limit
89 store.dispatch(actions.SET_ITEMS({
94 rowsPerPage: dataExplorer.rowsPerPage
98 default: () => next(action)
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) : [];
108 const getOrder = (attribute: "name" | "createdAt", direction: SortDirection) =>
110 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
111 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
112 OrderBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)
114 acc.concat(direction === SortDirection.Asc
115 ? b.addAsc(attribute)
116 : b.addDesc(attribute)), OrderBuilder.create());
118 const getSearchFilter = (searchValue: string) =>
121 FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Collection),
122 FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Process),
123 FilterBuilder.create<GroupContentsResource>(GroupContentsResourcePrefix.Project)]
125 acc.concat(b.addILike("name", searchValue)), FilterBuilder.create())
126 : FilterBuilder.create();