Added central action for navigation
[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
14 export const getResourceUrl = (resource: Resource): string => {
15     switch (resource.kind) {
16         case ResourceKind.LEVEL_UP: return `/projects/${resource.ownerUuid}`;
17         case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
18         case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
19         default:
20             return "#";
21     }
22 };
23
24 export const setProjectItem = (projects: Array<TreeItem<Project>>, itemId: string, itemKind: ResourceKind) => (dispatch: Dispatch) => {
25
26     const openProjectItem = (resource: Resource) => {
27         dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(resource.uuid));
28         dispatch(push(getResourceUrl({...resource, kind: itemKind})));
29     };
30     const treeItem = findTreeItem(projects, itemId);
31
32     if (treeItem) {
33         if (treeItem.status === TreeItemStatus.Loaded) {
34             openProjectItem(treeItem.data);
35         } else {
36             dispatch<any>(getProjectList(itemId))
37                 .then(() => openProjectItem(treeItem.data));
38         }
39         dispatch<any>(getCollectionList(itemId));
40
41         // if (item.type === ResourceKind.PROJECT || item.type === ResourceKind.LEVEL_UP) {
42         //     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(item.uuid));
43         // }
44         // this.props.dispatch<any>(getCollectionList(item.uuid));
45
46     }
47 };