X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/044eba18bb2cdc9d3633deef5e91b7eac2a03ac4..5627bf1a83323d2b0364cb069564998eb8c6ca7a:/src/store/navigation/navigation-action.ts diff --git a/src/store/navigation/navigation-action.ts b/src/store/navigation/navigation-action.ts index edb03682bd..3256846be6 100644 --- a/src/store/navigation/navigation-action.ts +++ b/src/store/navigation/navigation-action.ts @@ -2,59 +2,108 @@ // // 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 } 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"; - -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 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) { - - const resourceUrl = getResourceUrl(treeItem.data); - - if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) { - if (router.location && !router.location.pathname.includes(resourceUrl)) { - dispatch(push(resourceUrl)); +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(navigateToProject(uuid)); + } else if (kind === ResourceKind.COLLECTION) { + dispatch(navigateToCollection(uuid)); + } + if (uuid === SidePanelTreeCategory.FAVORITES) { + dispatch(navigateToFavorites); + } + }; + +export const loadWorkbench = () => + async (dispatch: Dispatch, getState: () => RootState) => { + const { auth, router } = getState(); + const { user } = auth; + if (user) { + const userResource = await dispatch(loadResource(user.uuid)); + if (userResource) { + dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns })); + dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns })); + dispatch(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); } + } else { + dispatch(userIsNotAuthenticated); + } + }; - const promise = treeItem.status === TreeItemStatus.Loaded - ? Promise.resolve() - : dispatch(getProjectList(itemId)); +export const navigateToFavorites = push(Routes.FAVORITES); - promise - .then(() => dispatch(() => { - 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})); - })); +export const loadFavorites = () => + (dispatch: Dispatch) => { + dispatch(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES)); + dispatch(loadFavoritePanel()); + dispatch(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES)); + }; - } + +export const navigateToProject = compose(push, getProjectUrl); + +export const loadProject = (uuid: string) => + async (dispatch: Dispatch) => { + await dispatch(activateSidePanelTreeItem(uuid)); + dispatch(setProjectBreadcrumbs(uuid)); + dispatch(openProjectPanel(uuid)); + dispatch(loadDetailsPanel(uuid)); }; +export const navigateToCollection = compose(push, getCollectionUrl); + +export const loadCollection = (uuid: string) => + async (dispatch: Dispatch) => { + const collection = await dispatch(loadCollectionPanel(uuid)); + await dispatch(activateSidePanelTreeItem(collection.ownerUuid)); + dispatch(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