1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
18 const getSearchBarTreeNode = (id: string) => (treePicker: TreePicker) => {
19 const searchTree = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(treePicker);
21 ? getNode(id)(searchTree)
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));
34 export const getSearchBarTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
35 const searchTree = getTreePicker(SEARCH_BAR_ADVANCED_FORM_PICKER_ID)(treePicker);
37 ? getNodeAncestorsIds(id)(searchTree)
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);
47 for (const ancestor of ancestors) {
48 await dispatch<any>(loadSearchBarTreeProjects(ancestor.uuid));
50 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
53 ...ancestors.map(ancestor => ancestor.uuid)
55 pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
57 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
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 }));
68 export const activateSearchBarProject = (id: string) =>
69 async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
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));
79 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
80 ids: getSearchBarTreeNodeAncestorsIds(id)(treePicker),
81 pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
83 dispatch<any>(expandSearchBarTreeItem(id));*/
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 }));
91 filters: new FilterBuilder()
92 .addEqual('owner_uuid', projectUuid)
94 order: new OrderBuilder<ProjectResource>()
98 const { items } = await services.projectService.list(params);
99 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
101 pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID,
102 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
104 dispatch(resourcesActions.SET_RESOURCES(items));