1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch } from "redux";
6 import { projectActions, getProjectList } from "../project/project-action";
7 import { push } from "react-router-redux";
8 import { TreeItemStatus } from "../../components/tree/tree";
9 import { findTreeItem } from "../project/project-reducer";
10 import { RootState } from "../store";
11 import { Resource, ResourceKind } from "../../models/resource";
12 import { projectPanelActions } from "../project-panel/project-panel-action";
13 import { getCollectionUrl } from "../../models/collection";
14 import { getProjectUrl } from "../../models/project";
16 export const getResourceUrl = <T extends Resource>(resource: T): string => {
17 switch (resource.kind) {
18 case ResourceKind.PROJECT: return getProjectUrl(resource.uuid);
19 case ResourceKind.COLLECTION: return getCollectionUrl(resource.uuid);
20 default: return resource.href;
24 export enum ItemMode {
30 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
31 (dispatch: Dispatch, getState: () => RootState) => {
32 const { projects, router } = getState();
33 const treeItem = findTreeItem(projects.items, itemId);
37 const resourceUrl = getResourceUrl(treeItem.data);
39 if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
40 if (router.location && !router.location.pathname.includes(resourceUrl)) {
41 dispatch(push(resourceUrl));
43 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
46 const promise = treeItem.status === TreeItemStatus.LOADED
48 : dispatch<any>(getProjectList(itemId));
51 .then(() => dispatch<any>(() => {
52 if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
53 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
55 dispatch(projectPanelActions.RESET_PAGINATION());
56 dispatch(projectPanelActions.REQUEST_ITEMS());