X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/10ce16c28de952f6533ca3cc9df909269e3d2a53..eb4491eea9ba873845f7a5796d139d19977f8112:/src/store/navigation/navigation-action.ts diff --git a/src/store/navigation/navigation-action.ts b/src/store/navigation/navigation-action.ts index 3920b5a2..981b852f 100644 --- a/src/store/navigation/navigation-action.ts +++ b/src/store/navigation/navigation-action.ts @@ -3,20 +3,27 @@ // SPDX-License-Identifier: AGPL-3.0 import { Dispatch } from "redux"; -import { projectActions, getProjectList } from "../project/project-action"; +import { getProjectList, projectActions } from "../project/project-action"; import { push } from "react-router-redux"; -import { TreeItemStatus } from "../../components/tree/tree"; +import { TreeItemStatus } from "~/components/tree/tree"; import { findTreeItem } 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 { Resource, ResourceKind } 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 { SidePanelIdentifiers } from "../side-panel/side-panel-reducer"; +import { getUuidObjectType, ObjectTypes } from "~/models/object-types"; -export const getResourceUrl = (resource: T): string => { - switch (resource.kind) { - case ResourceKind.Project: return `/projects/${resource.uuid}`; - case ResourceKind.Collection: return `/collections/${resource.uuid}`; - default: return resource.href; +export const getResourceUrl = (resourceKind: ResourceKind, resourceUuid: string): string => { + switch (resourceKind) { + case ResourceKind.PROJECT: return getProjectUrl(resourceUuid); + case ResourceKind.COLLECTION: return getCollectionUrl(resourceUuid); + default: + return ''; } }; @@ -27,17 +34,14 @@ export enum ItemMode { } export const setProjectItem = (itemId: string, itemMode: ItemMode) => - (dispatch: Dispatch, getState: () => RootState) => { + (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => { + console.log("SetProjectItem!!", itemId); + debugger; const { projects, router } = getState(); const treeItem = findTreeItem(projects.items, itemId); if (treeItem) { - - if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) { - dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid)); - } - - const resourceUrl = getResourceUrl(treeItem.data); + const resourceUrl = getResourceUrl(treeItem.data.kind, treeItem.data.uuid); if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) { if (router.location && !router.location.pathname.includes(resourceUrl)) { @@ -46,15 +50,54 @@ export const setProjectItem = (itemId: string, itemMode: ItemMode) => dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid)); } - const promise = treeItem.status === TreeItemStatus.Loaded + const promise = treeItem.status === TreeItemStatus.LOADED ? Promise.resolve() : dispatch(getProjectList(itemId)); promise .then(() => dispatch(() => { - dispatch(dataExplorerActions.RESET_PAGINATION({id: PROJECT_PANEL_ID})); - dispatch(dataExplorerActions.REQUEST_ITEMS({id: PROJECT_PANEL_ID})); + 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()); + } } }; + +export const restoreBranch = (itemId: string) => + 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(SidePanelIdentifiers.PROJECTS)); + dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS)); + uuids.forEach(uuid => { + dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid)); + }); + }; + +export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise> => { + 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 => { + const [uuid, ...rest] = uuids; + if (uuid) { + await dispatch(getProjectList(uuid)); + return loadBranch(rest, dispatch); + } +};