Merge remote-tracking branch 'origin/main' into 18169-cancel-button-not-working
[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 import { getResource } from "store/resources/resources";
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, forceReload = false) =>
33     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34         const { collectionPanel: { item } } = getState();
35         const collection = (item && item.uuid === uuid && !forceReload)
36             ? item
37             : await services.collectionService.get(uuid);
38         dispatch<any>(loadDetailsPanel(collection.uuid));
39         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
40         dispatch(resourcesActions.SET_RESOURCES([collection]));
41         if (collection.fileCount <= COLLECTION_PANEL_LOAD_FILES_THRESHOLD &&
42             !getState().collectionPanel.loadBigCollections) {
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         const cachedCollection = getResource<CollectionResource>(item.uuid)(getState().resources);
56         services.collectionService.update(
57             item.uuid, {
58                 properties: addProperty(properties, key, value)
59             }
60         ).then(updatedCollection => {
61             updatedCollection = {...cachedCollection, ...updatedCollection};
62             dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
63             dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
64             dispatch(snackbarActions.OPEN_SNACKBAR({
65                 message: "Property has been successfully added.",
66                 hideDuration: 2000,
67                 kind: SnackbarKind.SUCCESS }));
68             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
69             return updatedCollection;
70         }).catch (e =>
71             dispatch(snackbarActions.OPEN_SNACKBAR({
72                 message: e.errors[0],
73                 hideDuration: 2000,
74                 kind: SnackbarKind.ERROR }))
75         );
76     };
77
78 export const navigateToProcess = (uuid: string) =>
79     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
80         try {
81             await services.containerRequestService.get(uuid);
82             dispatch<any>(navigateTo(uuid));
83         } catch {
84             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exist!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
85         }
86     };
87
88 export const deleteCollectionTag = (key: string, value: string) =>
89     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90         const item = getState().collectionPanel.item;
91         if (!item) { return; }
92
93         const properties = Object.assign({}, item.properties);
94         const cachedCollection = getResource<CollectionResource>(item.uuid)(getState().resources);
95         services.collectionService.update(
96             item.uuid, {
97                 properties: deleteProperty(properties, key, value)
98             }
99         ).then(updatedCollection => {
100             updatedCollection = {...cachedCollection, ...updatedCollection};
101             dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
102             dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
103             dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
104             dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
105             return updatedCollection;
106         }).catch (e => {
107             dispatch(snackbarActions.OPEN_SNACKBAR({
108                 message: e.errors[0],
109                 hideDuration: 2000,
110                 kind: SnackbarKind.ERROR }));
111         });
112     };