Merge branch 'master' into 13883-arrow-animation-is-not-working-after-loading
[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 { dataExplorerActions } from "../data-explorer/data-explorer-action";
11 import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
12 import { RootState } from "../store";
13 import { Resource, ResourceKind } from "../../models/resource";
14
15 export const getResourceUrl = <T extends Resource>(resource: T): string => {
16     switch (resource.kind) {
17         case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
18         case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
19         default: return resource.href;
20     }
21 };
22
23 export enum ItemMode {
24     BOTH,
25     OPEN,
26     ACTIVE
27 }
28
29 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
30     (dispatch: Dispatch, getState: () => RootState) => {
31         const { projects, router } = getState();
32         const treeItem = findTreeItem(projects.items, itemId);
33
34         if (treeItem) {
35
36             const resourceUrl = getResourceUrl(treeItem.data);
37
38             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
39                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
40                     dispatch(push(resourceUrl));
41                 }
42                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
43             }
44
45             const promise = treeItem.status === TreeItemStatus.LOADED
46                 ? Promise.resolve()
47                 : dispatch<any>(getProjectList(itemId));
48
49             promise
50                 .then(() => dispatch<any>(() => {
51                     if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
52                         dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
53                     }
54                     dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
55                     dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
56                 }));
57
58         }
59     };
60