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