1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
6 DataExplorerMiddlewareService,
7 dataExplorerToListParams,
8 getDataExplorerColumnFilters,
9 listResultsToDataExplorerItemsMeta
10 } from '~/store/data-explorer/data-explorer-middleware-service';
11 import { ProjectPanelColumnNames } from "~/views/project-panel/project-panel";
12 import { RootState } from "~/store/store";
13 import { DataColumns } from "~/components/data-table/data-table";
14 import { ServiceRepository } from "~/services/services";
15 import { SortDirection } from "~/components/data-table/data-column";
16 import { OrderBuilder, OrderDirection } from "~/services/api/order-builder";
17 import { FilterBuilder, joinFilters } from "~/services/api/filter-builder";
18 import { GroupContentsResource, GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
19 import { updateFavorites } from "~/store/favorites/favorites-actions";
20 import { PROJECT_PANEL_CURRENT_UUID, IS_PROJECT_PANEL_TRASHED, projectPanelActions } from '~/store/project-panel/project-panel-action';
21 import { Dispatch, MiddlewareAPI } from "redux";
22 import { ProjectResource } from "~/models/project";
23 import { updateResources } from "~/store/resources/resources-actions";
24 import { getProperty } from "~/store/properties/properties";
25 import { snackbarActions, SnackbarKind } from '~/store/snackbar/snackbar-actions';
26 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions.ts';
27 import { DataExplorer, getDataExplorer } from '~/store/data-explorer/data-explorer-reducer';
28 import { ListResults } from '~/services/common-service/common-service';
29 import { loadContainers } from '~/store/processes/processes-actions';
30 import { ResourceKind } from '~/models/resource';
31 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
32 import { serializeResourceTypeFilters } from '~/store/resource-type-filters/resource-type-filters';
33 import { updatePublicFavorites } from '~/store/public-favorites/public-favorites-actions';
35 export class ProjectPanelMiddlewareService extends DataExplorerMiddlewareService {
36 constructor(private services: ServiceRepository, id: string) {
40 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
41 const state = api.getState();
42 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
43 const projectUuid = getProperty<string>(PROJECT_PANEL_CURRENT_UUID)(state.properties);
44 const isProjectTrashed = getProperty<string>(IS_PROJECT_PANEL_TRASHED)(state.properties);
46 api.dispatch(projectPanelCurrentUuidIsNotSet());
47 } else if (!dataExplorer) {
48 api.dispatch(projectPanelDataExplorerIsNotSet());
51 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
52 const response = await this.services.groupsService.contents(projectUuid, getParams(dataExplorer, !!isProjectTrashed));
53 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
54 const resourceUuids = response.items.map(item => item.uuid);
55 api.dispatch<any>(updateFavorites(resourceUuids));
56 api.dispatch<any>(updatePublicFavorites(resourceUuids));
57 api.dispatch(updateResources(response.items));
58 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
59 api.dispatch(setItems(response));
61 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
62 api.dispatch(projectPanelActions.SET_ITEMS({
66 rowsPerPage: dataExplorer.rowsPerPage
68 api.dispatch(couldNotFetchProjectContents());
74 export const loadMissingProcessesInformation = (resources: GroupContentsResource[]) =>
75 async (dispatch: Dispatch) => {
76 const containerUuids = resources.reduce((uuids, resource) => {
77 return resource.kind === ResourceKind.CONTAINER_REQUEST
78 ? resource.containerUuid
79 ? [...uuids, resource.containerUuid]
83 if (containerUuids.length > 0) {
84 await dispatch<any>(loadContainers(
85 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
90 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
91 projectPanelActions.SET_ITEMS({
92 ...listResultsToDataExplorerItemsMeta(listResults),
93 items: listResults.items.map(resource => resource.uuid),
96 export const getParams = (dataExplorer: DataExplorer, isProjectTrashed: boolean) => ({
97 ...dataExplorerToListParams(dataExplorer),
98 order: getOrder(dataExplorer),
99 filters: getFilters(dataExplorer),
100 includeTrash: isProjectTrashed
103 export const getFilters = (dataExplorer: DataExplorer) => {
104 const columns = dataExplorer.columns as DataColumns<string>;
105 const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
107 // TODO: Extract group contents name filter
108 const nameFilters = new FilterBuilder()
109 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
110 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
111 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
118 // TODO: Restore process status filters
119 // const statusFilters = getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.STATUS);
122 export const getOrder = (dataExplorer: DataExplorer) => {
123 const sortColumn = getSortColumn(dataExplorer);
124 const order = new OrderBuilder<ProjectResource>();
126 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
128 : OrderDirection.DESC;
130 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
132 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
133 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
134 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
137 return order.getOrder();
141 const projectPanelCurrentUuidIsNotSet = () =>
142 snackbarActions.OPEN_SNACKBAR({
143 message: 'Project panel is not opened.',
144 kind: SnackbarKind.ERROR
147 const couldNotFetchProjectContents = () =>
148 snackbarActions.OPEN_SNACKBAR({
149 message: 'Could not fetch project contents.',
150 kind: SnackbarKind.ERROR
153 const projectPanelDataExplorerIsNotSet = () =>
154 snackbarActions.OPEN_SNACKBAR({
155 message: 'Project panel is not ready.',
156 kind: SnackbarKind.ERROR