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