Use fullTextSearch filter only if search query is not empty
[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 { ServiceRepository } from "~/services/services";
10 import { treePickerActions } from "~/store/tree-picker/tree-picker-actions";
11 import { FilterBuilder } from "~/services/api/filter-builder";
12 import { OrderBuilder } from "~/services/api/order-builder";
13 import { ProjectResource } from "~/models/project";
14 import { resourcesActions } from "~/store/resources/resources-actions";
15 import { SEARCH_BAR_ADVANCE_FORM_PICKER_ID } from "~/store/search-bar/search-bar-actions";
16
17 const getSearchBarTreeNode = (id: string) => (treePicker: TreePicker) => {
18     const searchTree = getTreePicker(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)(treePicker);
19     return searchTree
20         ? getNode(id)(searchTree)
21         : undefined;
22 };
23
24 export const loadSearchBarTreeProjects = (projectUuid: string) =>
25     async (dispatch: Dispatch, getState: () => RootState) => {
26         const treePicker = getTreePicker(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)(getState().treePicker);
27         const node = treePicker ? getNode(projectUuid)(treePicker) : undefined;
28         if (node || projectUuid === '') {
29             await dispatch<any>(loadSearchBarProject(projectUuid));
30         }
31     };
32
33 export const getSearchBarTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
34     const searchTree = getTreePicker(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)(treePicker);
35     return searchTree
36         ? getNodeAncestorsIds(id)(searchTree)
37         : [];
38 };
39
40 export const activateSearchBarTreeBranch = (id: string) =>
41     async (dispatch: Dispatch, _: void, services: ServiceRepository) => {
42         const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
43
44         for (const ancestor of ancestors) {
45             await dispatch<any>(loadSearchBarTreeProjects(ancestor.uuid));
46         }
47         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
48             ids: [
49                 ...[],
50                 ...ancestors.map(ancestor => ancestor.uuid)
51             ],
52             pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID
53         }));
54         dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
55     };
56
57 export const expandSearchBarTreeItem = (id: string) =>
58     async (dispatch: Dispatch, getState: () => RootState) => {
59         const node = getSearchBarTreeNode(id)(getState().treePicker);
60         if (node && !node.expanded) {
61             dispatch(treePickerActions.TOGGLE_TREE_PICKER_NODE_COLLAPSE({ id, pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
62         }
63     };
64
65 export const activateSearchBarProject = (id: string) =>
66     async (dispatch: Dispatch, getState: () => RootState) => {
67         const { treePicker } = getState();
68         const node = getSearchBarTreeNode(id)(treePicker);
69         if (node && node.status !== TreeNodeStatus.LOADED) {
70             await dispatch<any>(loadSearchBarTreeProjects(id));
71         } else if (node === undefined) {
72             await dispatch<any>(activateSearchBarTreeBranch(id));
73         }
74         dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
75             ids: getSearchBarTreeNodeAncestorsIds(id)(treePicker),
76             pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID
77         }));
78         dispatch<any>(expandSearchBarTreeItem(id));
79     };
80
81
82 const loadSearchBarProject = (projectUuid: string) =>
83     async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
84         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
85         const params = {
86             filters: new FilterBuilder()
87                 .addEqual('ownerUuid', projectUuid)
88                 .getFilters(),
89             order: new OrderBuilder<ProjectResource>()
90                 .addAsc('name')
91                 .getOrder()
92         };
93         const { items } = await services.projectService.list(params);
94         dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
95             id: projectUuid,
96             pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID,
97             nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
98         }));
99         dispatch(resourcesActions.SET_RESOURCES(items));
100     };
101