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 '../data-explorer/data-explorer-middleware-service';
11 import { ProjectPanelColumnNames, ProjectPanelFilter } from "~/views/project-panel/project-panel";
12 import { RootState } from "../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 } from "~/services/api/filter-builder";
18 import { GroupContentsResource, GroupContentsResourcePrefix } from "~/services/groups-service/groups-service";
19 import { updateFavorites } from "../favorites/favorites-actions";
20 import { PROJECT_PANEL_CURRENT_UUID, projectPanelActions } from './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 '../snackbar/snackbar-actions';
26 import { progressIndicatorActions } from '~/store/progress-indicator/progress-indicator-actions.ts';
27 import { DataExplorer, getDataExplorer } from '../data-explorer/data-explorer-reducer';
28 import { ListResults } from '~/services/common-service/common-resource-service';
29 import { loadContainers } from '../processes/processes-actions';
30 import { ResourceKind } from '~/models/resource';
31 import { getResource } from "~/store/resources/resources";
32 import { CollectionResource } from "~/models/collection";
33 import { resourcesDataActions } from "~/store/resources-data/resources-data-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);
45 api.dispatch(projectPanelCurrentUuidIsNotSet());
46 } else if (!dataExplorer) {
47 api.dispatch(projectPanelDataExplorerIsNotSet());
50 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
51 const response = await this.services.groupsService.contents(projectUuid, getParams(dataExplorer));
52 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
53 const resourceUuids = response.items.map(item => item.uuid);
54 api.dispatch<any>(updateFavorites(resourceUuids));
55 api.dispatch(updateResources(response.items));
56 api.dispatch<any>(updateResourceData(resourceUuids));
57 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
58 api.dispatch(setItems(response));
60 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
61 api.dispatch(projectPanelActions.SET_ITEMS({
65 rowsPerPage: dataExplorer.rowsPerPage
67 api.dispatch(couldNotFetchProjectContents());
73 export const loadMissingProcessesInformation = (resources: GroupContentsResource[]) =>
74 async (dispatch: Dispatch) => {
75 const containerUuids = resources.reduce((uuids, resource) => {
76 return resource.kind === ResourceKind.CONTAINER_REQUEST
77 ? resource.containerUuid
78 ? [...uuids, resource.containerUuid]
82 if (containerUuids.length > 0) {
83 await dispatch<any>(loadContainers(
84 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
89 export const updateResourceData = (resourceUuids: string[]) =>
90 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
91 resourceUuids.map(async uuid => {
92 const resource = getResource<CollectionResource>(uuid)(getState().resources);
93 if (resource && resource.kind === ResourceKind.COLLECTION) {
94 const files = await services.collectionService.files(uuid);
96 dispatch(resourcesDataActions.SET_FILES({ uuid, files }));
102 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
103 projectPanelActions.SET_ITEMS({
104 ...listResultsToDataExplorerItemsMeta(listResults),
105 items: listResults.items.map(resource => resource.uuid),
108 export const getParams = (dataExplorer: DataExplorer) => ({
109 ...dataExplorerToListParams(dataExplorer),
110 order: getOrder(dataExplorer),
111 filters: getFilters(dataExplorer),
114 export const getFilters = (dataExplorer: DataExplorer) => {
115 const columns = dataExplorer.columns as DataColumns<string, ProjectPanelFilter>;
116 const typeFilters = getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE);
117 const statusFilters = getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.STATUS);
118 return new FilterBuilder()
119 .addIsA("uuid", typeFilters.map(f => f.type))
120 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
121 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
122 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
126 export const getOrder = (dataExplorer: DataExplorer) => {
127 const sortColumn = dataExplorer.columns.find(c => c.sortDirection !== SortDirection.NONE);
128 const order = new OrderBuilder<ProjectResource>();
130 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
132 : OrderDirection.DESC;
134 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
136 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
137 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
138 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
141 return order.getOrder();
145 const projectPanelCurrentUuidIsNotSet = () =>
146 snackbarActions.OPEN_SNACKBAR({
147 message: 'Project panel is not opened.'
150 const couldNotFetchProjectContents = () =>
151 snackbarActions.OPEN_SNACKBAR({
152 message: 'Could not fetch project contents.',
153 kind: SnackbarKind.ERROR
156 const projectPanelDataExplorerIsNotSet = () =>
157 snackbarActions.OPEN_SNACKBAR({
158 message: 'Project panel is not ready.'