refs #13989 Merge branch 'origin/13989-webdav-service'
[arvados.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 } 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, kind: ResourceKind }>(),
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, kind: ResourceKind) =>
32     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
33         dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid, kind }));
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                 return services.collectionFilesService.getFiles(item.uuid);
40             })
41             .then(files => {
42                 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
43             });
44     };
45
46 export const loadCollectionTags = (uuid: string) =>
47     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48         dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
49         return services.tagService
50             .list(uuid)
51             .then(tags => {
52                 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
53             });
54     };
55
56
57 export const createCollectionTag = (data: TagProperty) =>
58     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
59         dispatch(collectionPanelActions.CREATE_COLLECTION_TAG({ data }));
60         const item = getState().collectionPanel.item;
61         const uuid = item ? item.uuid : '';
62         return services.tagService
63             .create(uuid, data)
64             .then(tag => {
65                 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
66                 dispatch(snackbarActions.OPEN_SNACKBAR({
67                     message: "Tag has been successfully added.",
68                     hideDuration: 2000
69                 }));
70             });
71     };
72
73 export const deleteCollectionTag = (uuid: string) =>
74     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75         dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
76         return services.linkService
77             .delete(uuid)
78             .then(tag => {
79                 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
80                 dispatch(snackbarActions.OPEN_SNACKBAR({
81                     message: "Tag has been successfully deleted.",
82                     hideDuration: 2000
83                 }));
84             });
85     };