Merge branch '19899-cache-control-fix' refs #19899
[arvados-workbench2.git] / 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 } 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         default: () => { }
41     });
42
43     if (isSearchAction && !searchChanged) {
44         return;
45     }
46
47     // pass it on to the reducer
48     const r = next(action);
49
50     treePickerSearchActions.match(action, {
51         SET_TREE_PICKER_PROJECT_SEARCH: ({ pickerId }) =>
52             store.dispatch<any>((dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
53                 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
54                 if (picker) {
55                     const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
56                     dispatch<any>(loadProject({
57                         ...loadParams,
58                         id: SEARCH_PROJECT_ID,
59                         pickerId: pickerId,
60                         searchProjects: true
61                     }));
62                 }
63             }),
64
65         SET_TREE_PICKER_COLLECTION_FILTER: ({ pickerId }) =>
66             store.dispatch<any>((dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
67                 const picker = getTreePicker<ProjectsTreePickerItem>(pickerId)(getState().treePicker);
68                 if (picker) {
69                     const loadParams = getState().treePickerSearch.loadProjectParams[pickerId];
70                     getNodeDescendantsIds('')(picker)
71                         .map(id => {
72                             const node = getNode(id)(picker);
73                             if (node && node.status !== TreeNodeStatus.INITIAL) {
74                                 if (node.id.substring(6, 11) === 'tpzed' || node.id.substring(6, 11) === 'j7d0g') {
75                                     dispatch<any>(loadProject({
76                                         ...loadParams,
77                                         id: node.id,
78                                         pickerId: pickerId,
79                                     }));
80                                 }
81                                 if (node.id === SHARED_PROJECT_ID) {
82                                     dispatch<any>(loadProject({
83                                         ...loadParams,
84                                         id: node.id,
85                                         pickerId: pickerId,
86                                         loadShared: true
87                                     }));
88                                 }
89                                 if (node.id === SEARCH_PROJECT_ID) {
90                                     dispatch<any>(loadProject({
91                                         ...loadParams,
92                                         id: node.id,
93                                         pickerId: pickerId,
94                                         searchProjects: true
95                                     }));
96                                 }
97                                 if (node.id === FAVORITES_PROJECT_ID) {
98                                     dispatch<any>(loadFavoritesProject({
99                                         ...loadParams,
100                                         pickerId: pickerId,
101                                     }));
102                                 }
103                                 if (node.id === PUBLIC_FAVORITES_PROJECT_ID) {
104                                     dispatch<any>(loadPublicFavoritesProject({
105                                         ...loadParams,
106                                         pickerId: pickerId,
107                                     }));
108                                 }
109                             }
110                             return id;
111                         });
112                 }
113             }),
114         default: () => { }
115     });
116
117     return r;
118 }