Merge branch '14099-process-service'
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
index 68d7fc5239528cb3bbcbb1a0464172fcfd713757..188acf12bd30afc55b838f6d31f279822e0b6628 100644 (file)
@@ -2,81 +2,34 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Dispatch } from "redux";
-import { projectActions, getProjectList } from "../project/project-action";
+import { Dispatch, compose } from 'redux';
 import { push } from "react-router-redux";
-import { TreeItemStatus } from "../../components/tree/tree";
-import { findTreeItem, getTreePath } from "../project/project-reducer";
-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 { getCollectionUrl } from "../../models/collection";
-import { getProjectUrl, ProjectResource } from "../../models/project";
-import { projectService } from "../../services/services";
-
-export const getResourceUrl = <T extends Resource>(resource: T): string => {
-    switch (resource.kind) {
-        case ResourceKind.PROJECT: return getProjectUrl(resource.uuid);
-        case ResourceKind.COLLECTION: return getCollectionUrl(resource.uuid);
-        default: return resource.href;
-    }
-};
-
-export enum ItemMode {
-    BOTH,
-    OPEN,
-    ACTIVE
-}
-
-export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
-    (dispatch: Dispatch, getState: () => RootState) => {
-        const { projects, router } = getState();
-        const treeItem = findTreeItem(projects.items, itemId);
-
-        if (treeItem) {
-            console.log('treeItem', treeItem);
-
-            const treePath = getTreePath(projects.items, treeItem.data.uuid);
-
-            console.log('treePath', treePath);
-            const resourceUrl = getResourceUrl(treeItem.data);
-
-            console.log('resourceUrl', resourceUrl);
-            const ancestors = loadProjectAncestors(treeItem.data.uuid);
-            console.log('ancestors', ancestors);
-
-            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(treePath[treePath.length - 1].id));
-            }
-
-            const promise = treeItem.status === TreeItemStatus.LOADED
-                ? Promise.resolve()
-                : dispatch<any>(getProjectList(itemId));
-
-            promise
-                .then(() => dispatch<any>(() => {
-                    if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
-                        dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
-                    }
-                    dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID}));
-                    dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID}));
-                }));
-
+import { ResourceKind, extractUuidKind } from '~/models/resource';
+import { getCollectionUrl } from "~/models/collection";
+import { getProjectUrl } from "~/models/project";
+
+import { SidePanelTreeCategory } from '../side-panel-tree/side-panel-tree-actions';
+import { Routes, getProcessUrl } from '~/routes/routes';
+
+export const navigateTo = (uuid: string) =>
+    async (dispatch: Dispatch) => {
+        const kind = extractUuidKind(uuid);
+        if (kind === ResourceKind.PROJECT || kind === ResourceKind.USER) {
+            dispatch<any>(navigateToProject(uuid));
+        } else if (kind === ResourceKind.COLLECTION) {
+            dispatch<any>(navigateToCollection(uuid));
+        } else if (kind === ResourceKind.CONTAINER_REQUEST) {
+            dispatch<any>(navigateToProcess(uuid));
+        }
+        if (uuid === SidePanelTreeCategory.FAVORITES) {
+            dispatch<any>(navigateToFavorites);
         }
     };
 
-    const USER_UUID_REGEX = /.*tpzed.*/;
+export const navigateToFavorites = push(Routes.FAVORITES);
 
-    export const loadProjectAncestors = async (uuid: string): Promise<Array<ProjectResource>> => {
-        if (USER_UUID_REGEX.test(uuid)) {
-            return [];
-        } else {
-            const currentProject = await projectService.get(uuid);
-            const ancestors = await loadProjectAncestors(currentProject.ownerUuid);
-            return [...ancestors, currentProject];
-        }
-    };
\ No newline at end of file
+export const navigateToProject = compose(push, getProjectUrl);
+
+export const navigateToCollection = compose(push, getCollectionUrl);
+
+export const navigateToProcess = compose(push, getProcessUrl);