refs #master Merge branch 'origin/master' into 13901-services-repo
[arvados-workbench2.git] / src / store / store.ts
index 20ee09d40609bd83343a548fbb1b9bf6bf1c2b86..37343619fe2c4319288574a615bffe01f1e3e485 100644 (file)
@@ -2,26 +2,79 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { createStore, applyMiddleware, compose, Middleware } from 'redux';
-import { default as rootReducer, RootState } from "./root-reducer";
+import { createStore, applyMiddleware, compose, Middleware, combineReducers, Store, Action, Dispatch } from 'redux';
+import { routerMiddleware, routerReducer, RouterState } from "react-router-redux";
+import thunkMiddleware from 'redux-thunk';
+import { History } from "history";
+
+import { projectsReducer, ProjectState } from "./project/project-reducer";
+import { sidePanelReducer, SidePanelState } from './side-panel/side-panel-reducer';
+import { authReducer, AuthState } from "./auth/auth-reducer";
+import { dataExplorerReducer, DataExplorerState } from './data-explorer/data-explorer-reducer';
+import { detailsPanelReducer, DetailsPanelState } from './details-panel/details-panel-reducer';
+import { contextMenuReducer, ContextMenuState } from './context-menu/context-menu-reducer';
+import { reducer as formReducer } from 'redux-form';
+import { FavoritesState, favoritesReducer } from './favorites/favorites-reducer';
+import { snackbarReducer, SnackbarState } from './snackbar/snackbar-reducer';
+import { dataExplorerMiddleware } from "./data-explorer/data-explorer-middleware";
+import { FAVORITE_PANEL_ID } from "./favorite-panel/favorite-panel-action";
+import { PROJECT_PANEL_ID } from "./project-panel/project-panel-action";
+import { ProjectPanelMiddlewareService } from "./project-panel/project-panel-middleware-service";
+import { FavoritePanelMiddlewareService } from "./favorite-panel/favorite-panel-middleware-service";
+import { CollectionCreatorState, collectionCreationReducer } from './collections/creator/collection-creator-reducer';
+import { CollectionPanelState, collectionPanelReducer } from './collection-panel/collection-panel-reducer';
+import { ServiceRepository } from "../services/services";
 
 const composeEnhancers =
     (process.env.NODE_ENV === 'development' &&
-    window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
+        window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
     compose;
 
-function configureStore(initialState?: RootState) {
-    const middlewares: Middleware[] = [];
-    const enhancer = composeEnhancers(applyMiddleware(...middlewares));
-    return createStore(rootReducer, initialState!, enhancer);
+export interface RootState {
+    auth: AuthState;
+    projects: ProjectState;
+    collectionCreation: CollectionCreatorState;
+    router: RouterState;
+    dataExplorer: DataExplorerState;
+    sidePanel: SidePanelState;
+    collectionPanel: CollectionPanelState;
+    detailsPanel: DetailsPanelState;
+    contextMenu: ContextMenuState;
+    favorites: FavoritesState;
+    snackbar: SnackbarState;
 }
 
-const store = configureStore({
-    projects: [
-        { name: 'Mouse genome', createdAt: '2018-05-01' },
-        { name: 'Human body', createdAt: '2018-05-01' },
-        { name: 'Secret operation', createdAt: '2018-05-01' }
-    ]
-});
+export type RootStore = Store<RootState, Action> & { dispatch: Dispatch<any> };
+
+export function configureStore(history: History, services: ServiceRepository): RootStore {
+       const rootReducer = combineReducers({
+           auth: authReducer(services),
+           projects: projectsReducer,
+           collectionCreation: collectionCreationReducer,
+           router: routerReducer,
+           dataExplorer: dataExplorerReducer,
+           sidePanel: sidePanelReducer,
+           collectionPanel: collectionPanelReducer,
+           detailsPanel: detailsPanelReducer,
+           contextMenu: contextMenuReducer,
+           form: formReducer,
+           favorites: favoritesReducer,
+           snackbar: snackbarReducer,
+       });
 
-export default store;
+    const projectPanelMiddleware = dataExplorerMiddleware(
+        new ProjectPanelMiddlewareService(services, PROJECT_PANEL_ID)
+    );
+    const favoritePanelMiddleware = dataExplorerMiddleware(
+        new FavoritePanelMiddlewareService(services, FAVORITE_PANEL_ID)
+    );
+
+    const middlewares: Middleware[] = [
+        routerMiddleware(history),
+        thunkMiddleware.withExtraArgument(services),
+        projectPanelMiddleware,
+        favoritePanelMiddleware
+    ];
+    const enhancer = composeEnhancers(applyMiddleware(...middlewares));
+    return createStore(rootReducer, enhancer);
+}