15803: use getUserUuid instead of getting from local store
[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, TreeNodeStatus } 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_ADVANCE_FORM_PICKER_ID } from "~/store/search-bar/search-bar-actions";
17
18 const getSearchBarTreeNode = (id: string) => (treePicker: TreePicker) => {
19     const searchTree = getTreePicker(SEARCH_BAR_ADVANCE_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_ADVANCE_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_ADVANCE_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) {
45             return;
46         }
47         const ancestors = await services.ancestorsService.ancestors(id, userUuid);
48
49         for (const ancestor of ancestors) {
50             await dispatch<any>(loadSearchBarTreeProjects(ancestor.uuid));
51         }
52         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
53             ids: [
54                 ...[],
55                 ...ancestors.map(ancestor => ancestor.uuid)
56             ],
57             pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID
58         }));
59         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
60     };
61
62 export const expandSearchBarTreeItem = (id: string) =>
63     async (dispatch: Dispatch, getState: () => RootState) => {
64         const node = getSearchBarTreeNode(id)(getState().treePicker);
65         if (node && !node.expanded) {
66             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
67         }
68     };
69
70 export const activateSearchBarProject = (id: string) =>
71     async (dispatch: Dispatch, getState: () => RootState) => {
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_ADVANCE_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_ADVANCE_FORM_PICKER_ID }));
90         const params = {
91             filters: new FilterBuilder()
92                 .addEqual('ownerUuid', 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_ADVANCE_FORM_PICKER_ID,
102             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
103         }));
104         dispatch(resourcesActions.SET_RESOURCES(items));
105     };