Merge branch 'master'
[arvados.git] / src / store / side-panel-tree / side-panel-tree-actions.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 { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
7 import { RootState } from '../store';
8 import { ServiceRepository } from '~/services/services';
9 import { FilterBuilder } from '~/services/api/filter-builder';
10 import { resourcesActions } from '../resources/resources-actions';
11 import { getTreePicker, TreePicker } from '../tree-picker/tree-picker';
12 import { getNodeAncestors, getNodeAncestorsIds, getNode, TreeNode, initTreeNode, TreeNodeStatus } from '~/models/tree';
13 import { ProjectResource } from '~/models/project';
14 import { OrderBuilder } from '../../services/api/order-builder';
15
16 export enum SidePanelTreeCategory {
17     PROJECTS = 'Projects',
18     SHARED_WITH_ME = 'Shared with me',
19     WORKFLOWS = 'Workflows',
20     RECENT_OPEN = 'Recently open',
21     FAVORITES = 'Favorites',
22     TRASH = 'Trash'
23 }
24
25 export const SIDE_PANEL_TREE = 'sidePanelTree';
26
27 export const getSidePanelTree = (treePicker: TreePicker) =>
28     getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
29
30 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
31     const tree = getSidePanelTree(treePicker);
32     if (tree) {
33         const ancestors = getNodeAncestors(uuid)(tree);
34         const node = getNode(uuid)(tree);
35         if (node) {
36             return [...ancestors, node];
37         }
38     }
39     return [];
40 };
41
42 const SIDE_PANEL_CATEGORIES = [
43     SidePanelTreeCategory.SHARED_WITH_ME,
44     SidePanelTreeCategory.WORKFLOWS,
45     SidePanelTreeCategory.RECENT_OPEN,
46     SidePanelTreeCategory.FAVORITES,
47     SidePanelTreeCategory.TRASH,
48 ];
49
50 export const isSidePanelTreeCategory = (id: string) => SIDE_PANEL_CATEGORIES.some(category => category === id);
51
52 export const initSidePanelTree = () =>
53     (dispatch: Dispatch, getState: () => RootState, { authService }: ServiceRepository) => {
54         const rootProjectUuid = authService.getUuid() || '';
55         const nodes = SIDE_PANEL_CATEGORIES.map(id => initTreeNode({ id, value: id }));
56         const projectsNode = initTreeNode({ id: rootProjectUuid, value: SidePanelTreeCategory.PROJECTS });
57         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
58             id: '',
59             pickerId: SIDE_PANEL_TREE,
60             nodes: [projectsNode, ...nodes]
61         }));
62         SIDE_PANEL_CATEGORIES.forEach(category => {
63             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
64                 id: category,
65                 pickerId: SIDE_PANEL_TREE,
66                 nodes: []
67             }));
68         });
69     };
70
71 export const loadSidePanelTreeProjects = (projectUuid: string) =>
72     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
73         const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
74         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
75         if (node || projectUuid === '') {
76             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
77             const params = {
78                 filters: new FilterBuilder()
79                     .addEqual('ownerUuid', projectUuid)
80                     .getFilters(),
81                 order: new OrderBuilder<ProjectResource>()
82                     .addAsc('name')
83                     .getOrder()
84             };
85             const { items } = await services.projectService.list(params);
86             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
87                 id: projectUuid,
88                 pickerId: SIDE_PANEL_TREE,
89                 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
90             }));
91             dispatch(resourcesActions.SET_RESOURCES(items));
92         }
93     };
94
95 export const activateSidePanelTreeItem = (id: string) =>
96     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
97         const node = getSidePanelTreeNode(id)(getState().treePicker);
98         if (node && !node.active) {
99             dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
100         }
101         if (!isSidePanelTreeCategory(id)) {
102             await dispatch<any>(activateSidePanelTreeProject(id));
103         }
104     };
105
106 export const activateSidePanelTreeProject = (id: string) =>
107     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
108         const { treePicker } = getState();
109         const node = getSidePanelTreeNode(id)(treePicker);
110         if (node && node.status !== TreeNodeStatus.LOADED) {
111             await dispatch<any>(loadSidePanelTreeProjects(id));
112         } else if (node === undefined) {
113             await dispatch<any>(activateSidePanelTreeBranch(id));
114         }
115         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
116             ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
117             pickerId: SIDE_PANEL_TREE
118         }));
119         dispatch<any>(expandSidePanelTreeItem(id));
120     };
121
122 export const activateSidePanelTreeBranch = (id: string) =>
123     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
124         const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
125         for (const ancestor of ancestors) {
126             await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
127         }
128         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
129             ids: ancestors.map(ancestor => ancestor.uuid),
130             pickerId: SIDE_PANEL_TREE
131         }));
132         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
133     };
134
135 export const toggleSidePanelTreeItemCollapse = (id: string) =>
136     async (dispatch: Dispatch, getState: () => RootState) => {
137         const node = getSidePanelTreeNode(id)(getState().treePicker);
138         if (node && node.status === TreeNodeStatus.INITIAL) {
139             await dispatch<any>(loadSidePanelTreeProjects(node.id));
140         }
141         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
142     };
143
144 export const expandSidePanelTreeItem = (id: string) =>
145     async (dispatch: Dispatch, getState: () => RootState) => {
146         const node = getSidePanelTreeNode(id)(getState().treePicker);
147         if (node && !node.expanded) {
148             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
149         }
150     };
151
152 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
153     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
154     return sidePanelTree
155         ? getNode(id)(sidePanelTree)
156         : undefined;
157 };
158
159 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
160     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
161     return sidePanelTree
162         ? getNodeAncestorsIds(id)(sidePanelTree)
163         : [];
164 };