21846: applied select to context menu open
[arvados.git] / services / workbench2 / src / store / link-panel / link-panel-middleware-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { ServiceRepository } from 'services/services';
6 import { MiddlewareAPI, Dispatch } from 'redux';
7 import { DataExplorerMiddlewareService, dataExplorerToListParams, getOrder, listResultsToDataExplorerItemsMeta } from 'store/data-explorer/data-explorer-middleware-service';
8 import { RootState } from 'store/store';
9 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
10 import { DataExplorer, getDataExplorer } from 'store/data-explorer/data-explorer-reducer';
11 import { updateResources } from 'store/resources/resources-actions';
12 import { ListResults } from 'services/common-service/common-service';
13 import { LinkResource } from 'models/link';
14 import { linkPanelActions } from 'store/link-panel/link-panel-actions';
15 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
16
17 export class LinkMiddlewareService extends DataExplorerMiddlewareService {
18     constructor(private services: ServiceRepository, id: string) {
19         super(id);
20     }
21
22     async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
23         const state = api.getState();
24         const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
25         try {
26             api.dispatch(progressIndicatorActions.START_WORKING(this.getId()));
27             const response = await this.services.linkService.list(getParams(dataExplorer));
28             api.dispatch(updateResources(response.items));
29             api.dispatch(setItems(response));
30         } catch {
31             api.dispatch(couldNotFetchLinks());
32         } finally {
33             api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
34         }
35     }
36 }
37
38 export const getParams = (dataExplorer: DataExplorer) => ({
39     ...dataExplorerToListParams(dataExplorer),
40     order: getOrder<LinkResource>(dataExplorer)
41 });
42
43 export const setItems = (listResults: ListResults<LinkResource>) =>
44     linkPanelActions.SET_ITEMS({
45         ...listResultsToDataExplorerItemsMeta(listResults),
46         items: listResults.items.map(resource => resource.uuid),
47     });
48
49 const couldNotFetchLinks = () =>
50     snackbarActions.OPEN_SNACKBAR({
51         message: 'Could not fetch links.',
52         kind: SnackbarKind.ERROR
53     });