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 { 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";
17 const getSearchBarTreeNode = (id: string) => (treePicker: TreePicker) => {
18 const searchTree = getTreePicker(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)(treePicker);
20 ? getNode(id)(searchTree)
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));
33 export const getSearchBarTreeNodeAncestorsIds = (id: string) => (treePicker: TreePicker) => {
34 const searchTree = getTreePicker(SEARCH_BAR_ADVANCE_FORM_PICKER_ID)(treePicker);
36 ? getNodeAncestorsIds(id)(searchTree)
40 export const activateSearchBarTreeBranch = (id: string) =>
41 async (dispatch: Dispatch, _: void, services: ServiceRepository) => {
42 const ancestors = await services.ancestorsService.ancestors(id, services.authService.getUuid() || '');
44 for (const ancestor of ancestors) {
45 await dispatch<any>(loadSearchBarTreeProjects(ancestor.uuid));
47 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
50 ...ancestors.map(ancestor => ancestor.uuid)
52 pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID
54 dispatch(treePickerActions.ACTIVATE_TREE_PICKER_NODE({ id, pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID }));
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 }));
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));
74 dispatch(treePickerActions.EXPAND_TREE_PICKER_NODES({
75 ids: getSearchBarTreeNodeAncestorsIds(id)(treePicker),
76 pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID
78 dispatch<any>(expandSearchBarTreeItem(id));
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 }));
86 filters: new FilterBuilder()
87 .addEqual('ownerUuid', projectUuid)
89 order: new OrderBuilder<ProjectResource>()
93 const { items } = await services.projectService.list(params);
94 dispatch(treePickerActions.LOAD_TREE_PICKER_NODE_SUCCESS({
96 pickerId: SEARCH_BAR_ADVANCE_FORM_PICKER_ID,
97 nodes: items.map(item => initTreeNode({ id: item.uuid, value: item })),
99 dispatch(resourcesActions.SET_RESOURCES(items));