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 { getResource } from "~/store/resources/resources";
32 import { CollectionResource } from "~/models/collection";
33 import { resourcesDataActions } from "~/store/resources-data/resources-data-actions";
34 import { getSortColumn } from "~/store/data-explorer/data-explorer-reducer";
35 import { serializeResourceTypeFilters } from '~/store/resource-type-filters/resource-type-filters';
37 export class ProjectPanelMiddlewareService extends DataExplorerMiddlewareService {
38 constructor(private services: ServiceRepository, id: string) {
42 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
43 const state = api.getState();
44 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
45 const projectUuid = getProperty<string>(PROJECT_PANEL_CURRENT_UUID)(state.properties);
46 const isProjectTrashed = getProperty<string>(IS_PROJECT_PANEL_TRASHED)(state.properties);
48 api.dispatch(projectPanelCurrentUuidIsNotSet());
49 } else if (!dataExplorer) {
50 api.dispatch(projectPanelDataExplorerIsNotSet());
53 api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
54 const response = await this.services.groupsService.contents(projectUuid, getParams(dataExplorer, !!isProjectTrashed));
55 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
56 const resourceUuids = response.items.map(item => item.uuid);
57 api.dispatch<any>(updateFavorites(resourceUuids));
58 api.dispatch(updateResources(response.items));
59 api.dispatch<any>(updateResourceData(resourceUuids));
60 await api.dispatch<any>(loadMissingProcessesInformation(response.items));
61 api.dispatch(setItems(response));
63 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
64 api.dispatch(projectPanelActions.SET_ITEMS({
68 rowsPerPage: dataExplorer.rowsPerPage
70 api.dispatch(couldNotFetchProjectContents());
76 export const loadMissingProcessesInformation = (resources: GroupContentsResource[]) =>
77 async (dispatch: Dispatch) => {
78 const containerUuids = resources.reduce((uuids, resource) => {
79 return resource.kind === ResourceKind.CONTAINER_REQUEST
80 ? resource.containerUuid
81 ? [...uuids, resource.containerUuid]
85 if (containerUuids.length > 0) {
86 await dispatch<any>(loadContainers(
87 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
92 export const updateResourceData = (resourceUuids: string[]) =>
93 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
94 resourceUuids.map(async uuid => {
95 const resource = getResource<CollectionResource>(uuid)(getState().resources);
96 if (resource && resource.kind === ResourceKind.COLLECTION) {
97 const files = await services.collectionService.files(uuid);
99 dispatch(resourcesDataActions.SET_FILES({ uuid, files }));
105 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
106 projectPanelActions.SET_ITEMS({
107 ...listResultsToDataExplorerItemsMeta(listResults),
108 items: listResults.items.map(resource => resource.uuid),
111 export const getParams = (dataExplorer: DataExplorer, isProjectTrashed: boolean) => ({
112 ...dataExplorerToListParams(dataExplorer),
113 order: getOrder(dataExplorer),
114 filters: getFilters(dataExplorer),
115 includeTrash: isProjectTrashed
118 export const getFilters = (dataExplorer: DataExplorer) => {
119 const columns = dataExplorer.columns as DataColumns<string>;
120 const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
122 // TODO: Extract group contents name filter
123 const nameFilters = new FilterBuilder()
124 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.COLLECTION)
125 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROCESS)
126 .addILike("name", dataExplorer.searchValue, GroupContentsResourcePrefix.PROJECT)
133 // TODO: Restore process status filters
134 // const statusFilters = getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.STATUS);
137 export const getOrder = (dataExplorer: DataExplorer) => {
138 const sortColumn = getSortColumn(dataExplorer);
139 const order = new OrderBuilder<ProjectResource>();
141 const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
143 : OrderDirection.DESC;
145 const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
147 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
148 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
149 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
152 return order.getOrder();
156 const projectPanelCurrentUuidIsNotSet = () =>
157 snackbarActions.OPEN_SNACKBAR({
158 message: 'Project panel is not opened.'
161 const couldNotFetchProjectContents = () =>
162 snackbarActions.OPEN_SNACKBAR({
163 message: 'Could not fetch project contents.',
164 kind: SnackbarKind.ERROR
167 const projectPanelDataExplorerIsNotSet = () =>
168 snackbarActions.OPEN_SNACKBAR({
169 message: 'Project panel is not ready.'