check-for-collections-with-same-content-adress
[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 { FilterBuilder } from "~/services/api/filter-builder";
19 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
20
21 export const collectionPanelActions = unionize({
22     SET_COLLECTION: ofType<CollectionResource>(),
23     SET_NUMBER_OF_COLLECTIONS_WITH_SAME_PDH: ofType<number>(),
24     LOAD_COLLECTION: ofType<{ uuid: string }>(),
25     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>()
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) =>
33     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
34         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
35         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
36         const collection = await services.collectionService.get(uuid);
37         const collectionsByPDH = await services.collectionService.list({
38             filters: new FilterBuilder()
39                 .addEqual('portableDataHash', collection.portableDataHash)
40                 .getFilters()
41         });
42         dispatch(collectionPanelActions.SET_NUMBER_OF_COLLECTIONS_WITH_SAME_PDH(collectionsByPDH.itemsAvailable));
43         dispatch(loadDetailsPanel(collection.uuid));
44         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
45         dispatch(resourcesActions.SET_RESOURCES([collection]));
46         dispatch<any>(loadCollectionFiles(collection.uuid));
47         return collection;
48     };
49
50 export const createCollectionTag = (data: TagProperty) =>
51     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
52         const item = getState().collectionPanel.item;
53         const uuid = item ? item.uuid : '';
54         try {
55             if (item) {
56                 item.properties[data.key] = data.value;
57                 const version = 'version';
58                 delete item[version];
59                 const updatedCollection = await services.collectionService.update(uuid, item);
60                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
61                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
62                 return updatedCollection;
63             }
64             return;
65         } catch (e) {
66             return;
67         }
68     };
69
70 export const navigateToProcess = (uuid: string) =>
71     async (dispatch: Dispatch<any>, getState: () => RootState, services: ServiceRepository) => {
72         try {
73             await services.containerRequestService.get(uuid);
74             dispatch<any>(navigateTo(uuid));
75         } catch {
76             dispatch(snackbarActions.OPEN_SNACKBAR({ message: 'This process does not exists!', hideDuration: 2000, kind: SnackbarKind.ERROR }));
77         }
78     };
79
80 export const deleteCollectionTag = (key: string) =>
81     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
82         const item = getState().collectionPanel.item;
83         const uuid = item ? item.uuid : '';
84         try {
85             if (item) {
86                 delete item.properties[key];
87                 const updatedCollection = await services.collectionService.update(uuid, item);
88                 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
89                 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000, kind: SnackbarKind.SUCCESS }));
90                 return updatedCollection;
91             }
92             return;
93         } catch (e) {
94             return;
95         }
96     };