remove use of thunk action in ble and hub

This is a step towards moving these to sagas
This commit is contained in:
David Lechner
2020-05-27 20:57:22 -05:00
committed by David Lechner
parent 99fd62100d
commit 24acab901c
16 changed files with 356 additions and 260 deletions
+4 -6
View File
@@ -7,7 +7,7 @@ import Image from 'react-bootstrap/Image';
import OverlayTrigger from 'react-bootstrap/OverlayTrigger';
import Tooltip from 'react-bootstrap/Tooltip';
export interface ActionButtonProps<T = undefined> {
export interface ActionButtonProps {
/** A unique id for each instance. */
readonly id: string;
/** Tooltip text that appears when hovering over the button. */
@@ -16,13 +16,11 @@ export interface ActionButtonProps<T = undefined> {
readonly icon: string;
/** When true or undefined, the button is enabled. */
readonly enabled?: boolean;
/** Optional action hint passed to the onAction() callback. */
readonly context?: T;
/** Callback that is called when the button is activated (clicked). */
readonly onAction: (context?: T) => void;
readonly onAction: () => void;
}
class ActionButton<T = undefined> extends React.Component<ActionButtonProps<T>> {
class ActionButton extends React.Component<ActionButtonProps> {
render(): JSX.Element {
return (
<OverlayTrigger
@@ -35,7 +33,7 @@ class ActionButton<T = undefined> extends React.Component<ActionButtonProps<T>>
>
<Button
variant="light"
onClick={(): void => this.props.onAction(this.props.context)}
onClick={(): void => this.props.onAction()}
disabled={this.props.enabled === false}
style={
this.props.enabled === false
+5 -17
View File
@@ -2,9 +2,8 @@
// Copyright (c) 2020 The Pybricks Authors
import { connect } from 'react-redux';
import { AnyAction } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
import { connect as bleConnect, disconnect as bleDisconnect } from '../actions/ble';
import { Action, Dispatch } from '../actions';
import { toggleBluetooth } from '../actions/ble';
import { RootState } from '../reducers';
import { BLEConnectionState } from '../reducers/ble';
import { BootloaderConnectionState } from '../reducers/bootloader';
@@ -12,11 +11,8 @@ import ActionButton, { ActionButtonProps } from './ActionButton';
import btConnectedIcon from './images/bt-connected.svg';
import btDisconnectedIcon from './images/bt-disconnected.svg';
type Dispatch = ThunkDispatch<{}, {}, AnyAction>;
type ButtonProps = ActionButtonProps<string>;
type StateProps = Pick<ButtonProps, 'tooltip' | 'icon' | 'context' | 'enabled'>;
type DispatchProps = Pick<ButtonProps, 'onAction'>;
type StateProps = Pick<ActionButtonProps, 'tooltip' | 'icon' | 'enabled'>;
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
const mapStateToProps = (state: RootState): StateProps => {
if (
@@ -26,27 +22,19 @@ const mapStateToProps = (state: RootState): StateProps => {
return {
tooltip: 'Connect using Bluetooth',
icon: btDisconnectedIcon,
context: 'connect',
enabled: true,
};
} else {
return {
tooltip: 'Disconnect Bluetooth',
icon: btConnectedIcon,
context: 'disconnect',
enabled: state.ble.connection === BLEConnectionState.Connected,
};
}
};
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
onAction: (c): void => {
if (c === 'connect') {
dispatch(bleConnect());
} else {
dispatch(bleDisconnect());
}
},
onAction: (): Action => dispatch(toggleBluetooth()),
});
export default connect(mapStateToProps, mapDispatchToProps)(ActionButton);
+4 -9
View File
@@ -2,17 +2,14 @@
// Copyright (c) 2020 The Pybricks Authors
import { connect } from 'react-redux';
import { AnyAction } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
import { startRepl } from '../actions/hub';
import { Action, Dispatch } from '../actions';
import { repl } from '../actions/hub';
import { RootState } from '../reducers';
import { HubRuntimeState } from '../reducers/hub';
import ActionButton, { ActionButtonProps } from './ActionButton';
import replIcon from './images/repl.svg';
type Dispatch = ThunkDispatch<{}, {}, AnyAction>;
type StateProps = Pick<ActionButtonProps, 'enabled' | 'context'>;
type StateProps = Pick<ActionButtonProps, 'enabled'>;
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
type OwnProps = Pick<ActionButtonProps, 'id'>;
@@ -23,9 +20,7 @@ const mapStateToProps = (state: RootState): StateProps => ({
});
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
onAction: (): void => {
dispatch(startRepl());
},
onAction: (): Action => dispatch(repl()),
});
const mergeProps = (
+6 -33
View File
@@ -1,59 +1,32 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { Ace } from 'ace-builds';
import { connect } from 'react-redux';
import { AnyAction } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
import { Action, Dispatch } from '../actions';
import { downloadAndRun } from '../actions/hub';
import { compile } from '../actions/mpy';
import * as notification from '../actions/notification';
import { RootState } from '../reducers';
import { HubRuntimeState } from '../reducers/hub';
import ActionButton, { ActionButtonProps } from './ActionButton';
import runIcon from './images/run.svg';
type Dispatch = ThunkDispatch<{}, {}, AnyAction>;
type ButtonProps = ActionButtonProps<Ace.EditSession>;
type StateProps = Pick<ButtonProps, 'enabled' | 'context'>;
type DispatchProps = Pick<ButtonProps, 'onAction'>;
type OwnProps = Pick<ButtonProps, 'id'>;
type StateProps = Pick<ActionButtonProps, 'enabled'>;
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
type OwnProps = Pick<ActionButtonProps, 'id'>;
const mapStateToProps = (state: RootState): StateProps => ({
enabled:
state.editor.current !== null && state.hub.runtime === HubRuntimeState.Idle,
context: state.editor.current || undefined,
});
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
onAction: (c): void => {
if (!c) {
console.error('No current editor');
return;
}
const script = c.getValue();
// TODO: need to get options from hub because they depend on firmware compile options
dispatch(compile(script, ['-mno-unicode']))
.then((mpy) => {
if (mpy.data) {
dispatch(downloadAndRun(mpy.data));
} else {
dispatch(
notification.add('error', mpy.err || 'Unknown compiler error.'),
);
}
})
.catch((err) => console.error(err));
},
onAction: (): Action => dispatch(downloadAndRun()),
});
const mergeProps = (
stateProps: StateProps,
dispatchProps: DispatchProps,
ownProps: OwnProps,
): ButtonProps => ({
tooltip: 'Download and run this program',
): ActionButtonProps => ({
icon: runIcon,
...ownProps,
...stateProps,
+1 -1
View File
@@ -8,7 +8,7 @@ import { RootState } from '../reducers';
import ActionButton, { ActionButtonProps } from './ActionButton';
import downloadIcon from './images/download.svg';
type StateProps = Pick<ActionButtonProps, 'enabled' | 'context'>;
type StateProps = Pick<ActionButtonProps, 'enabled'>;
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
type OwnProps = Pick<ActionButtonProps, 'id'>;
+3 -8
View File
@@ -2,17 +2,14 @@
// Copyright (c) 2020 The Pybricks Authors
import { connect } from 'react-redux';
import { AnyAction } from 'redux';
import { ThunkDispatch } from 'redux-thunk';
import { Action, Dispatch } from '../actions';
import { stop } from '../actions/hub';
import { RootState } from '../reducers';
import { HubRuntimeState } from '../reducers/hub';
import ActionButton, { ActionButtonProps } from './ActionButton';
import stopIcon from './images/stop.svg';
type Dispatch = ThunkDispatch<{}, {}, AnyAction>;
type StateProps = Pick<ActionButtonProps, 'enabled' | 'context'>;
type StateProps = Pick<ActionButtonProps, 'enabled'>;
type DispatchProps = Pick<ActionButtonProps, 'onAction'>;
type OwnProps = Pick<ActionButtonProps, 'id'>;
@@ -21,9 +18,7 @@ const mapStateToProps = (state: RootState): StateProps => ({
});
const mapDispatchToProps = (dispatch: Dispatch): DispatchProps => ({
onAction: (): void => {
dispatch(stop());
},
onAction: (): Action => dispatch(stop()),
});
const mergeProps = (