15814: Now handles secret inputs properly.
[arvados.git] / services / workbench2 / src / store / tree-picker / tree-picker-middleware.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
11 import {
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';
18
19 export interface ProjectsTreePickerRootItem {
20     id: string;
21     name: string;
22 }
23
24 export type ProjectsTreePickerItem = ProjectsTreePickerRootItem | GroupContentsResource | CollectionDirectory | CollectionFile | LinkResource;
25
26 export const treePickerSearchMiddleware: Middleware = store => next => action => {
27     let isSearchAction = false;
28     let searchChanged = false;
29
30     treePickerSearchActions.match(action, {
31         SET_TREE_PICKER_PROJECT_SEARCH: ({ pickerId, projectSearchValue }) => {
32             isSearchAction = true;
33             searchChanged = store.getState().treePickerSearch.projectSearchValues[pickerId] !== projectSearchValue;
34         },
35
36         SET_TREE_PICKER_COLLECTION_FILTER: ({ pickerId, collectionFilterValue }) => {
37             isSearchAction = true;
38             searchChanged = store.getState().treePickerSearch.collectionFilterValues[pickerId] !== collectionFilterValue;
39         },
40
41         REFRESH_TREE_PICKER: refreshPickers(store),
42         default: () => { }
43     });
44
45     if (isSearchAction && !searchChanged) {
46         return;
47     }
48
49     // pass it on to the reducer
50     const r = next(action);
51
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);
56                 if (picker) {
57                     const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
58                     dispatch<any>(loadProject({
59                         ...loadParams,
60                         id: SEARCH_PROJECT_ID,
61                         pickerId: pickerId,
62                         searchProjects: true
63                     }));
64                 }
65             }),
66
67         SET_TREE_PICKER_COLLECTION_FILTER: refreshPickers(store),
68         default: () => { }
69     });
70
71     return r;
72 }
73
74 const refreshPickers = (store: MiddlewareAPI) => ({ pickerId }) =>
75     store.dispatch<any>((dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
76         const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
77         if (picker) {
78             const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
79             getNodeDescendantsIds('')(picker)
80                 .map(id => {
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({
85                                 ...loadParams,
86                                 id: node.id,
87                                 pickerId: pickerId,
88                             }));
89                         }
90                         if (node.id === SHARED_PROJECT_ID) {
91                             dispatch<any>(loadProject({
92                                 ...loadParams,
93                                 id: node.id,
94                                 pickerId: pickerId,
95                                 loadShared: true
96                             }));
97                         }
98                         if (node.id === SEARCH_PROJECT_ID) {
99                             dispatch<any>(loadProject({
100                                 ...loadParams,
101                                 id: node.id,
102                                 pickerId: pickerId,
103                                 searchProjects: true
104                             }));
105                         }
106                         if (node.id === FAVORITES_PROJECT_ID) {
107                             dispatch<any>(loadFavoritesProject({
108                                 ...loadParams,
109                                 pickerId: pickerId,
110                             }));
111                         }
112                         if (node.id === PUBLIC_FAVORITES_PROJECT_ID) {
113                             dispatch<any>(loadPublicFavoritesProject({
114                                 ...loadParams,
115                                 pickerId: pickerId,
116                             }));
117                         }
118                     }
119                     return id;
120                 });
121         }
122     })