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
+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);