Merge branch 'origin/master' into 13666-data-explorer-mapper
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import projectActions, { getProjectList } from "../project/project-action";
7 import { push } from "react-router-redux";
8 import { TreeItem, TreeItemStatus } from "../../components/tree/tree";
9 import { getCollectionList } from "../collection/collection-action";
10 import { findTreeItem } from "../project/project-reducer";
11 import { Project } from "../../models/project";
12 import { Resource, ResourceKind } from "../../models/resource";
13 import sidePanelActions from "../side-panel/side-panel-action";
14
15 export const getResourceUrl = (resource: Resource): string => {
16     switch (resource.kind) {
17         case ResourceKind.LEVEL_UP: return `/projects/${resource.ownerUuid}`;
18         case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
19         case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
20         default:
21             return "#";
22     }
23 };
24
25 export const setProjectItem = (projects: Array<TreeItem<Project>>, itemId: string, itemKind: ResourceKind) => (dispatch: Dispatch) => {
26
27     const openProjectItem = (resource: Resource) => {
28         dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(resource.uuid));
29         dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(resource.uuid));
30         dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(resource.uuid));
31         dispatch(push(getResourceUrl({...resource, kind: itemKind})));
32     };
33     const treeItem = findTreeItem(projects, itemId);
34
35     if (treeItem) {
36         if (treeItem.status === TreeItemStatus.Loaded) {
37             openProjectItem(treeItem.data);
38         } else {
39             dispatch<any>(getProjectList(itemId))
40                 .then(() => openProjectItem(treeItem.data));
41         }
42         dispatch<any>(getCollectionList(itemId));
43
44         // if (item.type === ResourceKind.PROJECT || item.type === ResourceKind.LEVEL_UP) {
45         //     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(item.uuid));
46         // }
47         // this.props.dispatch<any>(getCollectionList(item.uuid));
48
49     }
50 };
51
52     // toggleProjectTreeItemActive = (itemId: string, status: TreeItemStatus) => {
53     //     if (status === TreeItemStatus.Loaded) {
54     //         this.openProjectItem(itemId);
55     //         this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
56     //         this.props.dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(itemId));
57     //     } else {
58     //         this.props.dispatch<any>(getProjectList(itemId))
59     //             .then(() => {
60     //                 this.openProjectItem(itemId);
61     //                 this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
62     //                 this.props.dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(itemId));
63     //             });
64     //     }
65     // }