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 { 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 }>()
22 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
24 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
26 export const loadCollectionPanel = (uuid: string) =>
27 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
28 dispatch(collectionPanelActions.LOAD_COLLECTION({ uuid }));
29 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES({ files: createTree() }));
30 const collection = await services.collectionService.get(uuid);
31 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item: collection }));
32 dispatch(resourcesActions.SET_RESOURCES([collection]));
33 dispatch<any>(loadCollectionFiles(collection.uuid));
37 export const createCollectionTag = (data: TagProperty) =>
38 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
39 const item = getState().collectionPanel.item;
40 const uuid = item ? item.uuid : '';
43 item.properties[data.key] = data.value;
44 const updatedCollection = await services.collectionService.update(uuid, item);
45 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
46 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully added.", hideDuration: 2000 }));
47 return updatedCollection;
55 export const deleteCollectionTag = (key: string) =>
56 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
57 const item = getState().collectionPanel.item;
58 const uuid = item ? item.uuid : '';
61 delete item.properties[key];
62 const updatedCollection = await services.collectionService.update(uuid, item);
63 dispatch(resourcesActions.SET_RESOURCES([updatedCollection]));
64 dispatch(snackbarActions.OPEN_SNACKBAR({ message: "Tag has been successfully deleted.", hideDuration: 2000 }));
65 return updatedCollection;