1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
17 export class LinkMiddlewareService extends DataExplorerMiddlewareService {
18 constructor(private services: ServiceRepository, id: string) {
22 async requestItems(api: MiddlewareAPI<Dispatch, RootState>) {
23 const state = api.getState();
24 const dataExplorer = getDataExplorer(state.dataExplorer, this.getId());
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));
31 api.dispatch(couldNotFetchLinks());
33 api.dispatch(progressIndicatorActions.STOP_WORKING(this.getId()));
38 export const getParams = (dataExplorer: DataExplorer) => ({
39 ...dataExplorerToListParams(dataExplorer),
40 order: getOrder<LinkResource>(dataExplorer)
43 export const setItems = (listResults: ListResults<LinkResource>) =>
44 linkPanelActions.SET_ITEMS({
45 ...listResultsToDataExplorerItemsMeta(listResults),
46 items: listResults.items.map(resource => resource.uuid),
49 const couldNotFetchLinks = () =>
50 snackbarActions.OPEN_SNACKBAR({
51 message: 'Could not fetch links.',
52 kind: SnackbarKind.ERROR