13494: Makes several operations asynchronous.
[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 {
7     loadCollectionFiles,
8     COLLECTION_PANEL_LOAD_FILES_THRESHOLD
9 } from "./collection-panel-files/collection-panel-files-actions";
10 import { CollectionResource } from '~/models/collection';
11 import { RootState } from "~/store/store";
12 import { ServiceRepository } from "~/services/services";
13 import { TagProperty } from "~/models/tag";
14 import { snackbarActions } from "../snackbar/snackbar-actions";
15 import { resourcesActions } from "~/store/resources/resources-actions";
16 import { unionize, ofType, UnionOf } from '~/common/unionize';
17 import { SnackbarKind } from '~/store/snackbar/snackbar-actions';
18 import { navigateTo } from '~/store/navigation/navigation-action';
19 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
20 import { addProperty, deleteProperty } from "~/lib/resource-properties";
21
22 export const collectionPanelActions = unionize({
23     SET_COLLECTION: ofType<CollectionResource>(),
24     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
25     LOAD_BIG_COLLECTIONS: ofType<boolean>(),
26 });
27
28 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
29
30 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
31
32 export const loadCollectionPanel = (uuid: string) =>
33     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34         const { collectionPanel: { item } } = getState();
35         const collection = item ? item : await services.collectionService.get(uuid);
36         dispatch<any>(loadDetailsPanel(collection.uuid));
37         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
38         dispatch(resourcesActions.SET_RESOURCES([collection]));
39         if (collection.fileCount <= COLLECTION_PANEL_LOAD_FILES_THRESHOLD &&
40             !getState().collectionPanel.loadBigCollections) {
41             dispatch<any>(loadCollectionFiles(collection.uuid));
42         }
43         return collection;
44     };
45
46 export const createCollectionTag = (data: TagProperty) =>
47     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48         const item = getState().collectionPanel.item;
49         if (!item) { return; }
50
51         const properties = Object.assign({}, item.properties);
52         const key = data.keyID || data.key;
53         const value = data.valueID || data.value;
54         services.collectionService.update(
55             item.uuid, {
56                 properties: addProperty(properties, key, value)
57             }
58         ).then(updatedCollection => {
59             dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
60             dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
61             dispatch(snackbarActions.OPEN_SNACKBAR({
62                 message: "Tag has been successfully added.",
63                 hideDuration: 2000,
64                 kind: SnackbarKind.SUCCESS }));
65             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
66             return updatedCollection;
67         }).catch (e =>
68             dispatch(snackbarActions.OPEN_SNACKBAR({
69                 message: e.errors[0],
70                 hideDuration: 2000,
71                 kind: SnackbarKind.ERROR }))
72         );
73     };
74
75 export const navigateToProcess = (uuid: string) =>
76     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
77         try {
78             await services.containerRequestService.get(uuid);
79             dispatch<any>(navigateTo(uuid));
80         } catch {
81             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exist!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
82         }
83     };
84
85 export const deleteCollectionTag = (key: string, value: string) =>
86     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
87         const item = getState().collectionPanel.item;
88         if (!item) { return; }
89
90         const properties = Object.assign({}, item.properties);
91         services.collectionService.update(
92             item.uuid, {
93                 properties: deleteProperty(properties, key, value)
94             }
95         ).then(updatedCollection => {
96             dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
97             dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
98             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
99             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
100             return updatedCollection;
101         }).catch (e => {
102             dispatch(snackbarActions.OPEN_SNACKBAR({
103                 message: e.errors[0],
104                 hideDuration: 2000,
105                 kind: SnackbarKind.ERROR }));
106         });
107     };