f4244389fdf22ab2a2b0f5f7546dda687636bd9c
[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/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
20 export const collectionPanelActions = unionize({
21     SET_COLLECTION: ofType<CollectionResource>(),
22     LOAD_COLLECTION: ofType<{ uuid: string }>(),
23     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
24 });
25
26 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
27
28 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
29
30 export const loadCollectionPanel = (uuid: string) =>
31     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
32         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
33         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
34         const collection = await services.collectionService.get(uuid);
35         dispatch(loadDetailsPanel(collection.uuid));
36         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
37         dispatch(resourcesActions.SET_RESOURCES([collection]));
38         dispatch<any>(loadCollectionFiles(collection.uuid));
39         return collection;
40     };
41
42 export const createCollectionTag = (data: TagProperty) =>
43     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         const item = getState().collectionPanel.item;
45         const uuid = item ? item.uuid : '';
46         try {
47             if (item) {
48                 const key = data.keyID || data.key;
49                 const value = data.valueID || data.value;
50                 if (item.properties[key]) {
51                     if (Array.isArray(item.properties[key])) {
52                         item.properties[key] = [...item.properties[key], value];
53                         // Remove potential duplicates
54                         item.properties[key] = Array.from(new Set(item.properties[key]));
55                     } else {
56                         item.properties[key] = [item.properties[key], value];
57                     }
58                 } else {
59                     item.properties[key] = value;
60                 }
61                 const updatedCollection = await services.collectionService.update(
62                     uuid, {
63                         properties: {...JSON.parse(JSON.stringify(item.properties))}
64                     }
65                 );
66                 item.properties = updatedCollection.properties;
67                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
68                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
69                 return updatedCollection;
70             }
71             return;
72         } catch (e) {
73             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
74             return;
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     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
90         const item = getState().collectionPanel.item;
91         const uuid = item ? item.uuid : '';
92         try {
93             if (item) {
94                 if (Array.isArray(item.properties[key])) {
95                     item.properties[key] = item.properties[key].filter((v: string) => v !== value);
96                     if (item.properties[key].length === 1) {
97                         item.properties[key] = item.properties[key][0];
98                     } else if (item.properties[key].length === 0) {
99                         delete item.properties[key];
100                     }
101                 } else if (item.properties[key] === value) {
102                     delete item.properties[key];
103                 }
104
105                 const updatedCollection = await services.collectionService.update(
106                     uuid, {
107                         properties: {...item.properties}
108                     }
109                 );
110                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
111                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
112                 return updatedCollection;
113             }
114             return;
115         } catch (e) {
116             dispatch(snackbarActions.OPEN_SNACKBAR({ message: e.errors[0], hideDuration: 2000, kind: SnackbarKind.ERROR }));
117             return;
118         }
119     };