d8ad6d0a949575bbcf53392097ace6ea1a983dcf
[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 { TagResource, 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     LOAD_COLLECTION: ofType<{ uuid: string }>(),
19     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
20     LOAD_COLLECTION_TAGS: ofType<{ uuid: string }>(),
21     LOAD_COLLECTION_TAGS_SUCCESS: ofType<{ tags: TagResource[] }>(),
22     CREATE_COLLECTION_TAG: ofType<{ data: any }>(),
23     CREATE_COLLECTION_TAG_SUCCESS: ofType<{ tag: TagResource }>(),
24     DELETE_COLLECTION_TAG: ofType<{ uuid: string }>(),
25     DELETE_COLLECTION_TAG_SUCCESS: ofType<{ uuid: string }>()
26 });
27
28 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
29
30 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
31
32 export const loadCollection = (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         dispatch(resourcesActions.SET_RESOURCES([collection]));
38         dispatch<any>(loadCollectionFiles(collection.uuid));
39         dispatch<any>(loadCollectionTags(collection.uuid));
40     };
41
42 export const loadCollectionTags = (uuid: string) =>
43     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
45         return services.tagService
46             .list(uuid)
47             .then(tags => {
48                 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
49             });
50     };
51
52 export const createCollectionTag = (data: TagProperty) =>
53     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
54         dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data }));
55         const item = getState().collectionPanel.item;
56         const uuid = item ? item.uuid : '';
57         return services.tagService
58             .create(uuid, data)
59             .then(tag => {
60                 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
61                 dispatch(snackbarActions.OPEN_SNACKBAR({
62                     message: "Tag has been successfully added.",
63                     hideDuration: 2000
64                 }));
65             });
66     };
67
68 export const deleteCollectionTag = (uuid: string) =>
69     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70         dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
71         return services.linkService
72             .delete(uuid)
73             .then(tag => {
74                 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
75                 dispatch(snackbarActions.OPEN_SNACKBAR({
76                     message: "Tag has been successfully deleted.",
77                     hideDuration: 2000
78                 }));
79             });
80     };