Refactor to apply global navigation 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 } 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
27 export const navigateToResource = (uuid: string) =>
28     async (dispatch: Dispatch, getState: () => RootState) => {
29         const resource = getResource(uuid)(getState().resources);
30         if (resource) {
31             dispatch<any>(getResourceNavigationAction(resource));
32         }
33     };
34
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);
42         default:
43             return cannotNavigateToResource(resource);
44     }
45 };
46
47 export const loadWorkbench = () =>
48     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
49         const { auth, router } = getState();
50         const { user } = auth;
51         if (user) {
52             const userResource = await dispatch<any>(loadResource(user.uuid));
53             if (userResource) {
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);
59                     if (match) {
60                         dispatch(navigateToProject(userResource.uuid));
61                     }
62                 }
63             } else {
64                 dispatch(userIsNotAuthenticated);
65             }
66         } else {
67             dispatch(userIsNotAuthenticated);
68         }
69     };
70
71 export const navigateToFavorites = push(Routes.FAVORITES);
72
73 export const loadFavorites = () =>
74     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
75         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
76         dispatch<any>(loadFavoritePanel());
77     };
78
79
80 export const navigateToProject = compose(push, getProjectUrl);
81
82 export const loadProject = (uuid: string) =>
83     (dispatch: Dispatch) => {
84         dispatch<any>(activateSidePanelTreeItem(uuid));
85         dispatch<any>(openProjectPanel(uuid));
86         dispatch(loadDetailsPanel(uuid));
87     };
88
89 export const navigateToCollection = compose(push, getCollectionUrl);
90
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));
96     };
97
98 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
99     snackbarActions.OPEN_SNACKBAR({
100         message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`
101     });
102
103 export const resourceIsNotLoaded = (uuid: string) =>
104     snackbarActions.OPEN_SNACKBAR({
105         message: `Resource identified by ${uuid} is not loaded.`
106     });
107
108 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
109     message: 'User is not authenticated'
110 });
111
112 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
113     message: 'Could not load user'
114 });