Merge branch 'master' into 13864-Virtual-machines
[arvados-workbench2.git] / src / store / store.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { createStore, applyMiddleware, compose, Middleware, combineReducers, Store, Action, Dispatch } from 'redux';
6 import { routerMiddleware, routerReducer } from "react-router-redux";
7 import thunkMiddleware from 'redux-thunk';
8 import { History } from "history";
9
10 import { authReducer } from "./auth/auth-reducer";
11 import { dataExplorerReducer } from './data-explorer/data-explorer-reducer';
12 import { detailsPanelReducer } from './details-panel/details-panel-reducer';
13 import { contextMenuReducer } from './context-menu/context-menu-reducer';
14 import { reducer as formReducer } from 'redux-form';
15 import { favoritesReducer } from './favorites/favorites-reducer';
16 import { snackbarReducer } from './snackbar/snackbar-reducer';
17 import { collectionPanelFilesReducer } from './collection-panel/collection-panel-files/collection-panel-files-reducer';
18 import { dataExplorerMiddleware } from "./data-explorer/data-explorer-middleware";
19 import { FAVORITE_PANEL_ID } from "./favorite-panel/favorite-panel-action";
20 import { PROJECT_PANEL_ID } from "./project-panel/project-panel-action";
21 import { ProjectPanelMiddlewareService } from "./project-panel/project-panel-middleware-service";
22 import { FavoritePanelMiddlewareService } from "./favorite-panel/favorite-panel-middleware-service";
23 import { collectionPanelReducer } from './collection-panel/collection-panel-reducer';
24 import { dialogReducer } from './dialog/dialog-reducer';
25 import { ServiceRepository } from "~/services/services";
26 import { treePickerReducer } from './tree-picker/tree-picker-reducer';
27 import { resourcesReducer } from '~/store/resources/resources-reducer';
28 import { propertiesReducer } from './properties/properties-reducer';
29 import { RootState } from './store';
30 import { fileUploaderReducer } from './file-uploader/file-uploader-reducer';
31 import { TrashPanelMiddlewareService } from "~/store/trash-panel/trash-panel-middleware-service";
32 import { TRASH_PANEL_ID } from "~/store/trash-panel/trash-panel-action";
33 import { processLogsPanelReducer } from './process-logs-panel/process-logs-panel-reducer';
34 import { processPanelReducer } from '~/store/process-panel/process-panel-reducer';
35 import { SHARED_WITH_ME_PANEL_ID } from '~/store/shared-with-me-panel/shared-with-me-panel-actions';
36 import { SharedWithMeMiddlewareService } from './shared-with-me-panel/shared-with-me-middleware-service';
37 import { progressIndicatorReducer } from './progress-indicator/progress-indicator-reducer';
38 import { runProcessPanelReducer } from '~/store/run-process-panel/run-process-panel-reducer';
39 import { WorkflowMiddlewareService } from './workflow-panel/workflow-middleware-service';
40 import { WORKFLOW_PANEL_ID } from './workflow-panel/workflow-panel-actions';
41 import { appInfoReducer } from '~/store/app-info/app-info-reducer';
42 import { searchBarReducer } from './search-bar/search-bar-reducer';
43 import { SEARCH_RESULTS_PANEL_ID } from '~/store/search-results-panel/search-results-panel-actions';
44 import { SearchResultsMiddlewareService } from './search-results-panel/search-results-middleware-service';
45 import { resourcesDataReducer } from "~/store/resources-data/resources-data-reducer";
46 import { virtualMachinesReducer } from "~/store/virtual-machines/virtual-machines-reducer";
47 import { repositoriesReducer } from '~/store/repositories/repositories-reducer';
48
49 const composeEnhancers =
50     (process.env.NODE_ENV === 'development' &&
51         window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
52     compose;
53
54 export type RootState = ReturnType<ReturnType<typeof createRootReducer>>;
55
56 export type RootStore = Store<RootState, Action> & { dispatch: Dispatch<any> };
57
58 export function configureStore(history: History, services: ServiceRepository): RootStore {
59     const rootReducer = createRootReducer(services);
60
61     const projectPanelMiddleware = dataExplorerMiddleware(
62         new ProjectPanelMiddlewareService(services, PROJECT_PANEL_ID)
63     );
64     const favoritePanelMiddleware = dataExplorerMiddleware(
65         new FavoritePanelMiddlewareService(services, FAVORITE_PANEL_ID)
66     );
67     const trashPanelMiddleware = dataExplorerMiddleware(
68         new TrashPanelMiddlewareService(services, TRASH_PANEL_ID)
69     );
70     const searchResultsPanelMiddleware = dataExplorerMiddleware(
71         new SearchResultsMiddlewareService(services, SEARCH_RESULTS_PANEL_ID)
72     );
73     const sharedWithMePanelMiddleware = dataExplorerMiddleware(
74         new SharedWithMeMiddlewareService(services, SHARED_WITH_ME_PANEL_ID)
75     );
76     const workflowPanelMiddleware = dataExplorerMiddleware(
77         new WorkflowMiddlewareService(services, WORKFLOW_PANEL_ID)
78     );
79
80     const middlewares: Middleware[] = [
81         routerMiddleware(history),
82         thunkMiddleware.withExtraArgument(services),
83         projectPanelMiddleware,
84         favoritePanelMiddleware,
85         trashPanelMiddleware,
86         searchResultsPanelMiddleware,
87         sharedWithMePanelMiddleware,
88         workflowPanelMiddleware
89     ];
90     const enhancer = composeEnhancers(applyMiddleware(...middlewares));
91     return createStore(rootReducer, enhancer);
92 }
93
94 const createRootReducer = (services: ServiceRepository) => combineReducers({
95     auth: authReducer(services),
96     collectionPanel: collectionPanelReducer,
97     collectionPanelFiles: collectionPanelFilesReducer,
98     contextMenu: contextMenuReducer,
99     dataExplorer: dataExplorerReducer,
100     detailsPanel: detailsPanelReducer,
101     dialog: dialogReducer,
102     favorites: favoritesReducer,
103     form: formReducer,
104     processLogsPanel: processLogsPanelReducer,
105     properties: propertiesReducer,
106     resources: resourcesReducer,
107     resourcesData: resourcesDataReducer,
108     router: routerReducer,
109     snackbar: snackbarReducer,
110     treePicker: treePickerReducer,
111     fileUploader: fileUploaderReducer,
112     processPanel: processPanelReducer,
113     progressIndicator: progressIndicatorReducer,
114     runProcessPanel: runProcessPanelReducer,
115     appInfo: appInfoReducer,
116     searchBar: searchBarReducer,
117     virtualMachines: virtualMachinesReducer,
118     repositories: repositoriesReducer
119 });