1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import 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';
11 export type WithDialogStateProps<T> = {
16 export type WithDialogDispatchProps = {
17 closeDialog: () => void;
20 export type WithDialogProps<T> = WithDialogStateProps<T> & WithDialogDispatchProps;
21 export const withDialog = (id: string) =>
22 // TODO: How to make compiler happy with & P instead of & any?
23 // eslint-disable-next-line
24 <T, P>(component: React.ComponentType<WithDialogProps<T> & any>) =>
25 connect(mapStateToProps(id), mapDispatchToProps(id))(component);
29 export const mapStateToProps = (id: string) => <T>(state: { dialog: DialogState }): WithDialogStateProps<T> => {
30 const dialog = state.dialog[id];
31 return dialog ? dialog : { open: false, data: emptyData };
34 export const mapDispatchToProps = (id: string) => (dispatch: Dispatch): WithDialogDispatchProps => ({
36 dispatch(dialogActions.CLOSE_DIALOG({ id }));