Replace multiple if blocks with more convenient mechanism
[arvados-workbench2.git] / src / routes / route-change-handlers.ts
index 33e0bef753c6f5535a18a02e94b7e8e3012052ab..4118415acbb7295bd6e29f3e5e9516835ca15d78 100644 (file)
@@ -4,11 +4,14 @@
 
 import { History, Location } from 'history';
 import { RootStore } from '~/store/store';
-import { matchProcessRoute, matchProcessLogRoute, matchProjectRoute, matchCollectionRoute, matchFavoritesRoute, matchTrashRoute, matchRootRoute, matchSharedWithMeRoute } from './routes';
-import { loadProject, loadCollection, loadFavorites, loadTrash, loadProcess, loadProcessLog } from '~/store/workbench/workbench-actions';
+import * as R from '~/routes/routes';
+import * as WA from '~/store/workbench/workbench-actions';
 import { navigateToRootProject } from '~/store/navigation/navigation-action';
-import { navigateToSharedWithMe } from '../store/navigation/navigation-action';
-import { loadSharedWithMe } from '../store/workbench/workbench-actions';
+import { dialogActions } from '~/store/dialog/dialog-actions';
+import { contextMenuActions } from '~/store/context-menu/context-menu-actions';
+import { searchBarActions } from '~/store/search-bar/search-bar-actions';
+import { match } from 'react-router-dom';
+import { Dispatch } from 'redux';
 
 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
     const handler = handleLocationChange(store);
@@ -17,30 +20,155 @@ export const addRouteChangeHandlers = (history: History, store: RootStore) => {
 };
 
 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
-    const rootMatch = matchRootRoute(pathname);
-    const projectMatch = matchProjectRoute(pathname);
-    const collectionMatch = matchCollectionRoute(pathname);
-    const favoriteMatch = matchFavoritesRoute(pathname);
-    const trashMatch = matchTrashRoute(pathname);
-    const processMatch = matchProcessRoute(pathname);
-    const processLogMatch = matchProcessLogRoute(pathname);
-    const sharedWithMeMatch = matchSharedWithMeRoute(pathname);
-
-    if (projectMatch) {
-        store.dispatch(loadProject(projectMatch.params.id));
-    } else if (collectionMatch) {
-        store.dispatch(loadCollection(collectionMatch.params.id));
-    } else if (favoriteMatch) {
-        store.dispatch(loadFavorites());
-    } else if (trashMatch) {
-        store.dispatch(loadTrash());
-    } else if (processMatch) {
-        store.dispatch(loadProcess(processMatch.params.id));
-    } else if (processLogMatch) {
-        store.dispatch(loadProcessLog(processLogMatch.params.id));
-    } else if (rootMatch) {
-        store.dispatch(navigateToRootProject);
-    } else if (sharedWithMeMatch) {
-        store.dispatch(loadSharedWithMe);
-    }
+
+    store.dispatch(dialogActions.CLOSE_ALL_DIALOGS());
+    store.dispatch(contextMenuActions.CLOSE_CONTEXT_MENU());
+    store.dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
+
+    locationChangeHandlers.find(handler => handler(store.dispatch, pathname));
+
 };
+
+type MatchRoute<Params> = (route: string) => match<Params> | null;
+type ActionCreator<Params> = (params: Params) => void;
+
+const handle = <Params>(matchRoute: MatchRoute<Params>, actionCreator: ActionCreator<Params>) =>
+    (dispatch: Dispatch, route: string) => {
+        const match = matchRoute(route);
+        return match
+            ? (
+                dispatch<any>(actionCreator(match.params)),
+                true
+            )
+            : false;
+    };
+
+const locationChangeHandlers = [
+
+    handle(
+        R.matchApiClientAuthorizationsRoute,
+        () => WA.loadApiClientAuthorizations
+    ),
+
+    handle(
+        R.matchCollectionRoute,
+        ({ id }) => WA.loadCollection(id)
+    ),
+
+    handle(
+        R.matchComputeNodesRoute,
+        () => WA.loadComputeNodes
+    ),
+
+    handle(
+        R.matchFavoritesRoute,
+        () => WA.loadFavorites
+    ),
+
+    handle(
+        R.matchGroupDetailsRoute,
+        ({ id }) => WA.loadGroupDetailsPanel(id)
+    ),
+
+    handle(
+        R.matchGroupsRoute,
+        () => WA.loadGroupsPanel
+    ),
+
+    handle(
+        R.matchKeepServicesRoute,
+        () => WA.loadKeepServices
+    ),
+
+    handle(
+        R.matchLinksRoute,
+        () => WA.loadLinks
+    ),
+
+    handle(
+        R.matchMyAccountRoute,
+        () => WA.loadMyAccount
+    ),
+
+    handle(
+        R.matchProcessLogRoute,
+        ({ id }) => WA.loadProcessLog(id)
+    ),
+
+    handle(
+        R.matchProcessRoute,
+        ({ id }) => WA.loadProcess(id)
+    ),
+
+    handle(
+        R.matchProjectRoute,
+        ({ id }) => WA.loadProject(id)
+    ),
+
+    handle(
+        R.matchRepositoriesRoute,
+        () => WA.loadRepositories
+    ),
+
+    handle(
+        R.matchRootRoute,
+        () => navigateToRootProject
+    ),
+
+    handle(
+        R.matchRunProcessRoute,
+        () => WA.loadRunProcess
+    ),
+
+    handle(
+        R.matchSearchResultsRoute,
+        () => WA.loadSearchResults
+    ),
+
+    handle(
+        R.matchSharedWithMeRoute,
+        () => WA.loadSharedWithMe
+    ),
+
+    handle(
+        R.matchSiteManagerRoute,
+        () => WA.loadSiteManager
+    ),
+
+    handle(
+        R.matchSshKeysAdminRoute,
+        () => WA.loadSshKeys
+    ),
+
+    handle(
+        R.matchSshKeysUserRoute,
+        () => WA.loadSshKeys
+    ),
+
+    handle(
+        R.matchTrashRoute,
+        () => WA.loadTrash
+    ),
+
+    handle(
+        R.matchUsersRoute,
+        () => WA.loadUsers
+    ),
+
+    handle(
+        R.matchAdminVirtualMachineRoute,
+        () => WA.loadVirtualMachines
+    ),
+
+    handle(
+        R.matchUserVirtualMachineRoute,
+        () => WA.loadVirtualMachines
+    ),
+
+    handle(
+        R.matchWorkflowRoute,
+        () => WA.loadWorkflow
+    ),
+
+];
+