meriadec
7 years ago
11 changed files with 270 additions and 38 deletions
@ -0,0 +1,145 @@ |
|||
// @flow
|
|||
|
|||
import React, { PureComponent } from 'react' |
|||
import { compose } from 'redux' |
|||
import { connect } from 'react-redux' |
|||
import { translate } from 'react-i18next' |
|||
import type { Currency } from '@ledgerhq/live-common/lib/types' |
|||
|
|||
import type { T, Device } from 'types/common' |
|||
|
|||
import { getCurrentDevice } from 'reducers/devices' |
|||
|
|||
import Modal, { ModalContent, ModalTitle, ModalFooter, ModalBody } from 'components/base/Modal' |
|||
import Box from 'components/base/Box' |
|||
import Breadcrumb from 'components/Breadcrumb' |
|||
|
|||
import StepChooseCurrency, { StepChooseCurrencyFooter } from './steps/01-step-choose-currency' |
|||
import StepConnectDevice, { StepConnectDeviceFooter } from './steps/02-step-connect-device' |
|||
import StepInProgress from './steps/03-step-in-progress' |
|||
import StepFinish from './steps/04-step-finish' |
|||
|
|||
const createSteps = ({ t }: { t: T }) => [ |
|||
{ |
|||
id: 'chooseCurrency', |
|||
label: t('importAccounts:breadcrumb.informations'), |
|||
component: StepChooseCurrency, |
|||
footer: StepChooseCurrencyFooter, |
|||
hideFooter: false, |
|||
}, |
|||
{ |
|||
id: 'connectDevice', |
|||
label: t('importAccounts:breadcrumb.connectDevice'), |
|||
component: StepConnectDevice, |
|||
footer: StepConnectDeviceFooter, |
|||
hideFooter: false, |
|||
}, |
|||
{ |
|||
id: 'import', |
|||
label: t('importAccounts:breadcrumb.import'), |
|||
component: StepInProgress, |
|||
footer: null, |
|||
hideFooter: true, |
|||
}, |
|||
{ |
|||
id: 'finish', |
|||
label: t('importAccounts:breadcrumb.finish'), |
|||
component: StepFinish, |
|||
hideFooter: false, |
|||
footer: StepChooseCurrencyFooter, |
|||
}, |
|||
] |
|||
|
|||
type Props = { |
|||
t: T, |
|||
currentDevice: ?Device, |
|||
} |
|||
|
|||
type StepId = 'chooseCurrency' | 'connectDevice' | 'import' | 'finish' |
|||
|
|||
export type StepProps = { |
|||
t: T, |
|||
currency: ?Currency, |
|||
currentDevice: ?Device, |
|||
isAppOpened: boolean, |
|||
transitionTo: StepId => void, |
|||
setState: any => void, |
|||
} |
|||
|
|||
type State = { |
|||
stepId: StepId, |
|||
isAppOpened: boolean, |
|||
currency: ?Currency, |
|||
scannedAccounts: [], |
|||
} |
|||
|
|||
const mapStateToProps = state => ({ |
|||
currentDevice: getCurrentDevice(state), |
|||
}) |
|||
|
|||
const INITIAL_STATE = { |
|||
stepId: 'chooseCurrency', |
|||
isAppOpened: false, |
|||
currency: null, |
|||
scannedAccounts: [], |
|||
} |
|||
|
|||
class ImportAccounts extends PureComponent<Props, State> { |
|||
state = INITIAL_STATE |
|||
STEPS = createSteps({ |
|||
t: this.props.t, |
|||
}) |
|||
|
|||
transitionTo = stepId => { |
|||
this.setState({ stepId }) |
|||
} |
|||
|
|||
render() { |
|||
const { t, currentDevice } = this.props |
|||
const { stepId, currency, isAppOpened } = this.state |
|||
|
|||
const stepIndex = this.STEPS.findIndex(s => s.id === stepId) |
|||
const step = this.STEPS[stepIndex] |
|||
|
|||
if (!step) { |
|||
throw new Error(`ImportAccountsModal: step ${stepId} doesn't exists`) |
|||
} |
|||
|
|||
const { component: StepComponent, footer: StepFooter, hideFooter } = step |
|||
|
|||
const stepProps: StepProps = { |
|||
t, |
|||
currency, |
|||
currentDevice, |
|||
isAppOpened, |
|||
transitionTo: this.transitionTo, |
|||
setState: (...args) => this.setState(...args), |
|||
} |
|||
|
|||
return ( |
|||
<Modal |
|||
name="importAccounts" |
|||
preventBackdropClick |
|||
onHide={() => this.setState({ ...INITIAL_STATE })} |
|||
render={({ onClose }) => ( |
|||
<ModalBody onClose={onClose}> |
|||
<ModalTitle>{t('importAccounts:title')}</ModalTitle> |
|||
<ModalContent> |
|||
<Breadcrumb mb={6} currentStep={stepIndex} items={this.STEPS} /> |
|||
<StepComponent {...stepProps} /> |
|||
</ModalContent> |
|||
{!hideFooter && ( |
|||
<ModalFooter> |
|||
<Box horizontal alignItems="center" justifyContent="flex-end"> |
|||
{StepFooter ? <StepFooter {...stepProps} /> : <Box>footer</Box>} |
|||
</Box> |
|||
</ModalFooter> |
|||
)} |
|||
</ModalBody> |
|||
)} |
|||
/> |
|||
) |
|||
} |
|||
} |
|||
|
|||
export default compose(connect(mapStateToProps), translate())(ImportAccounts) |
@ -0,0 +1,22 @@ |
|||
// @flow
|
|||
|
|||
import React from 'react' |
|||
|
|||
import SelectCurrency from 'components/SelectCurrency' |
|||
import Button from 'components/base/Button' |
|||
|
|||
import type { StepProps } from '../index' |
|||
|
|||
function StepChooseCurrency({ currency, setState }: StepProps) { |
|||
return <SelectCurrency onChange={currency => setState({ currency })} value={currency} /> |
|||
} |
|||
|
|||
export function StepChooseCurrencyFooter({ transitionTo, currency, t }: StepProps) { |
|||
return ( |
|||
<Button primary disabled={!currency} onClick={() => transitionTo('connectDevice')}> |
|||
{t('common:next')} |
|||
</Button> |
|||
) |
|||
} |
|||
|
|||
export default StepChooseCurrency |
@ -0,0 +1,33 @@ |
|||
// @flow
|
|||
|
|||
import React from 'react' |
|||
|
|||
import Button from 'components/base/Button' |
|||
import ConnectDevice from 'components/modals/StepConnectDevice' |
|||
|
|||
import type { StepProps } from '../index' |
|||
|
|||
function StepConnectDevice({ t, currency, currentDevice, setState }: StepProps) { |
|||
return ( |
|||
<ConnectDevice |
|||
t={t} |
|||
deviceSelected={currentDevice} |
|||
currency={currency} |
|||
onStatusChange={s => { |
|||
if (s === 'connected') { |
|||
setState({ isAppOpened: true }) |
|||
} |
|||
}} |
|||
/> |
|||
) |
|||
} |
|||
|
|||
export function StepConnectDeviceFooter({ t, transitionTo, isAppOpened }: StepProps) { |
|||
return ( |
|||
<Button primary disabled={!isAppOpened} onClick={() => transitionTo('import')}> |
|||
{t('common:next')} |
|||
</Button> |
|||
) |
|||
} |
|||
|
|||
export default StepConnectDevice |
@ -0,0 +1,11 @@ |
|||
// @flow
|
|||
|
|||
import React from 'react' |
|||
|
|||
import Box from 'components/base/Box' |
|||
|
|||
function StepInProgress() { |
|||
return <Box>{'StepInProgress'}</Box> |
|||
} |
|||
|
|||
export default StepInProgress |
@ -0,0 +1,11 @@ |
|||
// @flow
|
|||
|
|||
import React from 'react' |
|||
|
|||
import Box from 'components/base/Box' |
|||
|
|||
function StepFinish() { |
|||
return <Box>{'StepFinish'}</Box> |
|||
} |
|||
|
|||
export default StepFinish |
@ -0,0 +1,6 @@ |
|||
title: Import accounts |
|||
breadcrumb: |
|||
informations: Informations |
|||
connectDevice: Connect device |
|||
import: Import accounts |
|||
finish: Confirm |
Loading…
Reference in new issue