modals-are-closed-after-using-browsers-back-or-forward-option
[arvados-workbench2.git] / src / store / dialog / dialog-reducer.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { DialogAction, dialogActions } from "./dialog-actions";
6
7 export type DialogState = Record<string, Dialog<any>>;
8
9 export interface Dialog<T> {
10     open: boolean;
11     data: T;
12 }
13
14 const initialState: DialogState = {};
15
16 export const dialogReducer = (state: DialogState = initialState, action: DialogAction) =>
17
18     dialogActions.match(action, {
19         OPEN_DIALOG: ({ id, data }) => ({ ...state, [id]: { open: true, data } }),
20         CLOSE_DIALOG: ({ id }) => ({
21             ...state,
22             [id]: state[id] ? { ...state[id], open: false } : { open: false, data: {} }
23         }),
24         CLOSE_ALL_DIALOGS: () => ({ ...initialState }),
25         default: () => state,
26     });
27
28 export const getDialog = <T>(state: DialogState, id: string) =>
29     state[id] ? state[id] as Dialog<T> : undefined;