Remove unused file, update side panel tree 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 { 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';
25
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));
33         }
34         if (uuid === SidePanelTreeCategory.FAVORITES) {
35             dispatch<any>(navigateToFavorites);
36         }
37     };
38
39 export const loadWorkbench = () =>
40     async (dispatch: Dispatch, getState: () => RootState) => {
41         const { auth, router } = getState();
42         const { user } = auth;
43         if (user) {
44             const userResource = await dispatch<any>(loadResource(user.uuid));
45             if (userResource) {
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);
51                     if (match) {
52                         dispatch(navigateToProject(userResource.uuid));
53                     }
54                 }
55             } else {
56                 dispatch(userIsNotAuthenticated);
57             }
58         } else {
59             dispatch(userIsNotAuthenticated);
60         }
61     };
62
63 export const navigateToFavorites = push(Routes.FAVORITES);
64
65 export const loadFavorites = () =>
66     (dispatch: Dispatch) => {
67         dispatch<any>(activateSidePanelTreeItem(SidePanelTreeCategory.FAVORITES));
68         dispatch<any>(loadFavoritePanel());
69         dispatch<any>(setSidePanelBreadcrumbs(SidePanelTreeCategory.FAVORITES));
70     };
71
72
73 export const navigateToProject = compose(push, getProjectUrl);
74
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));
81     };
82
83 export const navigateToCollection = compose(push, getCollectionUrl);
84
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));
91     };
92
93 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
94     snackbarActions.OPEN_SNACKBAR({
95         message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`
96     });
97
98 export const resourceIsNotLoaded = (uuid: string) =>
99     snackbarActions.OPEN_SNACKBAR({
100         message: `Resource identified by ${uuid} is not loaded.`
101     });
102
103 export const userIsNotAuthenticated = snackbarActions.OPEN_SNACKBAR({
104     message: 'User is not authenticated'
105 });
106
107 export const couldNotLoadUser = snackbarActions.OPEN_SNACKBAR({
108     message: 'Could not load user'
109 });