Merge branch 'master' into 13765-information-inside-details-panel
[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
40             dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY());
41             const projectsItem = sidePanelData[0];
42             if(sidePanel.some(item => item.id === projectsItem.id && !item.open)){
43                 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(projectsItem.id));
44             }
45
46             if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
47                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
48             }
49
50             const resourceUrl = getResourceUrl({ ...treeItem.data });
51
52             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
53                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
54                     dispatch(push(resourceUrl));
55                 }
56                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
57             }
58
59             const promise = treeItem.status === TreeItemStatus.Loaded
60                 ? Promise.resolve()
61                 : dispatch<any>(getProjectList(itemId));
62
63             promise
64                 .then(() => dispatch<any>(() => {
65                     dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
66                     dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
67                 }));
68
69         }
70     };