22141: Add details for more object types
[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 { UserResource } from "models/user";
17 import { GroupContentsResource } from 'services/groups-service/groups-service';
18 import { CollectionDirectory, CollectionFile } from 'models/collection-file';
19
20 export interface ProjectsTreePickerRootItem {
21     id: string;
22     name: string;
23 }
24
25 export type ProjectsTreePickerItem = ProjectsTreePickerRootItem | GroupContentsResource | CollectionDirectory | CollectionFile | LinkResource | UserResource;
26
27 export const treePickerSearchMiddleware: Middleware = store => next => action => {
28     let isSearchAction = false;
29     let searchChanged = false;
30
31     treePickerSearchActions.match(action, {
32         SET_TREE_PICKER_PROJECT_SEARCH: ({ pickerId, projectSearchValue }) => {
33             isSearchAction = true;
34             searchChanged = store.getState().treePickerSearch.projectSearchValues[pickerId] !== projectSearchValue;
35         },
36
37         SET_TREE_PICKER_COLLECTION_FILTER: ({ pickerId, collectionFilterValue }) => {
38             isSearchAction = true;
39             searchChanged = store.getState().treePickerSearch.collectionFilterValues[pickerId] !== collectionFilterValue;
40         },
41
42         REFRESH_TREE_PICKER: refreshPickers(store),
43         default: () => { }
44     });
45
46     if (isSearchAction && !searchChanged) {
47         return;
48     }
49
50     // pass it on to the reducer
51     const r = next(action);
52
53     treePickerSearchActions.match(action, {
54         SET_TREE_PICKER_PROJECT_SEARCH: ({ pickerId }) =>
55             store.dispatch<any>((dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
56                 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
57                 if (picker) {
58                     const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
59                     dispatch<any>(loadProject({
60                         ...loadParams,
61                         id: SEARCH_PROJECT_ID,
62                         pickerId: pickerId,
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                             }));
104                         }
105                         if (node.id === FAVORITES_PROJECT_ID) {
106                             dispatch<any>(loadFavoritesProject({
107                                 ...loadParams,
108                                 pickerId: pickerId,
109                             }));
110                         }
111                         if (node.id === PUBLIC_FAVORITES_PROJECT_ID) {
112                             dispatch<any>(loadPublicFavoritesProject({
113                                 ...loadParams,
114                                 pickerId: pickerId,
115                             }));
116                         }
117                     }
118                     return id;
119                 });
120         }
121     })