add property isAdmin to the user interface
[arvados.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 } from '~/store/snackbar/snackbar-actions';
14 import { sharedWithMePanelActions } from './shared-with-me-panel-actions';
15 import { ListResults } from '~/services/common-service/common-resource-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
23 export class SharedWithMeMiddlewareService extends DataExplorerMiddlewareService {
24     constructor(private services: ServiceRepository, id: string) {
25         super(id);
26     }
27
28     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
29         const state = api.getState();
30         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
31         try {
32             const response = await this.services.groupsService
33                 .contents('', {
34                     ...getParams(dataExplorer),
35                     excludeHomeProject: true,
36                 });
37             api.dispatch<any>(updateFavorites(response.items.map(item => item.uuid)));
38             api.dispatch(updateResources(response.items));
39             await api.dispatch<any>(loadMissingProcessesInformation(response.items));
40             api.dispatch(setItems(response));
41         } catch (e) {
42             api.dispatch(couldNotFetchSharedItems());
43         }
44     }
45 }
46
47 export const getParams = (dataExplorer: DataExplorer) => ({
48     ...dataExplorerToListParams(dataExplorer),
49     order: getOrder(dataExplorer),
50     filters: getFilters(dataExplorer),
51 });
52
53 export const getOrder = (dataExplorer: DataExplorer) => {
54     const sortColumn = getSortColumn(dataExplorer);
55     const order = new OrderBuilder<ProjectResource>();
56     if (sortColumn) {
57         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
58             ? OrderDirection.ASC
59             : OrderDirection.DESC;
60         const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
61         if (columnName === 'name') {
62             return order
63                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
64                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
65                 .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
66                 .getOrder();
67         } else {
68             return order
69                 .addOrder(sortDirection, columnName)
70                 .getOrder();
71         }
72     } else {
73         return order.getOrder();
74     }
75 };
76
77 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
78     sharedWithMePanelActions.SET_ITEMS({
79         ...listResultsToDataExplorerItemsMeta(listResults),
80         items: listResults.items.map(resource => resource.uuid),
81     });
82
83 const couldNotFetchSharedItems = () =>
84     snackbarActions.OPEN_SNACKBAR({
85         message: 'Could not fetch shared items.'
86     });