fixed conflicts
[arvados-workbench2.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/store';
8 import { ServiceRepository } from '~/services/services';
9 import { FilterBuilder } from '~/services/api/filter-builder';
10 import { resourcesActions } from '~/store/resources/resources-actions';
11 import { getTreePicker, TreePicker } from '~/store/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 import { ResourceKind } from '~/models/resource';
16 import { GroupContentsResourcePrefix } from '~/services/groups-service/groups-service';
17 import { GroupClass } from '~/models/group';
18
19 export enum SidePanelTreeCategory {
20     PROJECTS = 'Projects',
21     SHARED_WITH_ME = 'Shared with me',
22     WORKFLOWS = 'Workflows',
23     FAVORITES = 'Favorites',
24     TRASH = 'Trash'
25 }
26
27 export const SIDE_PANEL_TREE = 'sidePanelTree';
28
29 export const getSidePanelTree = (treePicker: TreePicker) =>
30     getTreePicker<ProjectResource | string>(SIDE_PANEL_TREE)(treePicker);
31
32 export const getSidePanelTreeBranch = (uuid: string) => (treePicker: TreePicker): Array<TreeNode<ProjectResource | string>> => {
33     const tree = getSidePanelTree(treePicker);
34     if (tree) {
35         const ancestors = getNodeAncestors(uuid)(tree);
36         const node = getNode(uuid)(tree);
37         if (node) {
38             return [...ancestors, node];
39         }
40     }
41     return [];
42 };
43
44 const SIDE_PANEL_CATEGORIES = [
45     SidePanelTreeCategory.WORKFLOWS,
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, _: () => 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         const sharedNode = initTreeNode({ id: SidePanelTreeCategory.SHARED_WITH_ME, value: SidePanelTreeCategory.SHARED_WITH_ME });
58         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
59             id: '',
60             pickerId: SIDE_PANEL_TREE,
61             nodes: [projectsNode, sharedNode, ...nodes]
62         }));
63         SIDE_PANEL_CATEGORIES.forEach(category => {
64             dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
65                 id: category,
66                 pickerId: SIDE_PANEL_TREE,
67                 nodes: []
68             }));
69         });
70     };
71
72 export const loadSidePanelTreeProjects = (projectUuid: string) =>
73     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
74         const treePicker = getTreePicker(SIDE_PANEL_TREE)(getState().treePicker);
75         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
76         if (projectUuid === SidePanelTreeCategory.SHARED_WITH_ME) {
77             await dispatch<any>(loadSharedRoot);
78         } else if (node || projectUuid === '') {
79             await dispatch<any>(loadProject(projectUuid));
80         }
81     };
82
83 const loadProject = (projectUuid: string) =>
84     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
85         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SIDE_PANEL_TREE }));
86         const params = {
87             filters: new FilterBuilder()
88                 .addEqual('ownerUuid', projectUuid)
89                 .getFilters(),
90             order: new OrderBuilder<ProjectResource>()
91                 .addAsc('name')
92                 .getOrder()
93         };
94         const { items } = await services.projectService.list(params);
95         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
96             id: projectUuid,
97             pickerId: SIDE_PANEL_TREE,
98             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
99         }));
100         dispatch(resourcesActions.SET_RESOURCES(items));
101     };
102
103 const loadSharedRoot = async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
104     dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: SidePanelTreeCategory.SHARED_WITH_ME, pickerId: SIDE_PANEL_TREE }));
105
106     const params = {
107         filters:  `[${new FilterBuilder()
108             .addIsA('uuid', ResourceKind.PROJECT)
109             .addEqual('groupClass', GroupClass.PROJECT)
110             .getFilters()}]`,
111         order: new OrderBuilder<ProjectResource>()
112             .addAsc('name', GroupContentsResourcePrefix.PROJECT)
113             .getOrder(),
114     };
115
116     const { items } = await services.groupsService.shared(params);
117
118     dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
119         id: SidePanelTreeCategory.SHARED_WITH_ME,
120         pickerId: SIDE_PANEL_TREE,
121         nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
122     }));
123
124     dispatch(resourcesActions.SET_RESOURCES(items));
125 };
126
127 export const activateSidePanelTreeItem = (id: string) =>
128     async (dispatch: Dispatch, getState: () => RootState) => {
129         const node = getSidePanelTreeNode(id)(getState().treePicker);
130         if (node && !node.active) {
131             dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
132         }
133         if (!isSidePanelTreeCategory(id)) {
134             await dispatch<any>(activateSidePanelTreeProject(id));
135         }
136     };
137
138 export const activateSidePanelTreeProject = (id: string) =>
139     async (dispatch: Dispatch, getState: () => RootState) => {
140         const { treePicker } = getState();
141         const node = getSidePanelTreeNode(id)(treePicker);
142         if (node && node.status !== TreeNodeStatus.LOADED) {
143             await dispatch<any>(loadSidePanelTreeProjects(id));
144         } else if (node === undefined) {
145             await dispatch<any>(activateSidePanelTreeBranch(id));
146         }
147         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
148             ids: getSidePanelTreeNodeAncestorsIds(id)(treePicker),
149             pickerId: SIDE_PANEL_TREE
150         }));
151         dispatch<any>(expandSidePanelTreeItem(id));
152     };
153
154 export const activateSidePanelTreeBranch = (id: string) =>
155     async (dispatch: Dispatch, _: void, services: ServiceRepository) => {
156         const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
157         const isShared = ancestors.every(({ uuid }) => uuid !== services.authService.getUuid());
158         if (isShared) {
159             await dispatch<any>(loadSidePanelTreeProjects(SidePanelTreeCategory.SHARED_WITH_ME));
160         }
161         for (const ancestor of ancestors) {
162             await dispatch<any>(loadSidePanelTreeProjects(ancestor.uuid));
163         }
164         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
165             ids: [
166                 ...(isShared ? [SidePanelTreeCategory.SHARED_WITH_ME] : []),
167                 ...ancestors.map(ancestor => ancestor.uuid)
168             ],
169             pickerId: SIDE_PANEL_TREE
170         }));
171         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SIDE_PANEL_TREE }));
172     };
173
174 export const toggleSidePanelTreeItemCollapse = (id: string) =>
175     async (dispatch: Dispatch, getState: () => RootState) => {
176         const node = getSidePanelTreeNode(id)(getState().treePicker);
177         if (node && node.status === TreeNodeStatus.INITIAL) {
178             await dispatch<any>(loadSidePanelTreeProjects(node.id));
179         }
180         dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
181     };
182
183 export const expandSidePanelTreeItem = (id: string) =>
184     async (dispatch: Dispatch, getState: () => RootState) => {
185         const node = getSidePanelTreeNode(id)(getState().treePicker);
186         if (node && !node.expanded) {
187             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SIDE_PANEL_TREE }));
188         }
189     };
190
191 export const getSidePanelTreeNode = (id: string) => (treePicker: TreePicker) => {
192     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
193     return sidePanelTree
194         ? getNode(id)(sidePanelTree)
195         : undefined;
196 };
197
198 export const getSidePanelTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
199     const sidePanelTree = getTreePicker(SIDE_PANEL_TREE)(treePicker);
200     return sidePanelTree
201         ? getNodeAncestorsIds(id)(sidePanelTree)
202         : [];
203 };