15610: Avoids loading the file list on big collections, offers manual loading.
[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 { collectionPanelFilesAction } from "./collection-panel-files/collection-panel-files-actions";
9 import { createTree } from "~/models/tree";
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: ofType<{ uuid: string }>(),
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         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
35         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
36         const collection = await services.collectionService.get(uuid);
37         dispatch(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     async (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         try {
54             const key = data.keyID || data.key;
55             const value = data.valueID || data.value;
56             const updatedCollection = await services.collectionService.update(
57                 item.uuid, {
58                     properties: addProperty(properties, key, value)
59                 }
60             );
61             dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
62             dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
63             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
64             return updatedCollection;
65         } catch (e) {
66             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
67             return;
68         }
69     };
70
71 export const navigateToProcess = (uuid: string) =>
72     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
73         try {
74             await services.containerRequestService.get(uuid);
75             dispatch<any>(navigateTo(uuid));
76         } catch {
77             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exist!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
78         }
79     };
80
81 export const deleteCollectionTag = (key: string, value: string) =>
82     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
83         const item = getState().collectionPanel.item;
84         if (!item) { return; }
85
86         const properties = Object.assign({}, item.properties);
87         try {
88             const updatedCollection = await services.collectionService.update(
89                 item.uuid, {
90                     properties: deleteProperty(properties, key, value)
91                 }
92             );
93             dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
94             dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
95             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
96             return updatedCollection;
97         } catch (e) {
98             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
99             return;
100         }
101     };