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, 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_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) => {
70 const { treePicker } = getState();
71 const node = getSearchBarTreeNode(id)(treePicker);
72 if (node && node.status !== TreeNodeStatus.LOADED) {
73 await dispatch<any>(loadSearchBarTreeProjects(id));
74 } else if (node === undefined) {
75 await dispatch<any>(activateSearchBarTreeBranch(id));
77 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
78 ids: getSearchBarTreeNodeAncestorsIds(id)(treePicker),
79 pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID
81 dispatch<any>(expandSearchBarTreeItem(id));
85 const loadSearchBarProject = (projectUuid: string) =>
86 async (dispatch: Dispatch, _: () => RootState, services: ServiceRepository) => {
87 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE({ id: projectUuid, pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID }));
89 filters: new FilterBuilder()
90 .addEqual('owner_uuid', projectUuid)
92 order: new OrderBuilder<ProjectResource>()
96 const { items } = await services.projectService.list(params);
97 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
99 pickerId: SEARCH_BAR_ADVANCED_FORM_PICKER_ID,
100 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
102 dispatch(resourcesActions.SET_RESOURCES(items));