1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { Dispatch, MiddlewareAPI } from 'redux';
6 import { RootState } from 'store/store';
7 import { ServiceRepository } from 'services/services';
8 import { Middleware } from "redux";
9 import { getNode, getNodeDescendantsIds, TreeNodeStatus } from 'models/tree';
10 import { getTreePicker } from './tree-picker';
12 treePickerSearchActions, loadProject, loadFavoritesProject, loadPublicFavoritesProject,
13 SHARED_PROJECT_ID, FAVORITES_PROJECT_ID, PUBLIC_FAVORITES_PROJECT_ID, SEARCH_PROJECT_ID
14 } from "./tree-picker-actions";
15 import { LinkResource } from "models/link";
16 import { GroupContentsResource } from 'services/groups-service/groups-service';
17 import { CollectionDirectory, CollectionFile } from 'models/collection-file';
19 export interface ProjectsTreePickerRootItem {
24 export type ProjectsTreePickerItem = ProjectsTreePickerRootItem | GroupContentsResource | CollectionDirectory | CollectionFile | LinkResource;
26 export const treePickerSearchMiddleware: Middleware = store => next => action => {
27 let isSearchAction = false;
28 let searchChanged = false;
30 treePickerSearchActions.match(action, {
31 SET_TREE_PICKER_PROJECT_SEARCH: ({ pickerId, projectSearchValue }) => {
32 isSearchAction = true;
33 searchChanged = store.getState().treePickerSearch.projectSearchValues[pickerId] !== projectSearchValue;
36 SET_TREE_PICKER_COLLECTION_FILTER: ({ pickerId, collectionFilterValue }) => {
37 isSearchAction = true;
38 searchChanged = store.getState().treePickerSearch.collectionFilterValues[pickerId] !== collectionFilterValue;
41 REFRESH_TREE_PICKER: refreshPickers(store),
45 if (isSearchAction && !searchChanged) {
49 // pass it on to the reducer
50 const r = next(action);
52 treePickerSearchActions.match(action, {
53 SET_TREE_PICKER_PROJECT_SEARCH: ({ pickerId }) =>
54 store.dispatch<any>((dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
55 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
57 const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
58 dispatch<any>(loadProject({
60 id: SEARCH_PROJECT_ID,
67 SET_TREE_PICKER_COLLECTION_FILTER: refreshPickers(store),
74 const refreshPickers = (store: MiddlewareAPI) => ({ pickerId }) =>
75 store.dispatch<any>((dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
78 const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
79 getNodeDescendantsIds('')(picker)
81 const node = getNode(id)(picker);
82 if (node && node.status !== TreeNodeStatus.INITIAL) {
83 if (node.id.substring(6, 11) === 'tpzed' || node.id.substring(6, 11) === 'j7d0g') {
84 dispatch<any>(loadProject({
90 if (node.id === SHARED_PROJECT_ID) {
91 dispatch<any>(loadProject({
98 if (node.id === SEARCH_PROJECT_ID) {
99 dispatch<any>(loadProject({
106 if (node.id === FAVORITES_PROJECT_ID) {
107 dispatch<any>(loadFavoritesProject({
112 if (node.id === PUBLIC_FAVORITES_PROJECT_ID) {
113 dispatch<any>(loadPublicFavoritesProject({