Clean up route-change-handlers imports
[arvados-workbench2.git] / src / routes / route-change-handlers.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { History, Location } from 'history';
6 import { match } from 'react-router-dom';
7 import { Dispatch } from 'redux';
8 import { ThunkAction } from 'redux-thunk';
9 import { RootStore } from '~/store/store';
10 import * as R from '~/routes/routes';
11 import * as WA from '~/store/workbench/workbench-actions';
12 import { navigateToRootProject } from '~/store/navigation/navigation-action';
13 import { dialogActions } from '~/store/dialog/dialog-actions';
14 import { contextMenuActions } from '~/store/context-menu/context-menu-actions';
15 import { searchBarActions } from '~/store/search-bar/search-bar-actions';
16
17 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
18     const handler = handleLocationChange(store);
19     handler(history.location);
20     history.listen(handler);
21 };
22
23 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
24
25     store.dispatch(dialogActions.CLOSE_ALL_DIALOGS());
26     store.dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
27     store.dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
28
29     locationChangeHandlers.find(handler => handler(store.dispatch, pathname));
30
31 };
32
33 type MatchRoute<Params> = (route: string) => match<Params> | null;
34 type ActionCreator<Params> = (params: Params) => void;
35
36 const handle = <Params>(matchRoute: MatchRoute<Params>, actionCreator: ActionCreator<Params>) =>
37     (dispatch: Dispatch, route: string) => {
38         const match = matchRoute(route);
39         return match
40             ? (
41                 dispatch<any>(actionCreator(match.params)),
42                 true
43             )
44             : false;
45     };
46
47 const locationChangeHandlers = [
48
49     handle(
50         R.matchApiClientAuthorizationsRoute,
51         () => WA.loadApiClientAuthorizations
52     ),
53
54     handle(
55         R.matchCollectionRoute,
56         ({ id }) => WA.loadCollection(id)
57     ),
58
59     handle(
60         R.matchComputeNodesRoute,
61         () => WA.loadComputeNodes
62     ),
63
64     handle(
65         R.matchFavoritesRoute,
66         () => WA.loadFavorites
67     ),
68
69     handle(
70         R.matchGroupDetailsRoute,
71         ({ id }) => WA.loadGroupDetailsPanel(id)
72     ),
73
74     handle(
75         R.matchGroupsRoute,
76         () => WA.loadGroupsPanel
77     ),
78
79     handle(
80         R.matchKeepServicesRoute,
81         () => WA.loadKeepServices
82     ),
83
84     handle(
85         R.matchLinksRoute,
86         () => WA.loadLinks
87     ),
88
89     handle(
90         R.matchMyAccountRoute,
91         () => WA.loadMyAccount
92     ),
93
94     handle(
95         R.matchProcessLogRoute,
96         ({ id }) => WA.loadProcessLog(id)
97     ),
98
99     handle(
100         R.matchProcessRoute,
101         ({ id }) => WA.loadProcess(id)
102     ),
103
104     handle(
105         R.matchProjectRoute,
106         ({ id }) => WA.loadProject(id)
107     ),
108
109     handle(
110         R.matchRepositoriesRoute,
111         () => WA.loadRepositories
112     ),
113
114     handle(
115         R.matchRootRoute,
116         () => navigateToRootProject
117     ),
118
119     handle(
120         R.matchRunProcessRoute,
121         () => WA.loadRunProcess
122     ),
123
124     handle(
125         R.matchSearchResultsRoute,
126         () => WA.loadSearchResults
127     ),
128
129     handle(
130         R.matchSharedWithMeRoute,
131         () => WA.loadSharedWithMe
132     ),
133
134     handle(
135         R.matchSiteManagerRoute,
136         () => WA.loadSiteManager
137     ),
138
139     handle(
140         R.matchSshKeysAdminRoute,
141         () => WA.loadSshKeys
142     ),
143
144     handle(
145         R.matchSshKeysUserRoute,
146         () => WA.loadSshKeys
147     ),
148
149     handle(
150         R.matchTrashRoute,
151         () => WA.loadTrash
152     ),
153
154     handle(
155         R.matchUsersRoute,
156         () => WA.loadUsers
157     ),
158
159     handle(
160         R.matchAdminVirtualMachineRoute,
161         () => WA.loadVirtualMachines
162     ),
163
164     handle(
165         R.matchUserVirtualMachineRoute,
166         () => WA.loadVirtualMachines
167     ),
168
169     handle(
170         R.matchWorkflowRoute,
171         () => WA.loadWorkflow
172     ),
173
174 ];
175