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