refs #master Merge branch 'origin/master' into 13828-trash-view
[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 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         dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
38         dispatch(resourcesActions.SET_RESOURCES([collection]));
39         dispatch<any>(loadCollectionFiles(collection.uuid));
40         dispatch<any>(loadCollectionTags(collection.uuid));
41         return collection;
42     };
43
44 export const loadCollectionTags = (uuid: string) =>
45     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
46         dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
47         return services.tagService
48             .list(uuid)
49             .then(tags => {
50                 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
51             });
52     };
53
54 export const createCollectionTag = (data: TagProperty) =>
55     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56         dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data }));
57         const item = getState().collectionPanel.item;
58         const uuid = item ? item.uuid : '';
59         return services.tagService
60             .create(uuid, data)
61             .then(tag => {
62                 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
63                 dispatch(snackbarActions.OPEN_SNACKBAR({
64                     message: "Tag has been successfully added.",
65                     hideDuration: 2000
66                 }));
67             });
68     };
69
70 export const deleteCollectionTag = (uuid: string) =>
71     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
72         dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
73         return services.linkService
74             .delete(uuid)
75             .then(tag => {
76                 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
77                 dispatch(snackbarActions.OPEN_SNACKBAR({
78                     message: "Tag has been successfully deleted.",
79                     hideDuration: 2000
80                 }));
81             });
82     };