Navigation fixes
[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 enum ItemMode {
26     BOTH,
27     OPEN,
28     ACTIVE
29 }
30
31 export const setProjectItem = (projects: Array<TreeItem<Project>>, itemId: string, itemKind: ResourceKind, itemMode: ItemMode) => (dispatch: Dispatch) => {
32
33     const openProjectItem = (resource: Resource) => {
34         if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
35             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(resource.uuid));
36         }
37
38         if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
39             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(resource.uuid));
40             dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(resource.uuid));
41         }
42
43         dispatch(push(getResourceUrl({...resource, kind: itemKind})));
44     };
45     let treeItem = findTreeItem(projects, itemId);
46     if (treeItem && itemKind === ResourceKind.LEVEL_UP) {
47         treeItem = findTreeItem(projects, treeItem.data.ownerUuid);
48     }
49
50     if (treeItem) {
51         if (treeItem.status === TreeItemStatus.Loaded) {
52             openProjectItem(treeItem.data);
53         } else {
54             dispatch<any>(getProjectList(itemId))
55                 .then(() => openProjectItem(treeItem!.data));
56         }
57         if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
58             dispatch<any>(getCollectionList(itemId));
59         }
60     }
61 };