Name hardcoded values
[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 { unionize, ofType, UnionOf } from "unionize";
6 import { Dispatch } from "redux";
7 import { ResourceKind } from "../../models/resource";
8 import { CollectionResource } from "../../models/collection";
9 import { collectionPanelFilesAction, loadCollectionFiles } from "./collection-panel-files/collection-panel-files-actions";
10 import { createTree } from "../../models/tree";
11 import { RootState } from "../store";
12 import { ServiceRepository } from "../../services/services";
13 import { TagResource, TagProperty } from "../../models/tag";
14 import { snackbarActions } from "../snackbar/snackbar-actions";
15
16 export const collectionPanelActions = unionize({
17     LOAD_COLLECTION: ofType<{ uuid: string }>(),
18     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
19     LOAD_COLLECTION_TAGS: ofType<{ uuid: string }>(),
20     LOAD_COLLECTION_TAGS_SUCCESS: ofType<{ tags: TagResource[] }>(),
21     CREATE_COLLECTION_TAG: ofType<{ data: any }>(),
22     CREATE_COLLECTION_TAG_SUCCESS: ofType<{ tag: TagResource }>(),
23     DELETE_COLLECTION_TAG: ofType<{ uuid: string }>(),
24     DELETE_COLLECTION_TAG_SUCCESS: ofType<{ uuid: string }>()
25 }, { tag: 'type', value: 'payload' });
26
27 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
28
29 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
30
31 export const loadCollection = (uuid: string) =>
32     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
34         dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
35         return services.collectionService
36             .get(uuid)
37             .then(item => {
38                 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item }));
39                 dispatch<any>(loadCollectionFiles(uuid));
40             });
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
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     };