Merge branch 'master' into 13969-advanced-tab
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
index b811f9a292b8b42bf9cfb6ba8fcdf93db017351d..b63fc2cb290af38edb74be1da9e9beb28c3a5a2c 100644 (file)
@@ -2,79 +2,62 @@
 //
 // 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 { getCollectionList } from "../collection/collection-action";
-import { findTreeItem } from "../project/project-reducer";
-import { Resource, ResourceKind } from "../../models/resource";
-import sidePanelActions from "../side-panel/side-panel-action";
-import dataExplorerActions from "../data-explorer/data-explorer-action";
-import { PROJECT_PANEL_ID } from "../../views/project-panel/project-panel";
-import { projectPanelItems } from "../../views/project-panel/project-panel-selectors";
-import { RootState } from "../store";
+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, getProcessLogUrl } from '~/routes/routes';
+import { RootState } from '~/store/store';
+import { ServiceRepository } from '~/services/services';
+
+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);
+        } else if (uuid === SidePanelTreeCategory.SHARED_WITH_ME) {
+            dispatch(navigateToSharedWithMe);
+        } else if (uuid === SidePanelTreeCategory.WORKFLOWS) {
+            dispatch(navigateToWorkflows);
+        } else if (uuid === SidePanelTreeCategory.TRASH) {
+            dispatch(navigateToTrash);
+        }
+    };
 
-export const getResourceUrl = (resource: Resource): 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 "#";
-    }
-};
+export const navigateToRoot = push(Routes.ROOT);
 
-export enum ItemMode {
-    BOTH,
-    OPEN,
-    ACTIVE
-}
+export const navigateToFavorites = push(Routes.FAVORITES);
 
-export const setProjectItem = (itemId: string, itemKind = ResourceKind.PROJECT, itemMode = ItemMode.OPEN) =>
-    (dispatch: Dispatch, getState: () => RootState) => {
-        const { projects } = getState();
+export const navigateToTrash = push(Routes.TRASH);
 
-        let treeItem = findTreeItem(projects.items, itemId);
-        if (treeItem && itemKind === ResourceKind.LEVEL_UP) {
-            treeItem = findTreeItem(projects.items, treeItem.data.ownerUuid);
-        }
+export const navigateToWorkflows = push(Routes.WORKFLOWS);
 
-        if (treeItem) {
-            dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
+export const navigateToProject = compose(push, getProjectUrl);
 
-            if (treeItem.status === TreeItemStatus.Loaded) {
-                dispatch<any>(openProjectItem(treeItem.data, itemKind, itemMode));
-            } else {
-                dispatch<any>(getProjectList(itemId))
-                    .then(() => dispatch<any>(openProjectItem(treeItem!.data, itemKind, itemMode)));
-            }
-            if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
-                dispatch<any>(getCollectionList(itemId));
-            }
-        }
-    };
+export const navigateToCollection = compose(push, getCollectionUrl);
 
-const openProjectItem = (resource: Resource, itemKind: ResourceKind, itemMode: ItemMode) =>
-    (dispatch: Dispatch, getState: () => RootState) => {
+export const navigateToProcess = compose(push, getProcessUrl);
 
-        const { collections, projects } = getState();
+export const navigateToProcessLogs = compose(push, getProcessLogUrl);
 
-        if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
-            dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(resource.uuid));
-        }
+export const navigateToRootProject = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+    const rootProjectUuid = services.authService.getUuid();
+    if (rootProjectUuid) {
+        dispatch(navigateToProject(rootProjectUuid));
+    }
+};
 
-        if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
-            dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(resource.uuid));
-        }
+export const navigateToSharedWithMe = push(Routes.SHARED_WITH_ME);
 
-        dispatch(push(getResourceUrl({ ...resource, kind: itemKind })));
-        dispatch(dataExplorerActions.SET_ITEMS({
-            id: PROJECT_PANEL_ID,
-            items: projectPanelItems(
-                projects.items,
-                resource.uuid,
-                collections
-            )
-        }));
-    };
+export const navigateToRunProcess = push(Routes.RUN_PROCESS);
+
+export const navigateToSearchResults = push(Routes.SEARCH_RESULTS);