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