change chip styles
[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 { RootState } from "../store";
10 import { ServiceRepository } from "../../services/services";
11 import { TagResource, TagProperty } from "../../models/tag";
12 import { snackbarActions } from "../snackbar/snackbar-actions";
13
14 export const collectionPanelActions = unionize({
15     LOAD_COLLECTION: ofType<{ uuid: string, kind: ResourceKind }>(),
16     LOAD_COLLECTION_SUCCESS: ofType<{ item: CollectionResource }>(),
17     LOAD_COLLECTION_TAGS: ofType<{ uuid: string }>(),
18     LOAD_COLLECTION_TAGS_SUCCESS: ofType<{ tags: TagResource[] }>(),
19     CREATE_COLLECTION_TAG: ofType<{ data: any }>(),
20     CREATE_COLLECTION_TAG_SUCCESS: ofType<{ tag: TagResource }>(),
21     DELETE_COLLECTION_TAG: ofType<{ uuid: string }>(),
22     DELETE_COLLECTION_TAG_SUCCESS: ofType<{ uuid: string }>()
23 }, { tag: 'type', value: 'payload' });
24
25 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
26
27 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
28
29 export const loadCollection = (uuid: string, kind: ResourceKind) =>
30     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
31         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid, kind }));
32         return services.collectionService
33             .get(uuid)
34             .then(item => {
35                 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: item as CollectionResource }));
36             });
37     };
38
39 export const loadCollectionTags = (uuid: string) => 
40     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
41         dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
42         return services.tagService
43             .list(uuid)
44             .then(tags => {
45                 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
46             });
47     };
48
49
50 export const createCollectionTag = (data: TagProperty) => 
51     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
52         dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data }));
53         const item = getState().collectionPanel.item;
54         const uuid = item ? item.uuid : '';
55         return services.tagService
56             .create(uuid, data)
57             .then(tag => {
58                 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
59                 dispatch(snackbarActions.OPEN_SNACKBAR({
60                     message: "Tag has been successfully added.",
61                     hideDuration: 2000
62                 }));
63             });
64     };
65
66 export const deleteCollectionTag = (uuid: string) => 
67     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
68         dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
69         return services.linkService
70             .delete(uuid)
71             .then(tag => {
72                 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
73                 dispatch(snackbarActions.OPEN_SNACKBAR({
74                     message: "Tag has been successfully deleted.",
75                     hideDuration: 2000
76                 }));
77             });
78     };