From f1f6b2076d06eb34123886aea0009b37abd13a57 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 18 Dec 2021 11:33:07 -0600 Subject: [PATCH] firmware: convert FlashButton to function --- src/firmware/FlashButton.tsx | 72 +++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 34 deletions(-) diff --git a/src/firmware/FlashButton.tsx b/src/firmware/FlashButton.tsx index 79f5b5a7..996a1fa9 100644 --- a/src/firmware/FlashButton.tsx +++ b/src/firmware/FlashButton.tsx @@ -1,49 +1,53 @@ // SPDX-License-Identifier: MIT // Copyright (c) 2020-2021 The Pybricks Authors -import { connect } from 'react-redux'; +import React from 'react'; +import { useDispatch, useSelector } from 'react-redux'; import { BleConnectionState } from '../ble/reducers'; import { BootloaderConnectionState } from '../lwp3-bootloader/reducers'; -import * as notification from '../notifications/actions'; +import * as notificationActions from '../notifications/actions'; import { RootState } from '../reducers'; import OpenFileButton, { OpenFileButtonProps } from '../toolbar/OpenFileButton'; import { TooltipId } from '../toolbar/i18n'; import { flashFirmware } from './actions'; import firmwareIcon from './firmware.svg'; -type StateProps = Pick< - OpenFileButtonProps, - 'tooltip' | 'enabled' | 'showProgress' | 'progress' ->; -type DispatchProps = Pick; -type OwnProps = Pick; +type FlashButtonProps = Pick; -const mapStateToProps = (state: RootState): StateProps => ({ - tooltip: state.firmware.flashing ? TooltipId.FlashProgress : TooltipId.Flash, - enabled: - state.bootloader.connection === BootloaderConnectionState.Disconnected && - state.ble.connection === BleConnectionState.Disconnected, - showProgress: state.firmware.flashing, - progress: state.firmware.progress === null ? undefined : state.firmware.progress, -}); +const FlashButton: React.FunctionComponent = (props) => { + const bootloaderConnection = useSelector( + (state: RootState) => state.bootloader.connection, + ); + const bleConnection = useSelector((state: RootState) => state.ble.connection); + const flashing = useSelector((state: RootState) => state.firmware.flashing); + const progress = useSelector((state: RootState) => state.firmware.progress); -const mapDispatchToProps: DispatchProps = { - onFile: flashFirmware, - onReject: (file) => - notification.add('error', `'${file.name}' is not a valid firmware file.`), - onClick: () => flashFirmware(null), + const dispatch = useDispatch(); + + return ( + dispatch(flashFirmware(data))} + onReject={(file) => + dispatch( + notificationActions.add( + 'error', + `'${file.name}' is not a valid firmware file.`, + ), + ) + } + onClick={() => dispatch(flashFirmware(null))} + {...props} + /> + ); }; -const mergeProps = ( - stateProps: StateProps, - dispatchProps: DispatchProps, - ownProps: OwnProps, -): OpenFileButtonProps => ({ - fileExtension: '.zip', - icon: firmwareIcon, - ...ownProps, - ...stateProps, - ...dispatchProps, -}); - -export default connect(mapStateToProps, mapDispatchToProps, mergeProps)(OpenFileButton); +export default FlashButton;