20377: Reject dataexplorer updates to incorrect page, allows dataexplorer to retry...
[arvados-workbench2.git] / src / store / search-bar / search-bar-tree-actions.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { getTreePicker, TreePicker } from "store/tree-picker/tree-picker";
6 import { getNode, getNodeAncestorsIds, initTreeNode } from "models/tree";
7 import { Dispatch } from "redux";
8 import { RootState } from "store/store";
9 import { getUserUuid } from "common/getuser";
10 import { ServiceRepository } from "services/services";
11 import { treePickerActions } from "store/tree-picker/tree-picker-actions";
12 import { FilterBuilder } from "services/api/filter-builder";
13 import { OrderBuilder } from "services/api/order-builder";
14 import { ProjectResource } from "models/project";
15 import { resourcesActions } from "store/resources/resources-actions";
16 import { SEARCH_BAR_ADVANCED_FORM_PICKER_ID } from "store/search-bar/search-bar-actions";
17
18 const getSearchBarTreeNode = (id: string) => (treePicker: TreePicker) => {
19     const searchTree = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(treePicker);
20     return searchTree
21         ? getNode(id)(searchTree)
22         : undefined;
23 };
24
25 export const loadSearchBarTreeProjects = (projectUuid: string) =>
26     async (dispatch: Dispatch, getState: () => RootState) => {
27         const treePicker = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(getState().treePicker);
28         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
29         if (node || projectUuid === '') {
30             await dispatch<any>(loadSearchBarProject(projectUuid));
31         }
32     };
33
34 export const getSearchBarTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
35     const searchTree = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(treePicker);
36     return searchTree
37         ? getNodeAncestorsIds(id)(searchTree)
38         : [];
39 };
40
41 export const activateSearchBarTreeBranch = (id: string) =>
42     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
43         const userUuid = getUserUuid(getState());
44         if (!userUuid) { return; }
45         const ancestors = await services.ancestorsService.ancestors(id, userUuid);
46
47         for (const ancestor of ancestors) {
48             await dispatch<any>(loadSearchBarTreeProjects(ancestor.uuid));
49         }
50         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
51             ids: [
52                 ...[],
53                 ...ancestors.map(ancestor => ancestor.uuid)
54             ],
55             pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
56         }));
57         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
58     };
59
60 export const expandSearchBarTreeItem = (id: string) =>
61     async (dispatch: Dispatch, getState: () => RootState) => {
62         const node = getSearchBarTreeNode(id)(getState().treePicker);
63         if (node && !node.expanded) {
64             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
65         }
66     };
67
68 export const activateSearchBarProject = (id: string) =>
69     async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
70
71
72         /*const { treePicker } = getState();
73         const node = getSearchBarTreeNode(id)(treePicker);
74         if (node && node.status !== TreeNodeStatus.LOADED) {
75             await dispatch<any>(loadSearchBarTreeProjects(id));
76         } else if (node === undefined) {
77             await dispatch<any>(activateSearchBarTreeBranch(id));
78         }
79         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
80             ids: getSearchBarTreeNodeAncestorsIds(id)(treePicker),
81             pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
82         }));
83         dispatch<any>(expandSearchBarTreeItem(id));*/
84     };
85
86
87 const loadSearchBarProject = (projectUuid: string) =>
88     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
89         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
90         const params = {
91             filters: new FilterBuilder()
92                 .addEqual('owner_uuid', projectUuid)
93                 .getFilters(),
94             order: new OrderBuilder<ProjectResource>()
95                 .addAsc('name')
96                 .getOrder()
97         };
98         const { items } = await services.projectService.list(params);
99         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
100             id: projectUuid,
101             pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID,
102             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
103         }));
104         dispatch(resourcesActions.SET_RESOURCES(items));
105     };