cr changes
[arvados.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
32 const composeEnhancers =
33     (process.env.NODE_ENV === 'development' &&
34         window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
35     compose;
36
37 export type RootState = ReturnType<ReturnType<typeof createRootReducer>>;
38
39 export type RootStore = Store<RootState, Action> & { dispatch: Dispatch<any> };
40
41 export function configureStore(history: History, services: ServiceRepository): RootStore {
42     const rootReducer = createRootReducer(services);
43
44     const projectPanelMiddleware = dataExplorerMiddleware(
45         new ProjectPanelMiddlewareService(services, PROJECT_PANEL_ID)
46     );
47     const favoritePanelMiddleware = dataExplorerMiddleware(
48         new FavoritePanelMiddlewareService(services, FAVORITE_PANEL_ID)
49     );
50
51     const middlewares: Middleware[] = [
52         routerMiddleware(history),
53         thunkMiddleware.withExtraArgument(services),
54         projectPanelMiddleware,
55         favoritePanelMiddleware
56     ];
57     const enhancer = composeEnhancers(applyMiddleware(...middlewares));
58     return createStore(rootReducer, enhancer);
59 }
60
61 const createRootReducer = (services: ServiceRepository) => combineReducers({
62     auth: authReducer(services),
63     collectionPanel: collectionPanelReducer,
64     collectionPanelFiles: collectionPanelFilesReducer,
65     contextMenu: contextMenuReducer,
66     dataExplorer: dataExplorerReducer,
67     detailsPanel: detailsPanelReducer,
68     dialog: dialogReducer,
69     favorites: favoritesReducer,
70     form: formReducer,
71     properties: propertiesReducer,
72     resources: resourcesReducer,
73     router: routerReducer,
74     snackbar: snackbarReducer,
75     treePicker: treePickerReducer,
76     fileUploader: fileUploaderReducer,
77 });