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, extractUuidKind } from '~/models/resource';
9 import { getCollectionUrl } from "~/models/collection";
10 import { getProjectUrl } from "~/models/project";
11 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
12 import { loadCollectionPanel } from '~/store/collection-panel/collection-panel-action';
13 import { snackbarActions } from '../snackbar/snackbar-actions';
14 import { resourceLabel } from "~/common/labels";
15 import { loadFavoritePanel } from '../favorite-panel/favorite-panel-action';
16 import { openProjectPanel, projectPanelActions } from '~/store/project-panel/project-panel-action';
17 import { activateSidePanelTreeItem, initSidePanelTree, SidePanelTreeCategory } from '../side-panel-tree/side-panel-tree-actions';
18 import { Routes } from '~/routes/routes';
19 import { loadResource } from '../resources/resources-actions';
20 import { favoritePanelActions } from '~/store/favorite-panel/favorite-panel-action';
21 import { projectPanelColumns } from '~/views/project-panel/project-panel';
22 import { favoritePanelColumns } from '~/views/favorite-panel/favorite-panel';
23 import { matchRootRoute } from '~/routes/routes';
24 import { setCollectionBreadcrumbs, setProjectBreadcrumbs, setSidePanelBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
26 export const navigateTo = (uuid: string) =>
27 async (dispatch: Dispatch) => {
28 const kind = extractUuidKind(uuid);
29 if (kind === ResourceKind.PROJECT || kind === ResourceKind.USER) {
30 dispatch<any>(navigateToProject(uuid));
31 } else if (kind === ResourceKind.COLLECTION) {
32 dispatch<any>(navigateToCollection(uuid));
34 if (uuid === SidePanelTreeCategory.FAVORITES) {
35 dispatch<any>(navigateToFavorites);
39 export const loadWorkbench = () =>
40 async (dispatch: Dispatch, getState: () => RootState) => {
41 const { auth, router } = getState();
42 const { user } = auth;
44 const userResource = await dispatch<any>(loadResource(user.uuid));
46 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
47 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
48 dispatch<any>(initSidePanelTree());
49 if (router.location) {
50 const match = matchRootRoute(router.location.pathname);
52 dispatch(navigateToProject(userResource.uuid));
56 dispatch(userIsNotAuthenticated);
59 dispatch(userIsNotAuthenticated);
63 export const navigateToFavorites = push(Routes.FAVORITES);
65 export const loadFavorites = () =>
66 (dispatch: Dispatch) => {
67 dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
68 dispatch<any>(loadFavoritePanel());
69 dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
73 export const navigateToProject = compose(push, getProjectUrl);
75 export const loadProject = (uuid: string) =>
76 async (dispatch: Dispatch) => {
77 await dispatch<any>(activateSidePanelTreeItem(uuid));
78 dispatch<any>(setProjectBreadcrumbs(uuid));
79 dispatch<any>(openProjectPanel(uuid));
80 dispatch(loadDetailsPanel(uuid));
83 export const navigateToCollection = compose(push, getCollectionUrl);
85 export const loadCollection = (uuid: string) =>
86 async (dispatch: Dispatch) => {
87 const collection = await dispatch<any>(loadCollectionPanel(uuid));
88 await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
89 dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
90 dispatch(loadDetailsPanel(uuid));
93 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
94 snackbarActions.OPEN_SNACKBAR({
95 message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`
98 export const resourceIsNotLoaded = (uuid: string) =>
99 snackbarActions.OPEN_SNACKBAR({
100 message: `Resource identified by ${uuid} is not loaded.`
103 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
104 message: 'User is not authenticated'
107 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
108 message: 'Could not load user'