Merge branch '17234-wf-runner-ram' refs #17234
[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
25 export class SharedWithMeMiddlewareService extends DataExplorerMiddlewareService {
26     constructor(private services: ServiceRepository, id: string) {
27         super(id);
28     }
29
30     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
31         const state = api.getState();
32         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
33         try {
34             const response = await this.services.groupsService
35                 .contents('', {
36                     ...getParams(dataExplorer),
37                     excludeHomeProject: true,
38                     filters: new FilterBuilder().addDistinct('uuid', `${state.auth.config.uuidPrefix}-j7d0g-publicfavorites`).getFilters()
39                 });
40             api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
41             api.dispatch<any>(updatePublicFavorites(response.items.map(item => item.uuid)));
42             api.dispatch(updateResources(response.items));
43             await api.dispatch<any>(loadMissingProcessesInformation(response.items));
44             api.dispatch(setItems(response));
45         } catch (e) {
46             api.dispatch(couldNotFetchSharedItems());
47         }
48     }
49 }
50
51 export const getParams = (dataExplorer: DataExplorer) => ({
52     ...dataExplorerToListParams(dataExplorer),
53     order: getOrder(dataExplorer),
54     filters: getFilters(dataExplorer),
55 });
56
57 export const getOrder = (dataExplorer: DataExplorer) => {
58     const sortColumn = getSortColumn(dataExplorer);
59     const order = new OrderBuilder<ProjectResource>();
60     if (sortColumn) {
61         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
62             ? OrderDirection.ASC
63             : OrderDirection.DESC;
64         const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
65         if (columnName === 'name') {
66             return order
67                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
68                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
69                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
70                 .getOrder();
71         } else {
72             return order
73                 .addOrder(sortDirection, columnName)
74                 .getOrder();
75         }
76     } else {
77         return order.getOrder();
78     }
79 };
80
81 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
82     sharedWithMePanelActions.SET_ITEMS({
83         ...listResultsToDataExplorerItemsMeta(listResults),
84         items: listResults.items.map(resource => resource.uuid),
85     });
86
87 const couldNotFetchSharedItems = () =>
88     snackbarActions.OPEN_SNACKBAR({
89         message: 'Could not fetch shared items.',
90         kind: SnackbarKind.ERROR
91     });