...
[arvados-workbench2.git] / src / store / dialog / dialog-reducer.ts
index 6013ab64c1bc240404679963fd0d355a1e402c16..48f8ee8a1e6caac149fb07b8c94a10a46a3f2377 100644 (file)
@@ -4,17 +4,21 @@
 
 import { DialogAction, dialogActions } from "./dialog-actions";
 
-export type DialogState = Record<string, Dialog>;
+export type DialogState = Record<string, Dialog<any>>;
 
-export interface Dialog {
+export interface Dialog <T> {
     open: boolean;
-    data?: any;
+    data: T;
 }
 
 export const dialogReducer = (state: DialogState = {}, action: DialogAction) =>
     dialogActions.match(action, {
         OPEN_DIALOG: ({ id, data }) => ({ ...state, [id]: { open: true, data } }),
-        CLOSE_DIALOG: ({ id }) => ({ ...state, [id]: { open: false } }),
+        CLOSE_DIALOG: ({ id }) => ({ 
+            ...state, 
+            [id]: state[id] ? { ...state[id], open: false } : { open: false, data: {} } }),
         default: () => state,
     });
 
+export const getDialog = <T>(state: DialogState, id: string) => 
+    state[id] ? state[id] as Dialog<T> : undefined;