1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch, compose } from 'redux';
6 import { push } from "react-router-redux";
7 import { RootState } from "../store";
8 import { ResourceKind, Resource } from '~/models/resource';
9 import { getCollectionUrl } from "~/models/collection";
10 import { getProjectUrl } from "~/models/project";
11 import { getResource } from '~/store/resources/resources';
12 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
13 import { loadCollectionPanel } from '~/store/collection-panel/collection-panel-action';
14 import { snackbarActions } from '../snackbar/snackbar-actions';
15 import { resourceLabel } from "~/common/labels";
16 import { loadFavoritePanel } from '../favorite-panel/favorite-panel-action';
17 import { openProjectPanel, projectPanelActions } from '~/store/project-panel/project-panel-action';
18 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory } from '../side-panel-tree/side-panel-tree-actions';
19 import { Routes } from '~/routes/routes';
20 import { loadResource } from '../resources/resources-actions';
21 import { ServiceRepository } from '~/services/services';
22 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
23 import { projectPanelColumns } from '~/views/project-panel/project-panel';
24 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
25 import { matchRootRoute } from '~/routes/routes';
27 export const navigateToResource = (uuid: string) =>
28 async (dispatch: Dispatch, getState: () => RootState) => {
29 const resource = getResource(uuid)(getState().resources);
31 dispatch<any>(getResourceNavigationAction(resource));
35 const getResourceNavigationAction = (resource: Resource) => {
36 switch (resource.kind) {
37 case ResourceKind.COLLECTION:
38 return navigateToCollection(resource.uuid);
39 case ResourceKind.PROJECT:
40 case ResourceKind.USER:
41 return navigateToProject(resource.uuid);
43 return cannotNavigateToResource(resource);
47 export const loadWorkbench = () =>
48 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49 const { auth, router } = getState();
50 const { user } = auth;
52 const userResource = await dispatch<any>(loadResource(user.uuid));
54 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
55 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
56 dispatch<any>(initSidePanelTree());
57 if (router.location) {
58 const match = matchRootRoute(router.location.pathname);
60 dispatch(navigateToProject(userResource.uuid));
64 dispatch(userIsNotAuthenticated);
67 dispatch(userIsNotAuthenticated);
71 export const navigateToFavorites = push(Routes.FAVORITES);
73 export const loadFavorites = () =>
74 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
76 dispatch<any>(loadFavoritePanel());
80 export const navigateToProject = compose(push, getProjectUrl);
82 export const loadProject = (uuid: string) =>
83 (dispatch: Dispatch) => {
84 dispatch<any>(activateSidePanelTreeItem(uuid));
85 dispatch<any>(openProjectPanel(uuid));
86 dispatch(loadDetailsPanel(uuid));
89 export const navigateToCollection = compose(push, getCollectionUrl);
91 export const loadCollection = (uuid: string) =>
92 async (dispatch: Dispatch) => {
93 const collection = await dispatch<any>(loadCollectionPanel(uuid));
94 dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
95 dispatch(loadDetailsPanel(uuid));
98 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
99 snackbarActions.OPEN_SNACKBAR({
100 message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`
103 export const resourceIsNotLoaded = (uuid: string) =>
104 snackbarActions.OPEN_SNACKBAR({
105 message: `Resource identified by ${uuid} is not loaded.`
108 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
109 message: 'User is not authenticated'
112 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
113 message: 'Could not load user'