You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
4.0 KiB
153 lines
4.0 KiB
6 years ago
|
import React, { PureComponent } from 'react'
|
||
|
import PropTypes from 'prop-types'
|
||
|
import { Switch, Route } from 'react-router-dom'
|
||
|
import FirstTimeFlowSwitch from './first-time-flow-switch'
|
||
|
import Welcome from './welcome'
|
||
6 years ago
|
import SelectAction from './select-action'
|
||
|
import EndOfFlow from './end-of-flow'
|
||
6 years ago
|
import Unlock from '../unlock-page'
|
||
|
import CreatePassword from './create-password'
|
||
|
import SeedPhrase from './seed-phrase'
|
||
6 years ago
|
import MetaMetricsOptInScreen from './metametrics-opt-in'
|
||
6 years ago
|
import {
|
||
|
DEFAULT_ROUTE,
|
||
|
INITIALIZE_WELCOME_ROUTE,
|
||
|
INITIALIZE_CREATE_PASSWORD_ROUTE,
|
||
|
INITIALIZE_SEED_PHRASE_ROUTE,
|
||
|
INITIALIZE_UNLOCK_ROUTE,
|
||
6 years ago
|
INITIALIZE_SELECT_ACTION_ROUTE,
|
||
|
INITIALIZE_END_OF_FLOW_ROUTE,
|
||
6 years ago
|
INITIALIZE_METAMETRICS_OPT_IN_ROUTE,
|
||
6 years ago
|
} from '../../helpers/constants/routes'
|
||
6 years ago
|
|
||
|
export default class FirstTimeFlow extends PureComponent {
|
||
|
static propTypes = {
|
||
|
completedOnboarding: PropTypes.bool,
|
||
|
createNewAccount: PropTypes.func,
|
||
|
createNewAccountFromSeed: PropTypes.func,
|
||
|
history: PropTypes.object,
|
||
|
isInitialized: PropTypes.bool,
|
||
|
isUnlocked: PropTypes.bool,
|
||
|
unlockAccount: PropTypes.func,
|
||
6 years ago
|
nextRoute: PropTypes.func,
|
||
6 years ago
|
}
|
||
|
|
||
|
state = {
|
||
|
seedPhrase: '',
|
||
|
isImportedKeyring: false,
|
||
|
}
|
||
|
|
||
|
componentDidMount () {
|
||
|
const { completedOnboarding, history, isInitialized, isUnlocked } = this.props
|
||
|
|
||
|
if (completedOnboarding) {
|
||
|
history.push(DEFAULT_ROUTE)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
if (isInitialized && !isUnlocked) {
|
||
|
history.push(INITIALIZE_UNLOCK_ROUTE)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
handleCreateNewAccount = async password => {
|
||
|
const { createNewAccount } = this.props
|
||
|
|
||
|
try {
|
||
|
const seedPhrase = await createNewAccount(password)
|
||
|
this.setState({ seedPhrase })
|
||
|
} catch (error) {
|
||
|
throw new Error(error.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
handleImportWithSeedPhrase = async (password, seedPhrase) => {
|
||
|
const { createNewAccountFromSeed } = this.props
|
||
|
|
||
|
try {
|
||
|
await createNewAccountFromSeed(password, seedPhrase)
|
||
|
this.setState({ isImportedKeyring: true })
|
||
|
} catch (error) {
|
||
|
throw new Error(error.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
handleUnlock = async password => {
|
||
6 years ago
|
const { unlockAccount, history, nextRoute } = this.props
|
||
6 years ago
|
|
||
|
try {
|
||
|
const seedPhrase = await unlockAccount(password)
|
||
|
this.setState({ seedPhrase }, () => {
|
||
6 years ago
|
history.push(nextRoute)
|
||
6 years ago
|
})
|
||
|
} catch (error) {
|
||
|
throw new Error(error.message)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
render () {
|
||
|
const { seedPhrase, isImportedKeyring } = this.state
|
||
|
|
||
|
return (
|
||
|
<div className="first-time-flow">
|
||
|
<Switch>
|
||
|
<Route
|
||
|
path={INITIALIZE_SEED_PHRASE_ROUTE}
|
||
|
render={props => (
|
||
|
<SeedPhrase
|
||
|
{ ...props }
|
||
|
seedPhrase={seedPhrase}
|
||
|
/>
|
||
|
)}
|
||
|
/>
|
||
|
<Route
|
||
|
path={INITIALIZE_CREATE_PASSWORD_ROUTE}
|
||
|
render={props => (
|
||
|
<CreatePassword
|
||
|
{ ...props }
|
||
6 years ago
|
isImportedKeyring={isImportedKeyring}
|
||
6 years ago
|
onCreateNewAccount={this.handleCreateNewAccount}
|
||
|
onCreateNewAccountFromSeed={this.handleImportWithSeedPhrase}
|
||
|
/>
|
||
|
)}
|
||
|
/>
|
||
6 years ago
|
<Route
|
||
|
path={INITIALIZE_SELECT_ACTION_ROUTE}
|
||
|
component={SelectAction}
|
||
|
/>
|
||
6 years ago
|
<Route
|
||
|
path={INITIALIZE_UNLOCK_ROUTE}
|
||
|
render={props => (
|
||
|
<Unlock
|
||
|
{ ...props }
|
||
|
onSubmit={this.handleUnlock}
|
||
|
/>
|
||
|
)}
|
||
|
/>
|
||
6 years ago
|
<Route
|
||
|
exact
|
||
|
path={INITIALIZE_END_OF_FLOW_ROUTE}
|
||
|
component={EndOfFlow}
|
||
|
/>
|
||
6 years ago
|
<Route
|
||
|
exact
|
||
|
path={INITIALIZE_WELCOME_ROUTE}
|
||
|
component={Welcome}
|
||
|
/>
|
||
6 years ago
|
<Route
|
||
|
exact
|
||
|
path={INITIALIZE_METAMETRICS_OPT_IN_ROUTE}
|
||
|
component={MetaMetricsOptInScreen}
|
||
|
/>
|
||
6 years ago
|
<Route
|
||
|
exact
|
||
|
path="*"
|
||
|
component={FirstTimeFlowSwitch}
|
||
|
/>
|
||
|
</Switch>
|
||
|
</div>
|
||
|
)
|
||
|
}
|
||
|
}
|