Merge branch 'master' into 14043-switch-tagging-adding-properties
[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 import { Dispatch } from 'redux';
9 import { dialogActions } from './dialog-actions';
10
11 export type WithDialogStateProps<T> = {
12     open: boolean;
13     data: T;
14 };
15
16 export type WithDialogDispatchProps = {
17     closeDialog: () => void;
18 };
19
20 export type WithDialogProps<T> = WithDialogStateProps<T> & WithDialogDispatchProps;
21
22 export const withDialog = (id: string) =>
23     <T, P>(component: React.ComponentType<WithDialogProps<T> & P>) =>
24         connect(mapStateToProps(id), mapDispatchToProps(id))(component);
25
26 export const mapStateToProps = (id: string) => <T>(state: { dialog: DialogState }): WithDialogStateProps<T> => {
27     const dialog = state.dialog[id];
28     return dialog ? dialog : { open: false, data: {} };
29 };
30
31 export const mapDispatchToProps = (id: string) => (dispatch: Dispatch): WithDialogDispatchProps => ({
32     closeDialog: () => {
33         dispatch(dialogActions.CLOSE_DIALOG({ id }));
34     }
35 });