Browse Source

Fix DeviceConnect for multi device

master
Loëck Vézien 7 years ago
parent
commit
605a7cd96b
No known key found for this signature in database GPG Key ID: CBCDCE384E853AC4
  1. 2
      src/components/DeviceConnect/index.js
  2. 51
      src/components/DeviceMonitNew/index.js
  3. 10
      src/components/modals/AddAccount/index.js
  4. 17
      src/components/modals/Receive/index.js
  5. 12
      src/components/modals/StepConnectDevice.js
  6. 2
      static/i18n/en/deviceConnect.yml

2
src/components/DeviceConnect/index.js

@ -206,7 +206,7 @@ class DeviceConnect extends PureComponent<Props> {
{hasMultipleDevices && ( {hasMultipleDevices && (
<ListDevices> <ListDevices>
<Box color="graphite" fontSize={3}> <Box color="graphite" fontSize={3}>
{t('deviceConnect:step1.choose', { devicesCount: devices.length })} {t('deviceConnect:step1.choose', { count: devices.length })}
</Box> </Box>
<Box flow={2}> <Box flow={2}>
{devices.map(d => { {devices.map(d => {

51
src/components/DeviceMonitNew/index.js

@ -14,29 +14,28 @@ const mapStateToProps = state => ({
devices: getDevices(state), devices: getDevices(state),
}) })
type DeviceStatus = type DeviceStatus = 'unconnected' | 'connected'
| 'unconnected'
| 'connected' type AppStatus = 'success' | 'fail' | 'progress'
| 'appOpened.success'
| 'appOpened.fail'
| 'appOpened.progress'
type Props = { type Props = {
coinType: number, coinType: number,
devices: Devices, devices: Devices,
deviceSelected: Device | null, deviceSelected: Device | null,
account?: Account, account?: Account,
onStatusChange?: DeviceStatus => void, onStatusChange?: (DeviceStatus, AppStatus) => void,
render?: Function, render?: Function,
} }
type State = { type State = {
status: DeviceStatus, deviceStatus: DeviceStatus,
appStatus: AppStatus,
} }
class DeviceMonit extends PureComponent<Props, State> { class DeviceMonit extends PureComponent<Props, State> {
state = { state = {
status: this.props.deviceSelected ? 'connected' : 'unconnected', appStatus: 'progress',
deviceStatus: this.props.deviceSelected ? 'connected' : 'unconnected',
} }
componentDidMount() { componentDidMount() {
@ -47,19 +46,18 @@ class DeviceMonit extends PureComponent<Props, State> {
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
const { status } = this.state const { deviceStatus } = this.state
const { deviceSelected, devices } = this.props const { deviceSelected, devices } = this.props
const { devices: nextDevices, deviceSelected: nextDeviceSelected } = nextProps const { devices: nextDevices, deviceSelected: nextDeviceSelected } = nextProps
if (status === 'unconnected' && !deviceSelected && nextDeviceSelected) { if (deviceStatus === 'unconnected' && !deviceSelected && nextDeviceSelected) {
this.handleStatusChange('connected') this.handleStatusChange('connected', 'progress')
} }
if (status !== 'unconnected' && devices !== nextDevices) { if (deviceStatus !== 'unconnected' && devices !== nextDevices) {
const isConnected = nextDevices.find(d => d === nextDeviceSelected) const isConnected = nextDevices.find(d => d === nextDeviceSelected)
if (!isConnected) { if (!isConnected) {
this.handleStatusChange('unconnected') this.handleStatusChange('unconnected', 'progress')
clearTimeout(this._timeout)
} }
} }
} }
@ -69,7 +67,7 @@ class DeviceMonit extends PureComponent<Props, State> {
const { deviceSelected: prevDeviceSelected } = prevProps const { deviceSelected: prevDeviceSelected } = prevProps
if (prevDeviceSelected !== deviceSelected) { if (prevDeviceSelected !== deviceSelected) {
this.handleStatusChange('appOpened.progress') this.handleStatusChange('connected', 'progress')
this._timeout = setTimeout(this.checkAppOpened, 250) this._timeout = setTimeout(this.checkAppOpened, 250)
} }
} }
@ -109,13 +107,15 @@ class DeviceMonit extends PureComponent<Props, State> {
_timeout: any = null _timeout: any = null
handleStatusChange = status => { handleStatusChange = (deviceStatus, appStatus) => {
const { onStatusChange } = this.props const { onStatusChange } = this.props
this.setState({ status }) clearTimeout(this._timeout)
onStatusChange && onStatusChange(status) this.setState({ deviceStatus, appStatus })
onStatusChange && onStatusChange(deviceStatus, appStatus)
} }
handleMsgEvent = (e, { type, data }) => { handleMsgEvent = (e, { type, data }) => {
const { deviceStatus } = this.state
const { deviceSelected } = this.props const { deviceSelected } = this.props
if (deviceSelected === null) { if (deviceSelected === null) {
@ -123,26 +123,27 @@ class DeviceMonit extends PureComponent<Props, State> {
} }
if (type === 'wallet.checkIfAppOpened.success' && deviceSelected.path === data.devicePath) { if (type === 'wallet.checkIfAppOpened.success' && deviceSelected.path === data.devicePath) {
clearTimeout(this._timeout) this.handleStatusChange(deviceStatus, 'success')
this.handleStatusChange('appOpened.success') this._timeout = setTimeout(this.checkAppOpened, 1e3)
} }
if (type === 'wallet.checkIfAppOpened.fail' && deviceSelected.path === data.devicePath) { if (type === 'wallet.checkIfAppOpened.fail' && deviceSelected.path === data.devicePath) {
this.handleStatusChange('appOpened.fail') this.handleStatusChange(deviceStatus, 'fail')
this._timeout = setTimeout(this.checkAppOpened, 1e3) this._timeout = setTimeout(this.checkAppOpened, 1e3)
} }
} }
render() { render() {
const { coinType, account, devices, deviceSelected, render } = this.props const { coinType, account, devices, deviceSelected, render } = this.props
const { status } = this.state const { appStatus, deviceStatus } = this.state
if (render) { if (render) {
return render({ return render({
appStatus,
coinType: (account && account.coinType) || coinType, coinType: (account && account.coinType) || coinType,
status,
devices, devices,
deviceSelected: status === 'connected' ? deviceSelected : null, deviceSelected: deviceStatus === 'connected' ? deviceSelected : null,
deviceStatus,
}) })
} }

10
src/components/modals/AddAccount/index.js

@ -67,7 +67,7 @@ type State = {
deviceSelected: Device | null, deviceSelected: Device | null,
fetchingCounterValues: boolean, fetchingCounterValues: boolean,
selectedAccounts: Array<number>, selectedAccounts: Array<number>,
status: null | string, appStatus: null | string,
stepIndex: number, stepIndex: number,
} }
@ -77,7 +77,7 @@ const INITIAL_STATE = {
deviceSelected: null, deviceSelected: null,
fetchingCounterValues: false, fetchingCounterValues: false,
selectedAccounts: [], selectedAccounts: [],
status: null, appStatus: null,
stepIndex: 0, stepIndex: 0,
} }
@ -148,8 +148,8 @@ class AddAccountModal extends PureComponent<Props, State> {
} }
if (stepIndex === 1) { if (stepIndex === 1) {
const { deviceSelected, status } = this.state const { deviceSelected, appStatus } = this.state
return deviceSelected !== null && status === 'appOpened.success' return deviceSelected !== null && appStatus === 'success'
} }
if (stepIndex === 3) { if (stepIndex === 3) {
@ -225,7 +225,7 @@ class AddAccountModal extends PureComponent<Props, State> {
handleChangeCurrency = (currency: Currency) => this.setState({ currency }) handleChangeCurrency = (currency: Currency) => this.setState({ currency })
handleChangeStatus = status => this.setState({ status }) handleChangeStatus = (deviceStatus, appStatus) => this.setState({ appStatus })
handleImportAccount = () => { handleImportAccount = () => {
const { archivedAccounts, updateAccount } = this.props const { archivedAccounts, updateAccount } = this.props

17
src/components/modals/Receive/index.js

@ -22,9 +22,10 @@ type Props = {
} }
type State = { type State = {
account: Account | null,
deviceSelected: Device | null, deviceSelected: Device | null,
appStatus: null | string,
stepIndex: number, stepIndex: number,
account: Account | null,
} }
const GET_STEPS = t => [ const GET_STEPS = t => [
@ -35,10 +36,11 @@ const GET_STEPS = t => [
const INITIAL_STATE = { const INITIAL_STATE = {
account: null, account: null,
deviceSelected: null, deviceSelected: null,
appStatus: null,
stepIndex: 0, stepIndex: 0,
} }
class SendModal extends PureComponent<Props, State> { class ReceiveModal extends PureComponent<Props, State> {
state = INITIAL_STATE state = INITIAL_STATE
_steps = GET_STEPS(this.props.t) _steps = GET_STEPS(this.props.t)
@ -50,6 +52,11 @@ class SendModal extends PureComponent<Props, State> {
return acc !== null return acc !== null
} }
if (stepIndex === 1) {
const { deviceSelected, appStatus } = this.state
return deviceSelected !== null && appStatus === 'success'
}
return false return false
} }
@ -67,6 +74,8 @@ class SendModal extends PureComponent<Props, State> {
handleChangeAccount = account => this.setState({ account }) handleChangeAccount = account => this.setState({ account })
handleChangeStatus = (deviceStatus, appStatus) => this.setState({ appStatus })
renderStep = acc => { renderStep = acc => {
const { deviceSelected, stepIndex } = this.state const { deviceSelected, stepIndex } = this.state
const { t } = this.props const { t } = this.props
@ -85,8 +94,10 @@ class SendModal extends PureComponent<Props, State> {
onChangeAccount: this.handleChangeAccount, onChangeAccount: this.handleChangeAccount,
}), }),
...props(stepIndex === 1, { ...props(stepIndex === 1, {
accountName: acc ? acc.name : undefined,
deviceSelected, deviceSelected,
onChangeDevice: this.handleChangeDevice, onChangeDevice: this.handleChangeDevice,
onStatusChange: this.handleChangeStatus,
}), }),
} }
@ -144,4 +155,4 @@ class SendModal extends PureComponent<Props, State> {
} }
} }
export default translate()(SendModal) export default translate()(ReceiveModal)

12
src/components/modals/StepConnectDevice.js

@ -2,13 +2,15 @@
import React from 'react' import React from 'react'
import type { Account, Currency } from '@ledgerhq/currencies/lib/types' import type { Account } from '@ledgerhq/wallet-common/lib/types'
import type { Currency } from '@ledgerhq/currencies/lib/types'
import type { Device } from 'types/common' import type { Device } from 'types/common'
import DeviceConnect from 'components/DeviceConnect' import DeviceConnect from 'components/DeviceConnect'
import DeviceMonit from 'components/DeviceMonitNew' import DeviceMonit from 'components/DeviceMonitNew'
type Props = { type Props = {
accountName?: string,
account?: Account, account?: Account,
currency?: Currency | null, currency?: Currency | null,
deviceSelected: Device | null, deviceSelected: Device | null,
@ -22,12 +24,11 @@ const StepConnectDevice = (props: Props) => (
coinType={props.currency && props.currency.coinType} coinType={props.currency && props.currency.coinType}
deviceSelected={props.deviceSelected} deviceSelected={props.deviceSelected}
onStatusChange={props.onStatusChange} onStatusChange={props.onStatusChange}
render={({ coinType, status, devices, deviceSelected }) => ( render={({ coinType, appStatus, devices, deviceSelected }) => (
<DeviceConnect <DeviceConnect
accountName={props.accountName}
coinType={coinType} coinType={coinType}
appOpened={ appOpened={appStatus === 'success' ? 'success' : appStatus === 'fail' ? 'fail' : null}
status === 'appOpened.success' ? 'success' : status === 'appOpened.fail' ? 'fail' : null
}
devices={devices} devices={devices}
deviceSelected={deviceSelected} deviceSelected={deviceSelected}
onChangeDevice={props.onChangeDevice} onChangeDevice={props.onChangeDevice}
@ -38,6 +39,7 @@ const StepConnectDevice = (props: Props) => (
StepConnectDevice.defaultProps = { StepConnectDevice.defaultProps = {
account: undefined, account: undefined,
accountName: undefined,
currency: undefined, currency: undefined,
} }

2
static/i18n/en/deviceConnect.yml

@ -1,6 +1,6 @@
step1: step1:
connect: Connect your <0>Ledger device</0> to your computer and enter your <1>PIN code</1> on your device connect: Connect your <0>Ledger device</0> to your computer and enter your <1>PIN code</1> on your device
choose: We detected {{devicesCount}} devices connected, please select one: choose_plural: We detected {{count}} devices connected, please select one:
step2: step2:
open: Open <0>{{appName}}</0> App on your device open: Open <0>{{appName}}</0> App on your device

Loading…
Cancel
Save