Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / store / collection-panel / collection-panel-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { CollectionResource } from 'models/collection';
7 import { RootState } from "store/store";
8 import { ServiceRepository } from "services/services";
9 import { snackbarActions } from "../snackbar/snackbar-actions";
10 import { resourcesActions } from "store/resources/resources-actions";
11 import { unionize, ofType, UnionOf } from 'common/unionize';
12 import { SnackbarKind } from 'store/snackbar/snackbar-actions';
13 import { navigateTo } from 'store/navigation/navigation-action';
14 import { loadDetailsPanel } from 'store/details-panel/details-panel-action';
15 import { progressIndicatorActions } from "store/progress-indicator/progress-indicator-actions";
16
17 export const collectionPanelActions = unionize({
18     SET_COLLECTION: ofType<CollectionResource>(),
19 });
20
21 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
22
23 export const loadCollectionPanel = (uuid: string, forceReload = false) =>
24     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
25         const { collectionPanel: { item } } = getState();
26         let collection: CollectionResource | null = null;
27         if (!item || item.uuid !== uuid || forceReload) {
28             try {
29                 dispatch(progressIndicatorActions.START_WORKING(uuid + "-panel"));
30                 collection = await services.collectionService.get(uuid);
31                 dispatch(collectionPanelActions.SET_COLLECTION(collection));
32                 dispatch(resourcesActions.SET_RESOURCES([collection]));
33             } finally {
34                 dispatch(progressIndicatorActions.STOP_WORKING(uuid + "-panel"));
35             }
36         } else {
37             collection = item;
38         }
39         dispatch<any>(loadDetailsPanel(collection.uuid));
40         return collection;
41     };
42
43 export const navigateToProcess = (uuid: string) =>
44     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
45         try {
46             await services.containerRequestService.get(uuid);
47             dispatch<any>(navigateTo(uuid));
48         } catch {
49             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exist!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
50         }
51     };