refs #master Merge branch 'origin/master' into 14007-ts-paths
[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 { projectActions, getProjectList } 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 { Resource, ResourceKind } 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 { SidePanelIdentifiers } from "../side-panel/side-panel-reducer";
19 import { getUuidObjectType, ObjectTypes } from "~/models/object-types";
20
21 export const getResourceUrl = <T extends Resource>(resource: T): string => {
22     switch (resource.kind) {
23         case ResourceKind.PROJECT: return getProjectUrl(resource.uuid);
24         case ResourceKind.COLLECTION: return getCollectionUrl(resource.uuid);
25         default: return resource.href;
26     }
27 };
28
29 export enum ItemMode {
30     BOTH,
31     OPEN,
32     ACTIVE
33 }
34
35 export const setProjectItem = (itemId: string, itemMode: ItemMode) =>
36     (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
37         const { projects, router } = getState();
38         const treeItem = findTreeItem(projects.items, itemId);
39
40         if (treeItem) {
41             const resourceUrl = getResourceUrl(treeItem.data);
42
43             if (itemMode === ItemMode.ACTIVE || itemMode === ItemMode.BOTH) {
44                 if (router.location && !router.location.pathname.includes(resourceUrl)) {
45                     dispatch(push(resourceUrl));
46                 }
47                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(treeItem.data.uuid));
48             }
49
50             const promise = treeItem.status === TreeItemStatus.LOADED
51                 ? Promise.resolve()
52                 : dispatch<any>(getProjectList(itemId));
53
54             promise
55                 .then(() => dispatch<any>(() => {
56                     if (itemMode === ItemMode.OPEN || itemMode === ItemMode.BOTH) {
57                         dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(treeItem.data.uuid));
58                     }
59                     dispatch(projectPanelActions.RESET_PAGINATION());
60                     dispatch(projectPanelActions.REQUEST_ITEMS());
61                 }));
62         } else {
63             const uuid = services.authService.getUuid();
64             if (itemId === uuid) {
65                 dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_ACTIVE(uuid));
66                 dispatch(projectPanelActions.RESET_PAGINATION());
67                 dispatch(projectPanelActions.REQUEST_ITEMS());
68             }
69         }
70     };
71
72 export const restoreBranch = (itemId: string) =>
73     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74         const ancestors = await loadProjectAncestors(itemId, services.projectService);
75         const uuids = ancestors.map(ancestor => ancestor.uuid);
76         await loadBranch(uuids, dispatch);
77         dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_OPEN(SidePanelIdentifiers.PROJECTS));
78         dispatch(sidePanelActions.TOGGLE_SIDE_PANEL_ITEM_ACTIVE(SidePanelIdentifiers.PROJECTS));
79         uuids.forEach(uuid => {
80             dispatch(projectActions.TOGGLE_PROJECT_TREE_ITEM_OPEN(uuid));
81         });
82     };
83
84 export const loadProjectAncestors = async (uuid: string, projectService: ProjectService): Promise<Array<ProjectResource>> => {
85     if (getUuidObjectType(uuid) === ObjectTypes.USER) {
86         return [];
87     } else {
88         const currentProject = await projectService.get(uuid);
89         const ancestors = await loadProjectAncestors(currentProject.ownerUuid, projectService);
90         return [...ancestors, currentProject];
91     }
92 };
93
94 const loadBranch = async (uuids: string[], dispatch: Dispatch): Promise<any> => {
95     const [uuid, ...rest] = uuids;
96     if (uuid) {
97         await dispatch<any>(getProjectList(uuid));
98         return loadBranch(rest, dispatch);
99     }
100 };