Create store for custom properties
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 23 Aug 2018 12:53:00 +0000 (14:53 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 23 Aug 2018 12:53:00 +0000 (14:53 +0200)
Feature #14102

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/store/properties/properties-actions.ts [new file with mode: 0644]
src/store/properties/properties-reducer.ts [new file with mode: 0644]
src/store/properties/properties.ts [new file with mode: 0644]
src/store/store.ts

diff --git a/src/store/properties/properties-actions.ts b/src/store/properties/properties-actions.ts
new file mode 100644 (file)
index 0000000..8647a9c
--- /dev/null
@@ -0,0 +1,12 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { unionize, ofType, UnionOf } from '~/common/unionize';
+
+export const propertiesActions = unionize({
+    SET_PROPERTY: ofType<{ key: string, value: any }>(),
+    DELETE_PROPERTY: ofType<string>(),
+});
+
+export type PropertiesAction = UnionOf<typeof propertiesActions>;
diff --git a/src/store/properties/properties-reducer.ts b/src/store/properties/properties-reducer.ts
new file mode 100644 (file)
index 0000000..27fdf85
--- /dev/null
@@ -0,0 +1,14 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { PropertiesState, setProperty, deleteProperty } from './properties';
+import { PropertiesAction, propertiesActions } from './properties-actions';
+
+
+export const propertiesReducer = (state: PropertiesState = {}, action: PropertiesAction) =>
+    propertiesActions.match(action, {
+        SET_PROPERTY: ({ key, value }) => setProperty(key, value)(state),
+        DELETE_PROPERTY: key => deleteProperty(key)(state),
+        default: () => state,
+    });
\ No newline at end of file
diff --git a/src/store/properties/properties.ts b/src/store/properties/properties.ts
new file mode 100644 (file)
index 0000000..bee5b19
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export type PropertiesState = { [key: string]: any };
+
+export const getProperty = <T>(id: string) =>
+    (state: PropertiesState): T | undefined =>
+        state[id];
+
+export const setProperty = <T>(id: string, data: T) =>
+    (state: PropertiesState) => ({
+        ...state,
+        [id]: data
+    });
+
+export const deleteProperty = (id: string) =>
+    (state: PropertiesState) => {
+        const newState = { ...state };
+        delete newState[id];
+        return newState;
+    };
+
index eb36fa4b90cc1eae2f3042b34cd0a52ca423bd38..0e548c587dbcd90a7119d51f65f6c32514513b3d 100644 (file)
@@ -3,79 +3,44 @@
 // SPDX-License-Identifier: AGPL-3.0
 
 import { createStore, applyMiddleware, compose, Middleware, combineReducers, Store, Action, Dispatch } from 'redux';
-import { routerMiddleware, routerReducer, RouterState } from "react-router-redux";
+import { routerMiddleware, routerReducer } 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 { projectsReducer } from "./project/project-reducer";
+import { authReducer } from "./auth/auth-reducer";
+import { dataExplorerReducer } from './data-explorer/data-explorer-reducer';
+import { detailsPanelReducer } from './details-panel/details-panel-reducer';
+import { contextMenuReducer } 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 { CollectionPanelFilesState } from './collection-panel/collection-panel-files/collection-panel-files-state';
+import { favoritesReducer } from './favorites/favorites-reducer';
+import { snackbarReducer } from './snackbar/snackbar-reducer';
 import { collectionPanelFilesReducer } from './collection-panel/collection-panel-files/collection-panel-files-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 { CollectionPanelState, collectionPanelReducer } from './collection-panel/collection-panel-reducer';
-import { DialogState, dialogReducer } from './dialog/dialog-reducer';
-import { CollectionsState, collectionsReducer } from './collections/collections-reducer';
+import { collectionPanelReducer } from './collection-panel/collection-panel-reducer';
+import { dialogReducer } from './dialog/dialog-reducer';
+import { collectionsReducer } from './collections/collections-reducer';
 import { ServiceRepository } from "~/services/services";
 import { treePickerReducer } from './tree-picker/tree-picker-reducer';
-import { TreePicker } from './tree-picker/tree-picker';
-import { ResourcesState } from '~/store/resources/resources';
 import { resourcesReducer } from '~/store/resources/resources-reducer';
+import { propertiesReducer } from './properties/properties-reducer';
+import { RootState } from './store';
 
 const composeEnhancers =
     (process.env.NODE_ENV === 'development' &&
         window && window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__) ||
     compose;
 
-export interface RootState {
-    auth: AuthState;
-    projects: ProjectState;
-    collections: CollectionsState;
-    router: RouterState;
-    dataExplorer: DataExplorerState;
-    sidePanel: SidePanelState;
-    collectionPanel: CollectionPanelState;
-    detailsPanel: DetailsPanelState;
-    contextMenu: ContextMenuState;
-    favorites: FavoritesState;
-    snackbar: SnackbarState;
-    collectionPanelFiles: CollectionPanelFilesState;
-    dialog: DialogState;
-    treePicker: TreePicker;
-    resources: ResourcesState;
-}
+export type RootState = ReturnType<ReturnType<typeof createRootReducer>>;
 
 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,
-        collections: collectionsReducer,
-        router: routerReducer,
-        dataExplorer: dataExplorerReducer,
-        sidePanel: sidePanelReducer,
-        collectionPanel: collectionPanelReducer,
-        detailsPanel: detailsPanelReducer,
-        contextMenu: contextMenuReducer,
-        form: formReducer,
-        favorites: favoritesReducer,
-        snackbar: snackbarReducer,
-        collectionPanelFiles: collectionPanelFilesReducer,
-        dialog: dialogReducer,
-        treePicker: treePickerReducer,
-        resources: resourcesReducer,
-    });
+    const rootReducer = createRootReducer(services);
 
     const projectPanelMiddleware = dataExplorerMiddleware(
         new ProjectPanelMiddlewareService(services, PROJECT_PANEL_ID)
@@ -93,3 +58,22 @@ export function configureStore(history: History, services: ServiceRepository): R
     const enhancer = composeEnhancers(applyMiddleware(...middlewares));
     return createStore(rootReducer, enhancer);
 }
+
+const createRootReducer = (services: ServiceRepository) => combineReducers({
+    auth: authReducer(services),
+    projects: projectsReducer,
+    collections: collectionsReducer,
+    router: routerReducer,
+    dataExplorer: dataExplorerReducer,
+    collectionPanel: collectionPanelReducer,
+    detailsPanel: detailsPanelReducer,
+    contextMenu: contextMenuReducer,
+    form: formReducer,
+    favorites: favoritesReducer,
+    snackbar: snackbarReducer,
+    collectionPanelFiles: collectionPanelFilesReducer,
+    dialog: dialogReducer,
+    treePicker: treePickerReducer,
+    resources: resourcesReducer,
+    properties: propertiesReducer,
+});