refs #15027-delete-version-attr-on-update-collection
[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 } 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";
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
19 export const collectionPanelActions = unionize({
20     SET_COLLECTION: ofType<CollectionResource>(),
21     LOAD_COLLECTION: ofType<{ uuid: string }>(),
22     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
23 });
24
25 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
26
27 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
28
29 export const loadCollectionPanel = (uuid: string) =>
30     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
32         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
33         const collection = await services.collectionService.get(uuid);
34         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
35         dispatch(resourcesActions.SET_RESOURCES([collection]));
36         dispatch<any>(loadCollectionFiles(collection.uuid));
37         return collection;
38     };
39
40 export const createCollectionTag = (data: TagProperty) =>
41     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
42         const item = getState().collectionPanel.item;
43         const uuid = item ? item.uuid : '';
44         try {
45             if (item) {
46                 item.properties[data.key] = data.value;
47                 const version = 'version';
48                 delete item[version];
49                 const updatedCollection = await services.collectionService.update(uuid, item);
50                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
51                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
52                 return updatedCollection;
53             }
54             return;
55         } catch (e) {
56             return;
57         }
58     };
59
60 export const navigateToProcess = (uuid: string) =>
61     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
62         try {
63             await services.containerRequestService.get(uuid);
64             dispatch<any>(navigateTo(uuid));
65         } catch {
66             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
67         }
68     };
69
70 export const deleteCollectionTag = (key: string) =>
71     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72         const item = getState().collectionPanel.item;
73         const uuid = item ? item.uuid : '';
74         try {
75             if (item) {
76                 delete item.properties[key];
77                 const updatedCollection = await services.collectionService.update(uuid, item);
78                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
79                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
80                 return updatedCollection;
81             }
82             return;
83         } catch (e) {
84             return;
85         }
86     };