Post merge fixes
[arvados-workbench2.git] / src / routes / routes.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 { matchPath } from 'react-router';
8 import { ResourceKind, RESOURCE_UUID_PATTERN, extractUuidKind } from '~/models/resource';
9 import { getProjectUrl } from '~/models/project';
10 import { getCollectionUrl } from '~/models/collection';
11 import { loadProject, loadFavorites, loadCollection, loadTrash } from '~/store/workbench/workbench-actions';
12 import { loadProcess } from '~/store/processes/processes-actions';
13
14 export const Routes = {
15     ROOT: '/',
16     TOKEN: '/token',
17     PROJECTS: `/projects/:id(${RESOURCE_UUID_PATTERN})`,
18     COLLECTIONS: `/collections/:id(${RESOURCE_UUID_PATTERN})`,
19     PROCESSES: `/processes/:id(${RESOURCE_UUID_PATTERN})`,
20     FAVORITES: '/favorites',
21     TRASH: '/trash'
22 };
23
24 export const getResourceUrl = (uuid: string) => {
25     const kind = extractUuidKind(uuid);
26     switch (kind) {
27         case ResourceKind.PROJECT:
28             return getProjectUrl(uuid);
29         case ResourceKind.COLLECTION:
30             return getCollectionUrl(uuid);
31         default:
32             return undefined;
33     }
34 };
35
36 export const getProcessUrl = (uuid: string) => `/processes/${uuid}`;
37
38 export const addRouteChangeHandlers = (history: History, store: RootStore) => {
39     const handler = handleLocationChange(store);
40     handler(history.location);
41     history.listen(handler);
42 };
43
44 export interface ResourceRouteParams {
45     id: string;
46 }
47
48 export const matchRootRoute = (route: string) =>
49     matchPath(route, { path: Routes.ROOT, exact: true });
50
51 export const matchFavoritesRoute = (route: string) =>
52     matchPath(route, { path: Routes.FAVORITES });
53
54 export const matchTrashRoute = (route: string) =>
55     matchPath(route, { path: Routes.TRASH });
56
57 export const matchProjectRoute = (route: string) =>
58     matchPath<ResourceRouteParams>(route, { path: Routes.PROJECTS });
59
60 export const matchCollectionRoute = (route: string) =>
61     matchPath<ResourceRouteParams>(route, { path: Routes.COLLECTIONS });
62
63 export const matchProcessRoute = (route: string) =>
64     matchPath<ResourceRouteParams>(route, { path: Routes.PROCESSES });
65
66
67 const handleLocationChange = (store: RootStore) => ({ pathname }: Location) => {
68     const projectMatch = matchProjectRoute(pathname);
69     const collectionMatch = matchCollectionRoute(pathname);
70     const favoriteMatch = matchFavoritesRoute(pathname);
71     const trashMatch = matchTrashRoute(pathname);
72     const processMatch = matchProcessRoute(pathname);
73     if (projectMatch) {
74         store.dispatch(loadProject(projectMatch.params.id));
75     } else if (collectionMatch) {
76         store.dispatch(loadCollection(collectionMatch.params.id));
77     } else if (favoriteMatch) {
78         store.dispatch(loadFavorites());
79     } else if (trashMatch) {
80         store.dispatch(loadTrash());
81     } else if (processMatch) {
82         store.dispatch(loadProcess(processMatch.params.id));
83     }
84 };