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