Refactor to apply global navigation actions
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
index d440f19009982b7daee59692d844ca65d6900c8a..db8efbfd989291a8dcf66dd8a49f72a2a338c75c 100644 (file)
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Dispatch } from "redux";
-import { getProjectList, projectActions } from "../project/project-action";
+import { Dispatch, compose } from 'redux';
 import { push } from "react-router-redux";
-import { TreeItemStatus } from "~/components/tree/tree";
-import { findTreeItem } from "../project/project-reducer";
 import { RootState } from "../store";
 import { ResourceKind, Resource } from '~/models/resource';
-import { projectPanelActions } from "../project-panel/project-panel-action";
 import { getCollectionUrl } from "~/models/collection";
-import { getProjectUrl, ProjectResource } from "~/models/project";
-import { ProjectService } from "~/services/project-service/project-service";
-import { ServiceRepository } from "~/services/services";
-import { sidePanelActions } from "../side-panel/side-panel-action";
-import { SidePanelId } from "../side-panel/side-panel-reducer";
-import { getUuidObjectType, ObjectTypes } from "~/models/object-types";
+import { getProjectUrl } from "~/models/project";
 import { getResource } from '~/store/resources/resources';
 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
-import { loadCollection } from '~/store/collection-panel/collection-panel-action';
-import { GroupContentsResource } from "~/services/groups-service/groups-service";
+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 { ServiceRepository } from '~/services/services';
+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';
 
-export const getResourceUrl = (resourceKind: ResourceKind, resourceUuid: string): string => {
-    switch (resourceKind) {
-        case ResourceKind.PROJECT: return getProjectUrl(resourceUuid);
-        case ResourceKind.COLLECTION: return getCollectionUrl(resourceUuid);
+export const navigateToResource = (uuid: string) =>
+    async (dispatch: Dispatch, getState: () => RootState) => {
+        const resource = getResource(uuid)(getState().resources);
+        if (resource) {
+            dispatch<any>(getResourceNavigationAction(resource));
+        }
+    };
+
+const getResourceNavigationAction = (resource: Resource) => {
+    switch (resource.kind) {
+        case ResourceKind.COLLECTION:
+            return navigateToCollection(resource.uuid);
+        case ResourceKind.PROJECT:
+        case ResourceKind.USER:
+            return navigateToProject(resource.uuid);
         default:
-            return '';
+            return cannotNavigateToResource(resource);
     }
 };
 
-export enum ItemMode {
-    BOTH,
-    OPEN,
-    ACTIVE
-}
-
-export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
-    (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const { projects, router } = getState();
-        const treeItem = findTreeItem(projects.items, itemId);
-
-        if (treeItem) {
-            const resourceUrl = getResourceUrl(treeItem.data.kind, treeItem.data.uuid);
-
-            if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
-                if (router.location && !router.location.pathname.includes(resourceUrl)) {
-                    dispatch(push(resourceUrl));
+export const loadWorkbench = () =>
+    async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+        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));
+                    }
                 }
-                dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
+            } else {
+                dispatch(userIsNotAuthenticated);
             }
-
-            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(projectPanelActions.RESET_PAGINATION());
-                    dispatch(projectPanelActions.REQUEST_ITEMS());
-                }));
         } else {
-            const uuid = services.authService.getUuid();
-            if (itemId === uuid) {
-                dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
-                dispatch(projectPanelActions.RESET_PAGINATION());
-                dispatch(projectPanelActions.REQUEST_ITEMS());
-            }
+            dispatch(userIsNotAuthenticated);
         }
     };
 
-export const restoreBranch = (itemId: string) =>
+export const navigateToFavorites = push(Routes.FAVORITES);
+
+export const loadFavorites = () =>
     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
-        const ancestors = await loadProjectAncestors(itemId, services.projectService);
-        const uuids = ancestors.map(ancestor => ancestor.uuid);
-        await loadBranch(uuids, dispatch);
-        dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelId.PROJECTS));
-        uuids.forEach(uuid => {
-            dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
-        });
+        dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
+        dispatch<any>(loadFavoritePanel());
     };
 
-export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
-    if (getUuidObjectType(uuid) === ObjectTypes.USER) {
-        return [];
-    } else {
-        const currentProject = await projectService.get(uuid);
-        const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
-        return [...ancestors, currentProject];
-    }
-};
 
-const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
-    const [uuid, ...rest] = uuids;
-    if (uuid) {
-        await dispatch<any>(getProjectList(uuid));
-        return loadBranch(rest, dispatch);
-    }
-};
-
-export const navigateToResource = (uuid: string) =>
-    (dispatch: Dispatch, getState: () => RootState) => {
-        const resource = getResource(uuid)(getState().resources);
-        resource
-            ? dispatch<any>(getResourceNavigationAction(resource))
-            : dispatch<any>(resourceIsNotLoaded(uuid));
-    };
+export const navigateToProject = compose(push, getProjectUrl);
 
-const getResourceNavigationAction = (resource: Resource) => {
-    switch (resource.kind) {
-        case ResourceKind.COLLECTION:
-            return navigateToCollection(resource);
-        case ResourceKind.PROJECT:
-            return navigateToProject(resource);
-        default:
-            return cannotNavigateToResource(resource);
-    }
-};
-
-export const navigateToProject = ({ uuid }: Resource) =>
+export const loadProject = (uuid: string) =>
     (dispatch: Dispatch) => {
-        dispatch<any>(setProjectItem(uuid, ItemMode.BOTH));
+        dispatch<any>(activateSidePanelTreeItem(uuid));
+        dispatch<any>(openProjectPanel(uuid));
         dispatch(loadDetailsPanel(uuid));
     };
 
-export const navigateToCollection = ({ uuid }: Resource) =>
-    (dispatch: Dispatch) => {
-        dispatch<any>(loadCollection(uuid));
-        dispatch(push(getCollectionUrl(uuid)));
+export const navigateToCollection = compose(push, getCollectionUrl);
+
+export const loadCollection = (uuid: string) =>
+    async (dispatch: Dispatch) => {
+        const collection = await dispatch<any>(loadCollectionPanel(uuid));
+        dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
+        dispatch(loadDetailsPanel(uuid));
     };
 
 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
     snackbarActions.OPEN_SNACKBAR({
-        message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`,
-        hideDuration: 3000
+        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.`,
-        hideDuration: 3000
+        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