a4b9a8f6ede99f23d6ef58ae35710a04ac08912e
[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.PROJECT: return `/projects/${resource.uuid}`;
21         case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
22         default: return "";
23     }
24 };
25
26 export enum ItemMode {
27     BOTH,
28     OPEN,
29     ACTIVE
30 }
31
32 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
33     (dispatch: Dispatch, getState: () => RootState) => {
34         const { projects, router } = getState();
35         const treeItem = findTreeItem(projects.items, itemId);
36
37         if (treeItem) {
38
39             dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY());
40
41             if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
42                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
43             }
44
45             const resourceUrl = getResourceUrl({ ...treeItem.data });
46
47             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
48                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
49                     dispatch(push(resourceUrl));
50                 }
51                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
52             }
53
54             const promise = treeItem.status === TreeItemStatus.Loaded
55                 ? Promise.resolve()
56                 : dispatch<any>(getProjectList(itemId));
57
58             promise
59                 .then(() => dispatch<any>(getCollectionList(itemId)))
60                 .then(() => dispatch<any>(() => {
61                     const { projects, collections } = getState();
62                     dispatch(dataExplorerActions.SET_ITEMS({
63                         id: PROJECT_PANEL_ID,
64                         items: projectPanelItems(
65                             projects.items,
66                             treeItem.data.uuid,
67                             collections
68                         )
69                     }));
70                 }));
71
72         }
73     };