034cdacc329fdc84cd3f8cd40cf7e6d091fe441b
[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 { findTreeItem } from "../project/project-reducer";
10 import { Resource, ResourceKind as R } from "../../models/resource";
11 import sidePanelActions from "../side-panel/side-panel-action";
12 import dataExplorerActions from "../data-explorer/data-explorer-action";
13 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
14 import { RootState } from "../store";
15 import { sidePanelData } from "../side-panel/side-panel-reducer";
16 import { loadDetails } from "../details-panel/details-panel-action";
17 import { ResourceKind } from "../../models/kinds";
18
19 export const getResourceUrl = (resource: Resource): string => {
20     switch (resource.kind) {
21         case R.PROJECT: return `/projects/${resource.uuid}`;
22         case R.COLLECTION: return `/collections/${resource.uuid}`;
23         default: return "";
24     }
25 };
26
27 export enum ItemMode {
28     BOTH,
29     OPEN,
30     ACTIVE
31 }
32
33 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
34     (dispatch: Dispatch, getState: () => RootState) => {
35         const { projects, router, sidePanel } = getState();
36         const treeItem = findTreeItem(projects.items, itemId);
37
38         if (treeItem) {
39             // TODO: Get correct resource kind
40             dispatch<any>(loadDetails(treeItem.data.uuid, ResourceKind.Project));
41
42             dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY());
43             const projectsItem = sidePanelData[0];
44             if(sidePanel.some(item => item.id === projectsItem.id && !item.open)){
45                 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(projectsItem.id));
46             }
47
48             if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
49                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
50             }
51
52             const resourceUrl = getResourceUrl({ ...treeItem.data });
53
54             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
55                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
56                     dispatch(push(resourceUrl));
57                 }
58                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
59             }
60
61             const promise = treeItem.status === TreeItemStatus.Loaded
62                 ? Promise.resolve()
63                 : dispatch<any>(getProjectList(itemId));
64
65             promise
66                 .then(() => dispatch<any>(() => {
67                     dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
68                     dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
69                 }));
70
71         }
72     };