d440f19009982b7daee59692d844ca65d6900c8a
[arvados-workbench2.git] / src / store / navigation / navigation-action.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Dispatch } from "redux";
6 import { getProjectList, projectActions } from "../project/project-action";
7 import { push } from "react-router-redux";
8 import { TreeItemStatus } from "~/components/tree/tree";
9 import { findTreeItem } from "../project/project-reducer";
10 import { RootState } from "../store";
11 import { ResourceKind, Resource } from '~/models/resource';
12 import { projectPanelActions } from "../project-panel/project-panel-action";
13 import { getCollectionUrl } from "~/models/collection";
14 import { getProjectUrl, ProjectResource } from "~/models/project";
15 import { ProjectService } from "~/services/project-service/project-service";
16 import { ServiceRepository } from "~/services/services";
17 import { sidePanelActions } from "../side-panel/side-panel-action";
18 import { SidePanelId } from "../side-panel/side-panel-reducer";
19 import { getUuidObjectType, ObjectTypes } from "~/models/object-types";
20 import { getResource } from '~/store/resources/resources';
21 import { loadDetailsPanel } from '~/store/details-panel/details-panel-action';
22 import { loadCollection } from '~/store/collection-panel/collection-panel-action';
23 import { GroupContentsResource } from "~/services/groups-service/groups-service";
24 import { snackbarActions } from '../snackbar/snackbar-actions';
25 import { resourceLabel } from "~/common/labels";
26
27 export const getResourceUrl = (resourceKind: ResourceKind, resourceUuid: string): string => {
28     switch (resourceKind) {
29         case ResourceKind.PROJECT: return getProjectUrl(resourceUuid);
30         case ResourceKind.COLLECTION: return getCollectionUrl(resourceUuid);
31         default:
32             return '';
33     }
34 };
35
36 export enum ItemMode {
37     BOTH,
38     OPEN,
39     ACTIVE
40 }
41
42 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
43     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
44         const { projects, router } = getState();
45         const treeItem = findTreeItem(projects.items, itemId);
46
47         if (treeItem) {
48             const resourceUrl = getResourceUrl(treeItem.data.kind, treeItem.data.uuid);
49
50             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
51                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
52                     dispatch(push(resourceUrl));
53                 }
54                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
55             }
56
57             const promise = treeItem.status === TreeItemStatus.LOADED
58                 ? Promise.resolve()
59                 : dispatch<any>(getProjectList(itemId));
60
61             promise
62                 .then(() => dispatch<any>(() => {
63                     if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
64                         dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
65                     }
66                     dispatch(projectPanelActions.RESET_PAGINATION());
67                     dispatch(projectPanelActions.REQUEST_ITEMS());
68                 }));
69         } else {
70             const uuid = services.authService.getUuid();
71             if (itemId === uuid) {
72                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
73                 dispatch(projectPanelActions.RESET_PAGINATION());
74                 dispatch(projectPanelActions.REQUEST_ITEMS());
75             }
76         }
77     };
78
79 export const restoreBranch = (itemId: string) =>
80     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
81         const ancestors = await loadProjectAncestors(itemId, services.projectService);
82         const uuids = ancestors.map(ancestor => ancestor.uuid);
83         await loadBranch(uuids, dispatch);
84         dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelId.PROJECTS));
85         uuids.forEach(uuid => {
86             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
87         });
88     };
89
90 export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
91     if (getUuidObjectType(uuid) === ObjectTypes.USER) {
92         return [];
93     } else {
94         const currentProject = await projectService.get(uuid);
95         const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
96         return [...ancestors, currentProject];
97     }
98 };
99
100 const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
101     const [uuid, ...rest] = uuids;
102     if (uuid) {
103         await dispatch<any>(getProjectList(uuid));
104         return loadBranch(rest, dispatch);
105     }
106 };
107
108 export const navigateToResource = (uuid: string) =>
109     (dispatch: Dispatch, getState: () => RootState) => {
110         const resource = getResource(uuid)(getState().resources);
111         resource
112             ? dispatch<any>(getResourceNavigationAction(resource))
113             : dispatch<any>(resourceIsNotLoaded(uuid));
114     };
115
116 const getResourceNavigationAction = (resource: Resource) => {
117     switch (resource.kind) {
118         case ResourceKind.COLLECTION:
119             return navigateToCollection(resource);
120         case ResourceKind.PROJECT:
121             return navigateToProject(resource);
122         default:
123             return cannotNavigateToResource(resource);
124     }
125 };
126
127 export const navigateToProject = ({ uuid }: Resource) =>
128     (dispatch: Dispatch) => {
129         dispatch<any>(setProjectItem(uuid, ItemMode.BOTH));
130         dispatch(loadDetailsPanel(uuid));
131     };
132
133 export const navigateToCollection = ({ uuid }: Resource) =>
134     (dispatch: Dispatch) => {
135         dispatch<any>(loadCollection(uuid));
136         dispatch(push(getCollectionUrl(uuid)));
137     };
138
139 export const cannotNavigateToResource = ({ kind, uuid }: Resource) =>
140     snackbarActions.OPEN_SNACKBAR({
141         message: `${resourceLabel(kind)} identified by ${uuid} cannot be opened.`,
142         hideDuration: 3000
143     });
144
145
146 export const resourceIsNotLoaded = (uuid: string) =>
147     snackbarActions.OPEN_SNACKBAR({
148         message: `Resource identified by ${uuid} is not loaded.`,
149         hideDuration: 3000
150     });