Merge branch 'master'
[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 { matchProcessRoute, matchProcessLogRoute, matchProjectRoute, matchCollectionRoute, matchFavoritesRoute, matchTrashRoute, matchRootRoute, matchSharedWithMeRoute } from './routes';
8 import { loadProject, loadCollection, loadFavorites, loadTrash, loadProcess, loadProcessLog } from '~/store/workbench/workbench-actions';
9 import { navigateToRootProject } from '~/store/navigation/navigation-action';
10 import { navigateToSharedWithMe } from '../store/navigation/navigation-action';
11 import { loadSharedWithMe } from '../store/workbench/workbench-actions';
12
13 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
14     const handler = handleLocationChange(store);
15     handler(history.location);
16     history.listen(handler);
17 };
18
19 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
20     const rootMatch = matchRootRoute(pathname);
21     const projectMatch = matchProjectRoute(pathname);
22     const collectionMatch = matchCollectionRoute(pathname);
23     const favoriteMatch = matchFavoritesRoute(pathname);
24     const trashMatch = matchTrashRoute(pathname);
25     const processMatch = matchProcessRoute(pathname);
26     const processLogMatch = matchProcessLogRoute(pathname);
27     const sharedWithMeMatch = matchSharedWithMeRoute(pathname);
28
29     if (projectMatch) {
30         store.dispatch(loadProject(projectMatch.params.id));
31     } else if (collectionMatch) {
32         store.dispatch(loadCollection(collectionMatch.params.id));
33     } else if (favoriteMatch) {
34         store.dispatch(loadFavorites());
35     } else if (trashMatch) {
36         store.dispatch(loadTrash());
37     } else if (processMatch) {
38         store.dispatch(loadProcess(processMatch.params.id));
39     } else if (processLogMatch) {
40         store.dispatch(loadProcessLog(processLogMatch.params.id));
41     } else if (rootMatch) {
42         store.dispatch(navigateToRootProject);
43     } else if (sharedWithMeMatch) {
44         store.dispatch(loadSharedWithMe);
45     }
46 };