1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
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' });
27 export type CollectionPanelAction = UnionOf<typeof collectionPanelActions>;
29 export const COLLECTION_TAG_FORM_NAME = 'collectionTagForm';
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
38 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item }));
39 return services.collectionFilesService.getFiles(item.uuid);
42 dispatch(collectionPanelFilesAction.SET_COLLECTION_FILES(files));
46 export const loadCollectionTags = (uuid: string) =>
47 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
48 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS({ uuid }));
49 return services.tagService
52 dispatch(collectionPanelActions.LOAD_COLLECTION_TAGS_SUCCESS({ tags }));
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
65 dispatch(collectionPanelActions.CREATE_COLLECTION_TAG_SUCCESS({ tag }));
66 dispatch(snackbarActions.OPEN_SNACKBAR({
67 message: "Tag has been successfully added.",
73 export const deleteCollectionTag = (uuid: string) =>
74 (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG({ uuid }));
76 return services.linkService
79 dispatch(collectionPanelActions.DELETE_COLLECTION_TAG_SUCCESS({ uuid: tag.uuid }));
80 dispatch(snackbarActions.OPEN_SNACKBAR({
81 message: "Tag has been successfully deleted.",