Remove unused file, update side panel tree actions
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
index 0b4bcdf87ab033421319eaa472a3ea15449ea296..3256846be67ecc3c2975f60aa874543a60e15400 100644 (file)
 //
 // 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 { TreeItem, TreeItemStatus } from "../../components/tree/tree";
-import { getCollectionList } from "../collection/collection-action";
-import { findTreeItem } from "../project/project-reducer";
-import { Project } from "../../models/project";
-import { Resource, ResourceKind } from "../../models/resource";
-import sidePanelActions from "../side-panel/side-panel-action";
-
-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 enum ItemMode {
-    BOTH,
-    OPEN,
-    ACTIVE
-}
-
-export const setProjectItem = (projects: Array<TreeItem<Project>>, itemId: string, itemKind: ResourceKind, itemMode: ItemMode) => (dispatch: Dispatch) => {
-
-    const openProjectItem = (resource: Resource) => {
-        if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
-            dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(resource.uuid));
+import { RootState } from "../store";
+import { ResourceKind, Resource, extractUuidKind } from '~/models/resource';
+import { getCollectionUrl } from "~/models/collection";
+import { getProjectUrl } from "~/models/project";
+import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
+import { loadCollectionPanel } from '~/store/collection-panel/collection-panel-action';
+import { snackbarActions } from '../snackbar/snackbar-actions';
+import { resourceLabel } from "~/common/labels";
+import { loadFavoritePanel } from '../favorite-panel/favorite-panel-action';
+import { openProjectPanel, projectPanelActions } from '~/store/project-panel/project-panel-action';
+import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory } from '../side-panel-tree/side-panel-tree-actions';
+import { Routes } from '~/routes/routes';
+import { loadResource } from '../resources/resources-actions';
+import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
+import { projectPanelColumns } from '~/views/project-panel/project-panel';
+import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
+import { matchRootRoute } from '~/routes/routes';
+import { setCollectionBreadcrumbs, setProjectBreadcrumbs, setSidePanelBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
+
+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));
+        }
+        if (uuid === SidePanelTreeCategory.FAVORITES) {
+            dispatch<any>(navigateToFavorites);
         }
+    };
 
-        if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
-            dispatch(sidePanelActions.RESET_SIDE_PANEL_ACTIVITY(resource.uuid));
+export const loadWorkbench = () =>
+    async (dispatch: Dispatch, getState: () => RootState) => {
+        const { auth, router } = getState();
+        const { user } = auth;
+        if (user) {
+            const userResource = await dispatch<any>(loadResource(user.uuid));
+            if (userResource) {
+                dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
+                dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
+                dispatch<any>(initSidePanelTree());
+                if (router.location) {
+                    const match = matchRootRoute(router.location.pathname);
+                    if (match) {
+                        dispatch(navigateToProject(userResource.uuid));
+                    }
+                }
+            } else {
+                dispatch(userIsNotAuthenticated);
+            }
+        } else {
+            dispatch(userIsNotAuthenticated);
         }
+    };
 
-        dispatch(push(getResourceUrl({...resource, kind: itemKind})));
+export const navigateToFavorites = push(Routes.FAVORITES);
+
+export const loadFavorites = () =>
+    (dispatch: Dispatch) => {
+        dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
+        dispatch<any>(loadFavoritePanel());
+        dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
     };
 
-    let treeItem = findTreeItem(projects, itemId);
-    if (treeItem && itemKind === ResourceKind.LEVEL_UP) {
-        treeItem = findTreeItem(projects, treeItem.data.ownerUuid);
-    }
 
-    if (treeItem) {
-        dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
+export const navigateToProject = compose(push, getProjectUrl);
 
-        if (treeItem.status === TreeItemStatus.Loaded) {
-            openProjectItem(treeItem.data);
-        } else {
-            dispatch<any>(getProjectList(itemId))
-                .then(() => openProjectItem(treeItem!.data));
-        }
-        if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
-            dispatch<any>(getCollectionList(itemId));
-        }
-    }
-};
+export const loadProject = (uuid: string) =>
+    async (dispatch: Dispatch) => {
+        await dispatch<any>(activateSidePanelTreeItem(uuid));
+        dispatch<any>(setProjectBreadcrumbs(uuid));
+        dispatch<any>(openProjectPanel(uuid));
+        dispatch(loadDetailsPanel(uuid));
+    };
+
+export const navigateToCollection = compose(push, getCollectionUrl);
+
+export const loadCollection = (uuid: string) =>
+    async (dispatch: Dispatch) => {
+        const collection = await dispatch<any>(loadCollectionPanel(uuid));
+        await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
+        dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
+        dispatch(loadDetailsPanel(uuid));
+    };
+
+export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
+    snackbarActions.OPEN_SNACKBAR({
+        message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`
+    });
+
+export const resourceIsNotLoaded = (uuid: string) =>
+    snackbarActions.OPEN_SNACKBAR({
+        message: `Resource identified by ${uuid} is not loaded.`
+    });
+
+export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
+    message: 'User is not authenticated'
+});
+
+export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
+    message: 'Could not load user'
+});
\ No newline at end of file