1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
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";
22 export const collectionPanelActions = unionize({
23 SET_COLLECTION: ofType<CollectionResource>(),
24 LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
25 LOAD_BIG_COLLECTIONS: ofType<boolean>(),
28 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
30 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
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)
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 dispatch<any>(loadCollectionFiles(collection.uuid));
48 export const createCollectionTag = (data: TagProperty) =>
49 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
50 const item = getState().collectionPanel.item;
51 if (!item) { return; }
53 const properties = Object.assign({}, item.properties);
54 const key = data.keyID || data.key;
55 const value = data.valueID || data.value;
56 services.collectionService.update(
58 properties: addProperty(properties, key, value)
60 ).then(updatedCollection => {
61 dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
62 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
63 dispatch(snackbarActions.OPEN_SNACKBAR({
64 message: "Property has been successfully added.",
66 kind: SnackbarKind.SUCCESS }));
67 dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
68 return updatedCollection;
70 dispatch(snackbarActions.OPEN_SNACKBAR({
73 kind: SnackbarKind.ERROR }))
77 export const navigateToProcess = (uuid: string) =>
78 async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
80 await services.containerRequestService.get(uuid);
81 dispatch<any>(navigateTo(uuid));
83 dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exist!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
87 export const deleteCollectionTag = (key: string, value: string) =>
88 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
89 const item = getState().collectionPanel.item;
90 if (!item) { return; }
92 const properties = Object.assign({}, item.properties);
93 services.collectionService.update(
95 properties: deleteProperty(properties, key, value)
97 ).then(updatedCollection => {
98 dispatch(collectionPanelActions.SET_COLLECTION(updatedCollection));
99 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
100 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
101 dispatch<any>(loadDetailsPanel(updatedCollection.uuid));
102 return updatedCollection;
104 dispatch(snackbarActions.OPEN_SNACKBAR({
105 message: e.errors[0],
107 kind: SnackbarKind.ERROR }));