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