Merge branch '13775-custom-theme-test-errors'
[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 } 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
17 export const getResourceUrl = (resource: Resource): string => {
18     switch (resource.kind) {
19         case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
20         case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
21         default: return "";
22     }
23 };
24
25 export enum ItemMode {
26     BOTH,
27     OPEN,
28     ACTIVE
29 }
30
31 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
32     (dispatch: Dispatch, getState: () => RootState) => {
33         const { projects, router, sidePanel } = getState();
34         const treeItem = findTreeItem(projects.items, itemId);
35
36         if (treeItem) {
37
38             dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY());
39             const projectsItem = sidePanelData[0];
40             if(sidePanel.some(item => item.id === projectsItem.id && !item.open)){
41                 dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(projectsItem.id));
42             }
43
44             if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
45                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
46             }
47
48             const resourceUrl = getResourceUrl({ ...treeItem.data });
49
50             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
51                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
52                     dispatch(push(resourceUrl));
53                 }
54                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
55             }
56
57             const promise = treeItem.status === TreeItemStatus.Loaded
58                 ? Promise.resolve()
59                 : dispatch<any>(getProjectList(itemId));
60
61             promise
62                 .then(() => dispatch<any>(() => {
63                     dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
64                     dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
65                 }));
66
67         }
68     };