19069: Fix create workflow test
[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, AnyAction } from 'redux';
6 import { push } from "react-router-redux";
7 import { ResourceKind, extractUuidKind } from 'models/resource';
8 import { SidePanelTreeCategory } from '../side-panel-tree/side-panel-tree-actions';
9 import { Routes, getGroupUrl, getNavUrl, getUserProfileUrl } from 'routes/routes';
10 import { RootState } from 'store/store';
11 import { ServiceRepository } from 'services/services';
12 import { pluginConfig } from 'plugins';
13 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
14 import { USERS_PANEL_LABEL, MY_ACCOUNT_PANEL_LABEL } from 'store/breadcrumbs/breadcrumbs-actions';
15
16 const navigationNotAvailable = (id: string) =>
17     snackbarActions.OPEN_SNACKBAR({
18         message: `${id} not available`,
19         hideDuration: 3000,
20         kind: SnackbarKind.ERROR
21     });
22
23 export const navigateTo = (uuid: string) =>
24     async (dispatch: Dispatch, getState: () => RootState) => {
25
26         for (const navToFn of pluginConfig.navigateToHandlers) {
27             if (navToFn(dispatch, getState, uuid)) {
28                 return;
29             }
30         }
31
32         const kind = extractUuidKind(uuid);
33         switch (kind) {
34             case ResourceKind.PROJECT:
35             case ResourceKind.USER:
36             case ResourceKind.COLLECTION:
37             case ResourceKind.CONTAINER_REQUEST:
38                 dispatch<any>(pushOrGoto(getNavUrl(uuid, getState().auth)));
39                 return;
40             case ResourceKind.VIRTUAL_MACHINE:
41                 dispatch<any>(navigateToAdminVirtualMachines);
42                 return;
43         }
44
45         switch (uuid) {
46             case SidePanelTreeCategory.PROJECTS:
47                 const usr = getState().auth.user;
48                 if (usr) {
49                     dispatch<any>(pushOrGoto(getNavUrl(usr.uuid, getState().auth)));
50                 }
51                 return;
52             case SidePanelTreeCategory.FAVORITES:
53                 dispatch<any>(navigateToFavorites);
54                 return;
55             case SidePanelTreeCategory.PUBLIC_FAVORITES:
56                 dispatch(navigateToPublicFavorites);
57                 return;
58             case SidePanelTreeCategory.SHARED_WITH_ME:
59                 dispatch(navigateToSharedWithMe);
60                 return;
61             case SidePanelTreeCategory.TRASH:
62                 dispatch(navigateToTrash);
63                 return;
64             case SidePanelTreeCategory.GROUPS:
65                 dispatch(navigateToGroups);
66                 return;
67             case SidePanelTreeCategory.ALL_PROCESSES:
68                 dispatch(navigateToAllProcesses);
69                 return;
70             case USERS_PANEL_LABEL:
71                 dispatch(navigateToUsers);
72                 return;
73             case MY_ACCOUNT_PANEL_LABEL:
74                 dispatch(navigateToMyAccount);
75                 return;
76         }
77
78         dispatch(navigationNotAvailable(uuid));
79     };
80
81
82 export const navigateToNotFound = push(Routes.NO_MATCH);
83
84 export const navigateToRoot = push(Routes.ROOT);
85
86 export const navigateToFavorites = push(Routes.FAVORITES);
87
88 export const navigateToTrash = push(Routes.TRASH);
89
90 export const navigateToPublicFavorites = push(Routes.PUBLIC_FAVORITES);
91
92 export const navigateToWorkflows = push(Routes.WORKFLOWS);
93
94 export const pushOrGoto = (url: string): AnyAction => {
95     if (url === "") {
96         return { type: "noop" };
97     } else if (url[0] === '/') {
98         return push(url);
99     } else {
100         window.location.href = url;
101         return { type: "noop" };
102     }
103 };
104
105
106 export const navigateToRootProject = (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
107     navigateTo(SidePanelTreeCategory.PROJECTS)(dispatch, getState);
108 };
109
110 export const navigateToSharedWithMe = push(Routes.SHARED_WITH_ME);
111
112 export const navigateToRunProcess = push(Routes.RUN_PROCESS);
113
114 export const navigateToSearchResults = (searchValue: string) => {
115     if (searchValue !== "") {
116         return push({ pathname: Routes.SEARCH_RESULTS, search: '?q=' + encodeURIComponent(searchValue) });
117     } else {
118         return push({ pathname: Routes.SEARCH_RESULTS });
119     }
120 };
121
122 export const navigateToUserVirtualMachines = push(Routes.VIRTUAL_MACHINES_USER);
123
124 export const navigateToAdminVirtualMachines = push(Routes.VIRTUAL_MACHINES_ADMIN);
125
126 export const navigateToRepositories = push(Routes.REPOSITORIES);
127
128 export const navigateToSshKeysAdmin = push(Routes.SSH_KEYS_ADMIN);
129
130 export const navigateToSshKeysUser = push(Routes.SSH_KEYS_USER);
131
132 export const navigateToSiteManager = push(Routes.SITE_MANAGER);
133
134 export const navigateToMyAccount = push(Routes.MY_ACCOUNT);
135
136 export const navigateToLinkAccount = push(Routes.LINK_ACCOUNT);
137
138 export const navigateToKeepServices = push(Routes.KEEP_SERVICES);
139
140 export const navigateToUsers = push(Routes.USERS);
141
142 export const navigateToUserProfile = compose(push, getUserProfileUrl);
143
144 export const navigateToApiClientAuthorizations = push(Routes.API_CLIENT_AUTHORIZATIONS);
145
146 export const navigateToGroups = push(Routes.GROUPS);
147
148 export const navigateToGroupDetails = compose(push, getGroupUrl);
149
150 export const navigateToLinks = push(Routes.LINKS);
151
152 export const navigateToCollectionsContentAddress = push(Routes.COLLECTIONS_CONTENT_ADDRESS);
153
154 export const navigateToAllProcesses = push(Routes.ALL_PROCESSES);