b811f9a292b8b42bf9cfb6ba8fcdf93db017351d
[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 { TreeItemStatus } from "../../components/tree/tree";
9 import { getCollectionList } from "../collection/collection-action";
10 import { findTreeItem } from "../project/project-reducer";
11 import { Resource, ResourceKind } from "../../models/resource";
12 import sidePanelActions from "../side-panel/side-panel-action";
13 import dataExplorerActions from "../data-explorer/data-explorer-action";
14 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
15 import { projectPanelItems } from "../../views/project-panel/project-panel-selectors";
16 import { RootState } from "../store";
17
18 export const getResourceUrl = (resource: Resource): string => {
19     switch (resource.kind) {
20         case ResourceKind.LEVEL_UP: return `/projects/${resource.ownerUuid}`;
21         case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
22         case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
23         default:
24             return "#";
25     }
26 };
27
28 export enum ItemMode {
29     BOTH,
30     OPEN,
31     ACTIVE
32 }
33
34 export const setProjectItem = (itemId: string, itemKind = ResourceKind.PROJECT, itemMode = ItemMode.OPEN) =>
35     (dispatch: Dispatch, getState: () => RootState) => {
36         const { projects } = getState();
37
38         let treeItem = findTreeItem(projects.items, itemId);
39         if (treeItem && itemKind === ResourceKind.LEVEL_UP) {
40             treeItem = findTreeItem(projects.items, treeItem.data.ownerUuid);
41         }
42
43         if (treeItem) {
44             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
45
46             if (treeItem.status === TreeItemStatus.Loaded) {
47                 dispatch<any>(openProjectItem(treeItem.data, itemKind, itemMode));
48             } else {
49                 dispatch<any>(getProjectList(itemId))
50                     .then(() => dispatch<any>(openProjectItem(treeItem!.data, itemKind, itemMode)));
51             }
52             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
53                 dispatch<any>(getCollectionList(itemId));
54             }
55         }
56     };
57
58 const openProjectItem = (resource: Resource, itemKind: ResourceKind, itemMode: ItemMode) =>
59     (dispatch: Dispatch, getState: () => RootState) => {
60
61         const { collections, projects } = getState();
62
63         if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
64             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(resource.uuid));
65         }
66
67         if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
68             dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(resource.uuid));
69         }
70
71         dispatch(push(getResourceUrl({ ...resource, kind: itemKind })));
72         dispatch(dataExplorerActions.SET_ITEMS({
73             id: PROJECT_PANEL_ID,
74             items: projectPanelItems(
75                 projects.items,
76                 resource.uuid,
77                 collections
78             )
79         }));
80     };