Remove unnecessary navigation operations
[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             if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
41                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
42             }
43
44             const resourceUrl = getResourceUrl({ ...treeItem.data });
45
46             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
47                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
48                     dispatch(push(resourceUrl));
49                 }
50                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
51             }
52
53             const promise = treeItem.status === TreeItemStatus.Loaded
54                 ? Promise.resolve()
55                 : dispatch<any>(getProjectList(itemId));
56
57             promise
58                 .then(() => dispatch<any>(() => {
59                     dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
60                     dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
61                 }));
62
63         }
64     };