Merge branch 'master' into 13969-advanced-tab
[arvados.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
17 export const collectionPanelActions = unionize({
18     SET_COLLECTION: ofType<CollectionResource>(),
19     LOAD_COLLECTION: ofType<{ uuid: string }>(),
20     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
21 });
22
23 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
24
25 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
26
27 export const loadCollectionPanel = (uuid: string) =>
28     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
29         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
30         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
31         const collection = await services.collectionService.get(uuid);
32         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
33         dispatch(resourcesActions.SET_RESOURCES([collection]));
34         dispatch<any>(loadCollectionFiles(collection.uuid));
35         return collection;
36     };
37
38 export const createCollectionTag = (data: TagProperty) =>
39     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
40         const item = getState().collectionPanel.item;
41         const uuid = item ? item.uuid : '';
42         try {
43             if (item) {
44                 item.properties[data.key] = data.value;
45                 const updatedCollection = await services.collectionService.update(uuid, item);
46                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
47                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000 }));
48                 return updatedCollection;
49             }
50             return;
51         } catch (e) {
52             return;
53         }
54     };
55
56 export const deleteCollectionTag = (key: string) =>
57     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
58         const item = getState().collectionPanel.item;
59         const uuid = item ? item.uuid : '';
60         try {
61             if (item) {
62                 delete item.properties[key];
63                 const updatedCollection = await services.collectionService.update(uuid, item);
64                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
65                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000 }));
66                 return updatedCollection;
67             }
68             return;
69         } catch (e) {
70             return;
71         }
72     };