Create breadcrumbs actions
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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 { 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';
26 import { setCollectionBreadcrumbs, setProjectBreadcrumbs, setSidePanelBreadcrumbs } from '../breadcrumbs/breadcrumbs-actions';
27
28 export const navigateTo = (uuid: string) =>
29     async (dispatch: Dispatch, getState: () => RootState) => {
30         const kind = extractUuidKind(uuid);
31         if (kind === ResourceKind.PROJECT || kind === ResourceKind.USER) {
32             dispatch<any>(navigateToProject(uuid));
33         } else if (kind === ResourceKind.COLLECTION) {
34             dispatch<any>(navigateToCollection(uuid));
35         }
36         if (uuid === SidePanelTreeCategory.FAVORITES) {
37             dispatch<any>(navigateToFavorites);
38         }
39     };
40
41 const getResourceNavigationAction = (resource: Resource) => {
42     switch (resource.kind) {
43         case ResourceKind.COLLECTION:
44             return navigateToCollection(resource.uuid);
45         case ResourceKind.PROJECT:
46         case ResourceKind.USER:
47             return navigateToProject(resource.uuid);
48         default:
49             return cannotNavigateToResource(resource);
50     }
51 };
52
53 export const loadWorkbench = () =>
54     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55         const { auth, router } = getState();
56         const { user } = auth;
57         if (user) {
58             const userResource = await dispatch<any>(loadResource(user.uuid));
59             if (userResource) {
60                 dispatch(projectPanelActions.SET_COLUMNS({ columns: projectPanelColumns }));
61                 dispatch(favoritePanelActions.SET_COLUMNS({ columns: favoritePanelColumns }));
62                 dispatch<any>(initSidePanelTree());
63                 if (router.location) {
64                     const match = matchRootRoute(router.location.pathname);
65                     if (match) {
66                         dispatch(navigateToProject(userResource.uuid));
67                     }
68                 }
69             } else {
70                 dispatch(userIsNotAuthenticated);
71             }
72         } else {
73             dispatch(userIsNotAuthenticated);
74         }
75     };
76
77 export const navigateToFavorites = push(Routes.FAVORITES);
78
79 export const loadFavorites = () =>
80     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
81         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
82         dispatch<any>(loadFavoritePanel());
83         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
84     };
85
86
87 export const navigateToProject = compose(push, getProjectUrl);
88
89 export const loadProject = (uuid: string) =>
90     async (dispatch: Dispatch) => {
91         await dispatch<any>(activateSidePanelTreeItem(uuid));
92         dispatch<any>(setProjectBreadcrumbs(uuid));
93         dispatch<any>(openProjectPanel(uuid));
94         dispatch(loadDetailsPanel(uuid));
95     };
96
97 export const navigateToCollection = compose(push, getCollectionUrl);
98
99 export const loadCollection = (uuid: string) =>
100     async (dispatch: Dispatch) => {
101         const collection = await dispatch<any>(loadCollectionPanel(uuid));
102         await dispatch<any>(activateSidePanelTreeItem(collection.ownerUuid));
103         dispatch<any>(setCollectionBreadcrumbs(collection.uuid));
104         dispatch(loadDetailsPanel(uuid));
105     };
106
107 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
108     snackbarActions.OPEN_SNACKBAR({
109         message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`
110     });
111
112 export const resourceIsNotLoaded = (uuid: string) =>
113     snackbarActions.OPEN_SNACKBAR({
114         message: `Resource identified by ${uuid} is not loaded.`
115     });
116
117 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
118     message: 'User is not authenticated'
119 });
120
121 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
122     message: 'Could not load user'
123 });