1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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';
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 }>()
28 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
30 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
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));
43 export const loadCollectionTags = (uuid: string) =>
44 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
45 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
46 return services.tagService
49 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
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
61 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
62 dispatch(snackbarActions.OPEN_SNACKBAR({
63 message: "Tag has been successfully added.",
69 export const deleteCollectionTag = (uuid: string) =>
70 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
71 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
72 return services.linkService
75 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
76 dispatch(snackbarActions.OPEN_SNACKBAR({
77 message: "Tag has been successfully deleted.",