add admin links feature - model, service, dialogs and panel
[arvados.git] / src / store / project-panel / project-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import {
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';
36
37 export class ProjectPanelMiddlewareService extends DataExplorerMiddlewareService {
38     constructor(private services: ServiceRepository, id: string) {
39         super(id);
40     }
41
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);
47         if (!projectUuid) {
48             api.dispatch(projectPanelCurrentUuidIsNotSet());
49         } else if (!dataExplorer) {
50             api.dispatch(projectPanelDataExplorerIsNotSet());
51         } else {
52             try {
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));
62             } catch (e) {
63                 api.dispatch(progressIndicatorActions.PERSIST_STOP_WORKING(this.getId()));
64                 api.dispatch(projectPanelActions.SET_ITEMS({
65                     items: [],
66                     itemsAvailable: 0,
67                     page: 0,
68                     rowsPerPage: dataExplorer.rowsPerPage
69                 }));
70                 api.dispatch(couldNotFetchProjectContents());
71             }
72         }
73     }
74 }
75
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]
82                     : uuids
83                 : uuids;
84         }, []);
85         if (containerUuids.length > 0) {
86             await dispatch<any>(loadContainers(
87                 new FilterBuilder().addIn('uuid', containerUuids).getFilters()
88             ));
89         }
90     };
91
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);
98                 if (files) {
99                     dispatch(resourcesDataActions.SET_FILES({ uuid, files }));
100                 }
101             }
102         });
103     };
104
105 export const setItems = (listResults: ListResults<GroupContentsResource>) =>
106     projectPanelActions.SET_ITEMS({
107         ...listResultsToDataExplorerItemsMeta(listResults),
108         items: listResults.items.map(resource => resource.uuid),
109     });
110
111 export const getParams = (dataExplorer: DataExplorer, isProjectTrashed: boolean) => ({
112     ...dataExplorerToListParams(dataExplorer),
113     order: getOrder(dataExplorer),
114     filters: getFilters(dataExplorer),
115     includeTrash: isProjectTrashed
116 });
117
118 export const getFilters = (dataExplorer: DataExplorer) => {
119     const columns = dataExplorer.columns as DataColumns<string>;
120     const typeFilters = serializeResourceTypeFilters(getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.TYPE));
121
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)
127         .getFilters();
128
129     return joinFilters(
130         typeFilters,
131         nameFilters,
132     );
133     // TODO: Restore process status filters
134     // const statusFilters = getDataExplorerColumnFilters(columns, ProjectPanelColumnNames.STATUS);
135 };
136
137 export const getOrder = (dataExplorer: DataExplorer) => {
138     const sortColumn = getSortColumn(dataExplorer);
139     const order = new OrderBuilder<ProjectResource>();
140     if (sortColumn) {
141         const sortDirection = sortColumn && sortColumn.sortDirection === SortDirection.ASC
142             ? OrderDirection.ASC
143             : OrderDirection.DESC;
144
145         const columnName = sortColumn && sortColumn.name === ProjectPanelColumnNames.NAME ? "name" : "createdAt";
146         return order
147             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.COLLECTION)
148             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROCESS)
149             .addOrder(sortDirection, columnName, GroupContentsResourcePrefix.PROJECT)
150             .getOrder();
151     } else {
152         return order.getOrder();
153     }
154 };
155
156 const projectPanelCurrentUuidIsNotSet = () =>
157     snackbarActions.OPEN_SNACKBAR({
158         message: 'Project panel is not opened.'
159     });
160
161 const couldNotFetchProjectContents = () =>
162     snackbarActions.OPEN_SNACKBAR({
163         message: 'Could not fetch project contents.',
164         kind: SnackbarKind.ERROR
165     });
166
167 const projectPanelDataExplorerIsNotSet = () =>
168     snackbarActions.OPEN_SNACKBAR({
169         message: 'Project panel is not ready.'
170     });