Create actions and reducer for favorites
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 23 Jul 2018 16:26:30 +0000 (18:26 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 23 Jul 2018 16:26:30 +0000 (18:26 +0200)
Feature #13784

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

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

diff --git a/src/store/favorites/favorites-actions.ts b/src/store/favorites/favorites-actions.ts
new file mode 100644 (file)
index 0000000..63ea206
--- /dev/null
@@ -0,0 +1,11 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { unionize, ofType, UnionOf } from "unionize";
+
+export const favoritesActions = unionize({
+    UPDATE_FAVORITES: ofType<Record<string, boolean>>()
+}, { tag: 'type', value: 'payload' });
+
+export type FavoritesAction = UnionOf<typeof favoritesActions>;
\ No newline at end of file
diff --git a/src/store/favorites/favorites-reducer.ts b/src/store/favorites/favorites-reducer.ts
new file mode 100644 (file)
index 0000000..9599db0
--- /dev/null
@@ -0,0 +1,15 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { FavoritesAction, favoritesActions } from "./favorites-actions";
+
+export type FavoritesState = Record<string, boolean>;
+
+export const favoritesReducer = (state: FavoritesState = {}, action: FavoritesAction) => 
+    favoritesActions.match(action, {
+        UPDATE_FAVORITES: favorites => ({...state, ...favorites}),
+        default: () => state
+    });
+
+export const isFavorite = (uuid: string, state: FavoritesState) => state[uuid] === true;
\ No newline at end of file
index adb7ddde133d8ac0b6c07e82d6d702017ed0bd10..e7dbe16f49163f7b1cde01b8a08b7463b37bcbce 100644 (file)
@@ -14,6 +14,7 @@ import { dataExplorerReducer, DataExplorerState } from './data-explorer/data-exp
 import { projectPanelMiddleware } from './project-panel/project-panel-middleware';
 import { detailsPanelReducer, DetailsPanelState } from './details-panel/details-panel-reducer';
 import { contextMenuReducer, ContextMenuState } from './context-menu/context-menu-reducer';
+import { FavoritesState, favoritesReducer } from './favorites/favorites-reducer';
 
 const composeEnhancers =
     (process.env.NODE_ENV === 'development' &&
@@ -28,6 +29,7 @@ export interface RootState {
     sidePanel: SidePanelState;
     detailsPanel: DetailsPanelState;
     contextMenu: ContextMenuState;
+    favorites: FavoritesState;
 }
 
 const rootReducer = combineReducers({
@@ -37,7 +39,8 @@ const rootReducer = combineReducers({
     dataExplorer: dataExplorerReducer,
     sidePanel: sidePanelReducer,
     detailsPanel: detailsPanelReducer,
-    contextMenu: contextMenuReducer
+    contextMenu: contextMenuReducer,
+    favorites: favoritesReducer,
 });