Merge branch '13797-refatoring-part2'
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
index 6d1754d92cd827f3d191f6ff1510c50a1bd541af..3920b5a27c69bf17cfbeca27a7c0bda562662f47 100644 (file)
@@ -3,63 +3,58 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { Dispatch } from "redux";
-import projectActions, { getProjectList } from "../project/project-action";
+import { projectActions, getProjectList } from "../project/project-action";
 import { push } from "react-router-redux";
-import { TreeItem, TreeItemStatus } from "../../components/tree/tree";
-import { getCollectionList } from "../collection/collection-action";
+import { TreeItemStatus } from "../../components/tree/tree";
 import { findTreeItem } from "../project/project-reducer";
-import { Project } from "../../models/project";
+import { dataExplorerActions } from "../data-explorer/data-explorer-action";
+import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
+import { RootState } from "../store";
 import { Resource, ResourceKind } from "../../models/resource";
-import sidePanelActions from "../side-panel/side-panel-action";
 
-export const getResourceUrl = (resource: Resource): string => {
+export const getResourceUrl = <T extends Resource>(resource: T): string => {
     switch (resource.kind) {
-        case ResourceKind.LEVEL_UP: return `/projects/${resource.ownerUuid}`;
-        case ResourceKind.PROJECT: return `/projects/${resource.uuid}`;
-        case ResourceKind.COLLECTION: return `/collections/${resource.uuid}`;
-        default:
-            return "#";
+        case ResourceKind.Project: return `/projects/${resource.uuid}`;
+        case ResourceKind.Collection: return `/collections/${resource.uuid}`;
+        default: return resource.href;
     }
 };
 
-export const setProjectItem = (projects: Array<TreeItem<Project>>, itemId: string, itemKind: ResourceKind) => (dispatch: Dispatch) => {
+export enum ItemMode {
+    BOTH,
+    OPEN,
+    ACTIVE
+}
 
-    const openProjectItem = (resource: Resource) => {
-        dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(resource.uuid));
-        dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(resource.uuid));
-        dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(resource.uuid));
-        dispatch(push(getResourceUrl({...resource, kind: itemKind})));
-    };
-    const treeItem = findTreeItem(projects, itemId);
+export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
+    (dispatch: Dispatch, getState: () => RootState) => {
+        const { projects, router } = getState();
+        const treeItem = findTreeItem(projects.items, itemId);
 
-    if (treeItem) {
-        if (treeItem.status === TreeItemStatus.Loaded) {
-            openProjectItem(treeItem.data);
-        } else {
-            dispatch<any>(getProjectList(itemId))
-                .then(() => openProjectItem(treeItem.data));
-        }
-        dispatch<any>(getCollectionList(itemId));
+        if (treeItem) {
 
-        // if (item.type === ResourceKind.PROJECT || item.type === ResourceKind.LEVEL_UP) {
-        //     this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM(item.uuid));
-        // }
-        // this.props.dispatch<any>(getCollectionList(item.uuid));
+            if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
+                dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
+            }
 
-    }
-};
+            const resourceUrl = getResourceUrl(treeItem.data);
+
+            if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
+                if (router.location && !router.location.pathname.includes(resourceUrl)) {
+                    dispatch(push(resourceUrl));
+                }
+                dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
+            }
 
-    // toggleProjectTreeItemActive = (itemId: string, status: TreeItemStatus) => {
-    //     if (status === TreeItemStatus.Loaded) {
-    //         this.openProjectItem(itemId);
-    //         this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
-    //         this.props.dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(itemId));
-    //     } else {
-    //         this.props.dispatch<any>(getProjectList(itemId))
-    //             .then(() => {
-    //                 this.openProjectItem(itemId);
-    //                 this.props.dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(itemId));
-    //                 this.props.dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(itemId));
-    //             });
-    //     }
-    // }
+            const promise = treeItem.status === TreeItemStatus.Loaded
+                ? Promise.resolve()
+                : dispatch<any>(getProjectList(itemId));
+
+            promise
+                .then(() => dispatch<any>(() => {
+                    dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
+                    dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
+                }));
+
+        }
+    };