Create generic dialog state
[arvados-workbench2.git] / src / store / dialog / with-dialog.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import * as React from 'react';
6 import { connect } from 'react-redux';
7 import { DialogState } from './dialog-reducer';
8
9 export type WithDialog<T> = {
10     open: boolean;
11     data?: T;
12 };
13
14 export const withDialog = (id: string) =>
15     <T>(component: React.ComponentType<WithDialog<T>>) =>
16         connect(mapStateToProps(id))(component);
17
18 export const mapStateToProps = (id: string) => <T>(state: { dialog: DialogState }): WithDialog<T> => {
19     const dialog = state.dialog[id];
20     return dialog ? dialog : { open: false };
21 };