Merge branch '15557-rerun-workflow' into main. Closes #15557
[arvados-workbench2.git] / src / store / shared-with-me-panel / shared-with-me-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { DataExplorerMiddlewareService, listResultsToDataExplorerItemsMeta, dataExplorerToListParams } from '../data-explorer/data-explorer-middleware-service';
6 import { ServiceRepository } from 'services/services';
7 import { MiddlewareAPI, Dispatch } from 'redux';
8 import { RootState } from 'store/store';
9 import { getDataExplorer, DataExplorer } from 'store/data-explorer/data-explorer-reducer';
10 import { updateFavorites } from 'store/favorites/favorites-actions';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { loadMissingProcessesInformation, getFilters } from 'store/project-panel/project-panel-middleware-service';
13 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
14 import { sharedWithMePanelActions } from './shared-with-me-panel-actions';
15 import { ListResults } from 'services/common-service/common-service';
16 import { GroupContentsResource, GroupContentsResourcePrefix } from 'services/groups-service/groups-service';
17 import { SortDirection } from 'components/data-table/data-column';
18 import { OrderBuilder, OrderDirection } from 'services/api/order-builder';
19 import { ProjectResource } from 'models/project';
20 import { ProjectPanelColumnNames } from 'views/project-panel/project-panel';
21 import { getSortColumn } from "store/data-explorer/data-explorer-reducer";
22 import { updatePublicFavorites } from 'store/public-favorites/public-favorites-actions';
23 import { FilterBuilder } from 'services/api/filter-builder';
24 import { progressIndicatorActions } from 'store/progress-indicator/progress-indicator-actions';
25
26 export class SharedWithMeMiddlewareService extends DataExplorerMiddlewareService {
27     constructor(private services: ServiceRepository, id: string) {
28         super(id);
29     }
30
31     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
32         const state = api.getState();
33         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
34         try {
35             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
36             const response = await this.services.groupsService
37                 .contents('', {
38                     ...getParams(dataExplorer),
39                     excludeHomeProject: true,
40                     filters: new FilterBuilder().addDistinct('uuid', `${state.auth.config.uuidPrefix}-j7d0g-publicfavorites`).getFilters()
41                 });
42             api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
43             api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
44             api.dispatch(updateResources(response.items));
45             await api.dispatch<any>(loadMissingProcessesInformation(response.items));
46             api.dispatch(setItems(response));
47         } catch (e) {
48             api.dispatch(couldNotFetchSharedItems());
49         } finally {
50             api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
51         }
52     }
53 }
54
55 export const getParams = (dataExplorer: DataExplorer) => ({
56     ...dataExplorerToListParams(dataExplorer),
57     order: getOrder(dataExplorer),
58     filters: getFilters(dataExplorer),
59 });
60
61 export const getOrder = (dataExplorer: DataExplorer) => {
62     const sortColumn = getSortColumn(dataExplorer);
63     const order = new OrderBuilder<ProjectResource>();
64     if (sortColumn) {
65         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
66             ? OrderDirection.ASC
67             : OrderDirection.DESC;
68         const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
69         if (columnName === 'name') {
70             return order
71                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
72                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
73                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
74                 .getOrder();
75         } else {
76             return order
77                 .addOrder(sortDirection, columnName)
78                 .getOrder();
79         }
80     } else {
81         return order.getOrder();
82     }
83 };
84
85 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
86     sharedWithMePanelActions.SET_ITEMS({
87         ...listResultsToDataExplorerItemsMeta(listResults),
88         items: listResults.items.map(resource => resource.uuid),
89     });
90
91 const couldNotFetchSharedItems = () =>
92     snackbarActions.OPEN_SNACKBAR({
93         message: 'Could not fetch shared items.',
94         kind: SnackbarKind.ERROR
95     });